Improve users table sanitization

This commit is contained in:
2020-09-17 17:35:11 +02:00
parent 7573e9ab41
commit 5a5486b751
5 changed files with 57 additions and 40 deletions

View File

@@ -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

View File

@@ -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():