From 2d701ba47344175e7c0d93e88e1f8b0d2424de95 Mon Sep 17 00:00:00 2001 From: coolneng Date: Thu, 10 Sep 2020 23:59:06 +0200 Subject: [PATCH] Sanitize database fields --- database/models.py | 36 +++++------ ...05f3ae5db7c7_rename_modified_to_updated.py | 1 + ...01e489de_set_users_status_default_value.py | 25 ++++++++ ...bff_sanitize_nullable_and_non_nullable_.py | 59 +++++++++++++++++++ ...653ece_set_modified_updated_as_nullable.py | 40 +++++++------ 5 files changed, 125 insertions(+), 36 deletions(-) create mode 100644 migrations/versions/0a4f01e489de_set_users_status_default_value.py create mode 100644 migrations/versions/15a14d428bff_sanitize_nullable_and_non_nullable_.py diff --git a/database/models.py b/database/models.py index cba2cde..af00ed7 100644 --- a/database/models.py +++ b/database/models.py @@ -9,12 +9,12 @@ class Users(Base): id = Column(Integer, primary_key=True, autoincrement=True) social_id = Column(Text) - type = Column(Integer) - full_name = Column(String(255), index=True, unique=True) - email = Column(String(255), index=True, unique=True) + type = Column(Integer, nullable=True) + full_name = Column(String(255), index=True, unique=True, nullable=False) + email = Column(String(255), index=True, unique=True, nullable=False) password = Column(String(255)) gender = Column(Integer) - mobile = Column(String(255)) + mobile = Column(String(255), nullable=False) user_image = Column(String(255)) city_id = Column(Integer, ForeignKey("cities.id")) user_type = Column(Integer) @@ -34,7 +34,7 @@ class Cities(Base): __tablename__ = "cities" id = Column(Integer, primary_key=True, autoincrement=True) - name = Column(String(255)) + name = Column(String(255), nullable=False) image = Column(String(255)) status = Column(Enum("1", "0")) created = Column(DateTime, nullable=False, server_default=func.now()) @@ -45,10 +45,10 @@ class Games(Base): __tablename__ = "games" id = Column(Integer, primary_key=True, autoincrement=True) - name = Column(String(255)) + name = Column(String(255), nullable=False) image = Column(String(255)) date_time = Column(DateTime) - price = Column(String(100)) + price = Column(String(100), nullable=False) description = Column(Text) user_id = Column(Integer, ForeignKey("users.id")) gender = Column(Enum("1", "2", "3")) @@ -126,8 +126,8 @@ class Sports(Base): __tablename__ = "sports" id = Column(Integer, primary_key=True, autoincrement=True) - name = Column(String(255)) - spanish_name = Column(String(100)) + name = Column(String(255), nullable=False) + spanish_name = Column(String(100), nullable=False) status = Column(Integer) created = Column(DateTime, nullable=False, server_default=func.now()) updated = Column(DateTime, nullable=True, onupdate=func.now()) @@ -152,7 +152,7 @@ class UserRatings(Base): game_id = Column(Integer, ForeignKey("games_id")) user_id = Column(Integer, ForeignKey("users_id")) player_id = Column(Integer, ForeignKey("users_id")) - rating = Column(String(100)) + rating = Column(String(100), nullable=False) created = Column(DateTime, nullable=False, server_default=func.now()) updated = Column(DateTime, nullable=True, onupdate=func.now()) user_type = Column(Integer) @@ -174,10 +174,10 @@ class Venues(Base): id = Column(Integer, primary_key=True, autoincrement=True) user_id = Column(Integer, ForeignKey("users_id")) - address = Column(Text) + address = Column(Text, nullable=False) latitude = Column(String(100)) longitude = Column(String(100)) - name = Column(String(100)) + name = Column(String(100), nullable=False) sports_id = Column(Integer, ForeignKey("sports_id")) created = Column(DateTime, nullable=False, server_default=func.now()) updated = Column(DateTime, nullable=True, onupdate=func.now()) @@ -197,11 +197,11 @@ class WebBookings(Base): __tablename__ = "web_bookings" id = Column(Integer, primary_key=True, autoincrement=True) - name = Column(String(255)) - email = Column(String(255)) - contact = Column(String(100)) - message = Column(Text) - game = Column(String(255)) - city = Column(String(100)) + name = Column(String(255), nullable=False) + email = Column(String(255), nullable=False) + contact = Column(String(100), nullable=False) + message = Column(Text, nullable=False) + game = Column(String(255), nullable=False) + city = Column(String(100), nullable=False) created = Column(DateTime, nullable=False, server_default=func.now()) updated = Column(DateTime, nullable=True, onupdate=func.now()) diff --git a/migrations/versions/05f3ae5db7c7_rename_modified_to_updated.py b/migrations/versions/05f3ae5db7c7_rename_modified_to_updated.py index 4e2e11f..05ad985 100644 --- a/migrations/versions/05f3ae5db7c7_rename_modified_to_updated.py +++ b/migrations/versions/05f3ae5db7c7_rename_modified_to_updated.py @@ -27,6 +27,7 @@ def upgrade(): "sports": "modified", "teams": "modified", "user_ratings": "modified", + "users": "modified", "venues": "modified", } for table, field in tables.items(): diff --git a/migrations/versions/0a4f01e489de_set_users_status_default_value.py b/migrations/versions/0a4f01e489de_set_users_status_default_value.py new file mode 100644 index 0000000..d02ef48 --- /dev/null +++ b/migrations/versions/0a4f01e489de_set_users_status_default_value.py @@ -0,0 +1,25 @@ +"""set users status default value + +Revision ID: 0a4f01e489de +Revises: 15a14d428bff +Create Date: 2020-09-10 23:54:22.624598 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = "0a4f01e489de" +down_revision = "15a14d428bff" +branch_labels = None +depends_on = None + + +def upgrade(): + with op.batch_alter_table("users") as batch_op: + batch_op.alter_column(column_name="status", server_default=sa.text("0")) + + +def downgrade(): + pass diff --git a/migrations/versions/15a14d428bff_sanitize_nullable_and_non_nullable_.py b/migrations/versions/15a14d428bff_sanitize_nullable_and_non_nullable_.py new file mode 100644 index 0000000..74d398b --- /dev/null +++ b/migrations/versions/15a14d428bff_sanitize_nullable_and_non_nullable_.py @@ -0,0 +1,59 @@ +"""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 diff --git a/migrations/versions/970563653ece_set_modified_updated_as_nullable.py b/migrations/versions/970563653ece_set_modified_updated_as_nullable.py index f24f8d8..19ef203 100644 --- a/migrations/versions/970563653ece_set_modified_updated_as_nullable.py +++ b/migrations/versions/970563653ece_set_modified_updated_as_nullable.py @@ -18,26 +18,30 @@ depends_on = None def upgrade(): tables = { - "cities": "updated", - "games": "updated", - "payments": "updated", - "player_availabilities": "updated", - "player_cancel_games": "updated", - "purchase_games": "updated", - "sports": "updated", - "teams": "updated", - "user_ratings": "updated", - "venues": "updated", - "venue_images": "updated", - "web_bookings": "updated", + "cities": ["modified"], + "games": ["modified", "cancel_date"], + "payments": ["modified"], + "player_availabilities": ["modified"], + "player_cancel_games": ["modified"], + "purchase_games": ["modified"], + "sports": ["modified"], + "teams": ["modified"], + "user_ratings": ["modified"], + "users": ["modified"], + "venues": ["modified"], + "venue_images": ["updated"], + "web_bookings": ["updated"], } for table, field in tables.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} = '0000-00-00 00:00:00'".format( - table, field - ) - op.execute(query) + 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} = '0000-00-00 00:00:00'".format( + table, item + ) + op.execute(query) def downgrade():