Sanitize database fields

This commit is contained in:
coolneng 2020-09-10 23:59:06 +02:00
parent 9f83f7498f
commit 2d701ba473
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
5 changed files with 125 additions and 36 deletions

View File

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

View File

@ -27,6 +27,7 @@ def upgrade():
"sports": "modified",
"teams": "modified",
"user_ratings": "modified",
"users": "modified",
"venues": "modified",
}
for table, field in tables.items():

View File

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

View File

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

View File

@ -18,24 +18,28 @@ 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():
for item in field:
with op.batch_alter_table(table) as batch_op:
batch_op.alter_column(column_name=field, nullable=True, server_default=None)
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, field
table, item
)
op.execute(query)