odapi/migrations/versions/15a14d428bff_sanitize_nulla...

54 lines
1.5 KiB
Python
Raw Normal View History

2020-09-10 23:59:06 +02:00
"""sanitize nullable and non-nullable fields
Revision ID: 15a14d428bff
Revises: 05f3ae5db7c7
Create Date: 2020-09-10 12:27:31.889962
"""
from alembic import op
# revision identifiers, used by Alembic.
revision = "15a14d428bff"
down_revision = "05f3ae5db7c7"
branch_labels = None
depends_on = None
def upgrade():
nullable = {
2020-09-17 17:35:11 +02:00
"users": ["full_name", "email", "mobile"],
"cities": ["name"],
"web_bookings": ["name", "email", "contact", "message", "game", "city"],
"games": ["name", "price"],
"venues": ["address", "name"],
"sports": ["spanish_name"],
"user_ratings": ["rating"],
"sports": ["name"],
2020-09-10 23:59:06 +02:00
}
non_nullable = {
2020-09-17 17:35:11 +02:00
"users": ["social_id", "type"],
2020-09-10 23:59:06 +02:00
}
2020-09-17 17:35:11 +02:00
for table, field in nullable.items():
for item in field:
query = "UPDATE {0} SET {1} = '' WHERE {1} IS NULL".format(table, item)
op.execute(query)
with op.batch_alter_table(table) as batch_op:
batch_op.alter_column(
column_name=item, nullable=False, server_default=None
)
for table, field in non_nullable.items():
for item in field:
with op.batch_alter_table(table) as batch_op:
batch_op.alter_column(
column_name=item, nullable=True, server_default=None
)
query = "UPDATE {0} SET {1} = NULL WHERE {1} = '' OR {1} = '0'".format(
table, item
2020-09-10 23:59:06 +02:00
)
2020-09-17 17:35:11 +02:00
op.execute(query)
2020-09-10 23:59:06 +02:00
def downgrade():
pass