Import database migrations

This commit is contained in:
coolneng 2021-02-08 02:19:26 +01:00
parent b5d4ed519d
commit c3ce963858
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
13 changed files with 516 additions and 0 deletions

85
alembic.ini Normal file
View File

@ -0,0 +1,85 @@
# A generic, single database configuration.
[alembic]
# path to migration scripts
script_location = migrations
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
# timezone to use when rendering the date
# within the migration file as well as the filename.
# string value is passed to dateutil.tz.gettz()
# leave blank for localtime
# timezone =
# max length of characters to apply to the
# "slug" field
# truncate_slug_length = 40
# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false
# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false
# version location specification; this defaults
# to migrations/versions. When using multiple version
# directories, initial revisions must be specified with --version-path
# version_locations = %(here)s/bar %(here)s/bat migrations/versions
# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8
sqlalchemy.url = mysql+pymysql:///root@localhost/odyfo?unix_socket=.mysql/mysql.sock
[post_write_hooks]
# post_write_hooks defines scripts or Python functions that are run
# on newly generated revision scripts. See the documentation for further
# detail and examples
# format using "black" - use the console_scripts runner, against the "black" entrypoint
# hooks=black
# black.type=console_scripts
# black.entrypoint=black
# black.options=-l 79
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S

1
migrations/README Normal file
View File

@ -0,0 +1 @@
Generic single-database configuration.

77
migrations/env.py Normal file
View File

@ -0,0 +1,77 @@
from logging.config import fileConfig
from sqlalchemy import engine_from_config
from sqlalchemy import pool
from alembic import context
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = None
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def run_migrations_offline():
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url,
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online():
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection, target_metadata=target_metadata
)
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()

24
migrations/script.py.mako Normal file
View File

@ -0,0 +1,24 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade():
${upgrades if upgrades else "pass"}
def downgrade():
${downgrades if downgrades else "pass"}

View File

@ -0,0 +1,39 @@
"""rename modified to updated
Revision ID: 05f3ae5db7c7
Revises: 970563653ece
Create Date: 2020-07-22 23:56:38.694837
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "05f3ae5db7c7"
down_revision = "970563653ece"
branch_labels = None
depends_on = None
def upgrade():
tables = {
"cities": "modified",
"games": "modified",
"payments": "modified",
"player_availabilities": "modified",
"player_cancel_games": "modified",
"purchase_games": "modified",
"sports": "modified",
"teams": "modified",
"user_ratings": "modified",
"users": "modified",
"venues": "modified",
}
for table, field in tables.items():
with op.batch_alter_table(table) as batch_op:
batch_op.alter_column(column_name=field, new_column_name="updated")
def downgrade():
pass

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

@ -0,0 +1,53 @@
"""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

View File

@ -0,0 +1,48 @@
"""set modified/updated as nullable
Revision ID: 970563653ece
Revises:
Create Date: 2020-07-22 23:40:07.674252
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "970563653ece"
down_revision = None
branch_labels = None
depends_on = None
def upgrade():
tables = {
"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=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():
pass

View File

@ -0,0 +1,31 @@
"""set unique fields in users table
Revision ID: 9ee45f714f8b
Revises: 0a4f01e489de
Create Date: 2020-09-12 18:25:17.026880
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "9ee45f714f8b"
down_revision = "0a4f01e489de"
branch_labels = None
depends_on = None
def upgrade():
fields = ["email", "mobile"]
for item in fields:
query = "DELETE FROM users WHERE email = '' AND status = 0"
op.execute(query)
with op.batch_alter_table("users") as batch_op:
batch_op.create_unique_constraint(
constraint_name="uq_users_{}".format(item), columns=[item],
)
def downgrade():
pass

View File

@ -0,0 +1,29 @@
"""set otp as integer
Revision ID: d994081ed483
Revises: 1387db583e1d
Create Date: 2020-09-25 07:11:21.603504
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "d994081ed483"
down_revision = "1387db583e1d"
branch_labels = None
depends_on = None
def upgrade():
with op.batch_alter_table("users") as batch_op:
batch_op.alter_column(
column_name="otp", existing_type=sa.String(length=255), type_=sa.Integer,
)
query = "SELECT CAST(otp AS INTEGER) from users"
op.execute(query)
def downgrade():
pass

View File

@ -0,0 +1,52 @@
"""add foreign keys
Revision ID: e443e876bf33
Revises: f8ef6bad794a
Create Date: 2020-10-26 14:37:21.156557
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "e443e876bf33"
down_revision = "f8ef6bad794a"
branch_labels = None
depends_on = None
def upgrade():
tables = {
"games": ["user_id", "city_id", "venue_id", "sports_id"],
"payments": ["user_id", "game_id"],
"player_availabilities": ["game_id", "player_id"],
"player_cancel_games": ["game_id", "player_id"],
"purchase_games": ["game_id", "user_id"],
"teams": ["game_id", "user_id"],
"users": ["city_id"],
"user_ratings": ["game_id", "user_id", "player_id"],
"venues": ["user_id", "sports_id"],
"venue_images": ["venue_id", "user_id"],
}
referent_tables = {
"user_id": "users",
"city_id": "cities",
"venue_id": "venues",
"sports_id": "sports",
"game_id": "games",
"player_id": "users",
}
for table, field in tables.items():
for item in field:
with op.batch_alter_table(table) as batch_op:
batch_op.create_foreign_key(
constraint_name="fk_{}".format(item),
referent_table=referent_tables[item],
local_cols=[item],
remote_cols=["id"],
)
def downgrade():
pass

View File

@ -0,0 +1,27 @@
"""add forgot_password field
Revision ID: f8ef6bad794a
Revises: d994081ed483
Create Date: 2020-10-08 18:04:08.204841
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "f8ef6bad794a"
down_revision = "d994081ed483"
branch_labels = None
depends_on = None
def upgrade():
with op.batch_alter_table("users") as batch_op:
batch_op.add_column(
sa.Column("forgot_password", sa.Integer, server_default=sa.text("0")),
)
def downgrade():
pass