"""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 = { "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"], } non_nullable = { "users": ["social_id", "type"], } 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 ) op.execute(query) def downgrade(): pass