Improve users table sanitization
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
"""set default value for badge
|
||||
|
||||
Revision ID: 1387db583e1d
|
||||
Revises: 9ee45f714f8b
|
||||
Create Date: 2020-09-15 19:31:15.709945
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "1387db583e1d"
|
||||
down_revision = "9ee45f714f8b"
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
with op.batch_alter_table("users") as batch_op:
|
||||
batch_op.alter_column(column_name="badge", server_default=sa.text("0"))
|
||||
|
||||
|
||||
def downgrade():
|
||||
pass
|
||||
@@ -17,42 +17,36 @@ 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",
|
||||
"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 = {
|
||||
"social_id": "users",
|
||||
"type": "users",
|
||||
"users": ["social_id", "type"],
|
||||
}
|
||||
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 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
|
||||
)
|
||||
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)
|
||||
op.execute(query)
|
||||
|
||||
|
||||
def downgrade():
|
||||
|
||||
Reference in New Issue
Block a user