odapi/migrations/versions/15a14d428bff_sanitize_nulla...

60 lines
1.6 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 = {
"full_name": "users",
"email": "users",
"mobile": "users",
"name": "cities",
"name": "games",
"price": "games",
"name": "web_bookings",
"email": "web_bookings",
"contact": "web_bookings",
"message": "web_bookings",
"game": "web_bookings",
"city": "web_bookings",
"address": "venues",
"name": "venues",
"spanish_name": "sports",
"rating": "user_ratings",
"name": "sports",
}
non_nullable = {
"social_id": "users",
"type": "users",
}
for field, table in nullable.items():
query = "UPDATE {0} SET {1} = '' WHERE {1} IS NULL".format(table, field)
op.execute(query)
with op.batch_alter_table(table) as batch_op:
batch_op.alter_column(
column_name=field, nullable=False, server_default=None
)
for field, table in non_nullable.items():
with op.batch_alter_table(table) as batch_op:
batch_op.alter_column(column_name=field, nullable=True, server_default=None)
query = "UPDATE {0} SET {1} = NULL WHERE {1} = '' OR {1} = '0'".format(
table, field
)
op.execute(query)
def downgrade():
pass