[odrs-web/UBI8: 4/6] Recreate migrations




commit 650bcff75ff334226edccd6b1326bb6961cf3bd4
Author: Bartłomiej Piotrowski <bpiotrowski gnome org>
Date:   Mon Apr 25 11:04:45 2022 +0200

    Recreate migrations

 migrations/README                                  |   1 +
 migrations/alembic.ini                             |   7 +-
 migrations/env.py                                  |  57 ++++----
 migrations/script.py.mako                          |  12 +-
 migrations/versions/036f0cd034e5_.py               |  23 ---
 migrations/versions/19526c284b29_.py               |  80 -----------
 migrations/versions/1b966aab67a1_.py               |  39 ------
 migrations/versions/64751cf97429_.py               |  25 ----
 migrations/versions/6f54fde07d02_.py               |  46 ------
 migrations/versions/7c3432c40267_.py               |  45 ------
 migrations/versions/84deb10331db_.py               |  35 -----
 migrations/versions/README.md                      |   4 -
 migrations/versions/a22c286d8094_.py               |  23 ---
 migrations/versions/b2d75ba212ed_.py               |  23 ---
 migrations/versions/b63a028c3346_.py               |  29 ----
 migrations/versions/b8243269e9cf_.py               |  47 -------
 migrations/versions/bbbcd54c69ac_.py               |  85 -----------
 .../versions/c856bd600df0_initial_migration.py     | 144 +++++++++++++++++++
 migrations/versions/e37c745e3097_.py               |  34 -----
 migrations/versions/e6fa15874247_.py               |  82 -----------
 migrations/versions/ef03b3a98056_.py               | 156 ---------------------
 migrations/versions/f32bd8265c3b_.py               |  33 -----
 22 files changed, 183 insertions(+), 847 deletions(-)
---
diff --git a/migrations/README b/migrations/README
new file mode 100644
index 0000000..0e04844
--- /dev/null
+++ b/migrations/README
@@ -0,0 +1 @@
+Single-database configuration for Flask.
diff --git a/migrations/alembic.ini b/migrations/alembic.ini
index f8ed480..ec9d45c 100644
--- a/migrations/alembic.ini
+++ b/migrations/alembic.ini
@@ -11,7 +11,7 @@
 
 # Logging configuration
 [loggers]
-keys = root,sqlalchemy,alembic
+keys = root,sqlalchemy,alembic,flask_migrate
 
 [handlers]
 keys = console
@@ -34,6 +34,11 @@ level = INFO
 handlers =
 qualname = alembic
 
+[logger_flask_migrate]
+level = INFO
+handlers =
+qualname = flask_migrate
+
 [handler_console]
 class = StreamHandler
 args = (sys.stderr,)
diff --git a/migrations/env.py b/migrations/env.py
index 148c7ff..68feded 100644
--- a/migrations/env.py
+++ b/migrations/env.py
@@ -1,8 +1,11 @@
 from __future__ import with_statement
-from alembic import context
-from sqlalchemy import engine_from_config, pool
-from logging.config import fileConfig
+
 import logging
+from logging.config import fileConfig
+
+from flask import current_app
+
+from alembic import context
 
 # this is the Alembic Config object, which provides
 # access to the values within the .ini file in use.
@@ -11,27 +14,23 @@ config = context.config
 # Interpret the config file for Python logging.
 # This line sets up loggers basically.
 fileConfig(config.config_file_name)
-logger = logging.getLogger("alembic.env")
+logger = logging.getLogger('alembic.env')
 
 # add your model's MetaData object here
 # for 'autogenerate' support
 # from myapp import mymodel
 # target_metadata = mymodel.Base.metadata
-from flask import current_app
-
 config.set_main_option(
-    "sqlalchemy.url", current_app.config.get("SQLALCHEMY_DATABASE_URI")
-)
-target_metadata = current_app.extensions["migrate"].db.metadata
+    'sqlalchemy.url',
+    str(current_app.extensions['migrate'].db.get_engine().url).replace(
+        '%', '%%'))
+target_metadata = current_app.extensions['migrate'].db.metadata
 
 # 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.
 
-# set to True to detect type changes in a better way (with false positives)
-COMPARE_TYPES = False
-
 
 def run_migrations_offline():
     """Run migrations in 'offline' mode.
@@ -46,7 +45,9 @@ def run_migrations_offline():
 
     """
     url = config.get_main_option("sqlalchemy.url")
-    context.configure(compare_type=COMPARE_TYPES, url=url)
+    context.configure(
+        url=url, target_metadata=target_metadata, literal_binds=True
+    )
 
     with context.begin_transaction():
         context.run_migrations()
@@ -62,34 +63,26 @@ def run_migrations_online():
 
     # this callback is used to prevent an auto-migration from being generated
     # when there are no changes to the schema
-    # reference: http://alembic.readthedocs.org/en/latest/cookbook.html
+    # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
     def process_revision_directives(context, revision, directives):
-        if getattr(config.cmd_opts, "autogenerate", False):
+        if getattr(config.cmd_opts, 'autogenerate', False):
             script = directives[0]
             if script.upgrade_ops.is_empty():
                 directives[:] = []
-                logger.info("No changes in schema detected.")
+                logger.info('No changes in schema detected.')
 
-    engine = engine_from_config(
-        config.get_section(config.config_ini_section),
-        prefix="sqlalchemy.",
-        poolclass=pool.NullPool,
-    )
+    connectable = current_app.extensions['migrate'].db.get_engine()
 
-    connection = engine.connect()
-    context.configure(
-        connection=connection,
-        compare_type=COMPARE_TYPES,
-        target_metadata=target_metadata,
-        process_revision_directives=process_revision_directives,
-        **current_app.extensions["migrate"].configure_args
-    )
+    with connectable.connect() as connection:
+        context.configure(
+            connection=connection,
+            target_metadata=target_metadata,
+            process_revision_directives=process_revision_directives,
+            **current_app.extensions['migrate'].configure_args
+        )
 
-    try:
         with context.begin_transaction():
             context.run_migrations()
-    finally:
-        connection.close()
 
 
 if context.is_offline_mode():
diff --git a/migrations/script.py.mako b/migrations/script.py.mako
index 89dc3eb..2c01563 100644
--- a/migrations/script.py.mako
+++ b/migrations/script.py.mako
@@ -1,18 +1,20 @@
-"""
+"""${message}
 
 Revision ID: ${up_revision}
-Revises: ${down_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)}
 
-from alembic import op
-import sqlalchemy as sa
-${imports if imports else ""}
 
 def upgrade():
     ${upgrades if upgrades else "pass"}
diff --git a/migrations/versions/c856bd600df0_initial_migration.py 
b/migrations/versions/c856bd600df0_initial_migration.py
new file mode 100644
index 0000000..462ef44
--- /dev/null
+++ b/migrations/versions/c856bd600df0_initial_migration.py
@@ -0,0 +1,144 @@
+"""Initial migration.
+
+Revision ID: c856bd600df0
+Revises: 
+Create Date: 2022-04-25 09:02:57.555232
+
+"""
+from alembic import op
+import sqlalchemy as sa
+
+
+# revision identifiers, used by Alembic.
+revision = 'c856bd600df0'
+down_revision = None
+branch_labels = None
+depends_on = None
+
+
+def upgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.create_table('analytics',
+    sa.Column('datestr', sa.Integer(), nullable=False),
+    sa.Column('app_id', sa.String(length=128), nullable=False),
+    sa.Column('fetch_cnt', sa.Integer(), nullable=True),
+    sa.PrimaryKeyConstraint('datestr', 'app_id'),
+    mysql_character_set='utf8mb4'
+    )
+    op.create_index('datestr', 'analytics', ['datestr', 'app_id'], unique=True)
+    op.create_table('components',
+    sa.Column('component_id', sa.Integer(), nullable=False),
+    sa.Column('component_id_parent', sa.Integer(), nullable=True),
+    sa.Column('app_id', sa.Text(), nullable=True),
+    sa.Column('fetch_cnt', sa.Integer(), nullable=True),
+    sa.Column('review_cnt', sa.Integer(), nullable=True),
+    sa.ForeignKeyConstraint(['component_id_parent'], ['components.component_id'], ),
+    sa.PrimaryKeyConstraint('component_id'),
+    sa.UniqueConstraint('component_id'),
+    mysql_character_set='utf8mb4'
+    )
+    op.create_table('taboos',
+    sa.Column('taboo_id', sa.Integer(), nullable=False),
+    sa.Column('locale', sa.String(length=8), nullable=False),
+    sa.Column('value', sa.Text(), nullable=False),
+    sa.Column('description', sa.Text(), nullable=True),
+    sa.Column('severity', sa.Integer(), nullable=True),
+    sa.PrimaryKeyConstraint('taboo_id'),
+    sa.UniqueConstraint('taboo_id'),
+    mysql_character_set='utf8mb4'
+    )
+    op.create_index(op.f('ix_taboos_locale'), 'taboos', ['locale'], unique=False)
+    op.create_table('users',
+    sa.Column('user_id', sa.Integer(), nullable=False),
+    sa.Column('date_created', sa.DateTime(), nullable=False),
+    sa.Column('user_hash', sa.String(length=40), nullable=True),
+    sa.Column('karma', sa.Integer(), nullable=True),
+    sa.Column('is_banned', sa.Boolean(), nullable=True),
+    sa.PrimaryKeyConstraint('user_id'),
+    sa.UniqueConstraint('user_id'),
+    mysql_character_set='utf8mb4'
+    )
+    op.create_index('users_hash_idx', 'users', ['user_hash'], unique=False)
+    op.create_table('eventlog',
+    sa.Column('eventlog_id', sa.Integer(), nullable=False),
+    sa.Column('date_created', sa.DateTime(), nullable=False),
+    sa.Column('user_addr', sa.Text(), nullable=True),
+    sa.Column('user_id', sa.Integer(), nullable=True),
+    sa.Column('message', sa.Text(), nullable=True),
+    sa.Column('app_id', sa.Text(), nullable=True),
+    sa.Column('important', sa.Boolean(), nullable=True),
+    sa.ForeignKeyConstraint(['user_id'], ['users.user_id'], ),
+    sa.PrimaryKeyConstraint('eventlog_id'),
+    sa.UniqueConstraint('eventlog_id'),
+    mysql_character_set='utf8mb4'
+    )
+    op.create_index('date_created_idx', 'eventlog', ['date_created'], unique=False)
+    op.create_index('message_idx', 'eventlog', ['message'], unique=False, mysql_length=8)
+    op.create_table('moderators',
+    sa.Column('moderator_id', sa.Integer(), nullable=False),
+    sa.Column('username', sa.Text(), nullable=True),
+    sa.Column('password', sa.Text(), nullable=True),
+    sa.Column('display_name', sa.Text(), nullable=True),
+    sa.Column('is_enabled', sa.Boolean(), nullable=True),
+    sa.Column('is_admin', sa.Boolean(), nullable=True),
+    sa.Column('user_id', sa.Integer(), nullable=True),
+    sa.Column('locales', sa.Text(), nullable=True),
+    sa.ForeignKeyConstraint(['user_id'], ['users.user_id'], ),
+    sa.PrimaryKeyConstraint('moderator_id'),
+    sa.UniqueConstraint('moderator_id'),
+    mysql_character_set='utf8mb4'
+    )
+    op.create_table('reviews',
+    sa.Column('review_id', sa.Integer(), nullable=False),
+    sa.Column('date_created', sa.DateTime(), nullable=False),
+    sa.Column('date_deleted', sa.DateTime(), nullable=True),
+    sa.Column('component_id', sa.Integer(), nullable=False),
+    sa.Column('locale', sa.Text(), nullable=True),
+    sa.Column('summary', sa.Text(), nullable=True),
+    sa.Column('description', sa.Text(), nullable=True),
+    sa.Column('user_id', sa.Integer(), nullable=True),
+    sa.Column('user_addr', sa.Text(), nullable=True),
+    sa.Column('user_display', sa.Text(), nullable=True),
+    sa.Column('version', sa.Text(), nullable=True),
+    sa.Column('distro', sa.Text(), nullable=True),
+    sa.Column('rating', sa.Integer(), nullable=True),
+    sa.Column('karma_up', sa.Integer(), nullable=True),
+    sa.Column('karma_down', sa.Integer(), nullable=True),
+    sa.Column('reported', sa.Integer(), nullable=True),
+    sa.ForeignKeyConstraint(['component_id'], ['components.component_id'], ),
+    sa.ForeignKeyConstraint(['user_id'], ['users.user_id'], ),
+    sa.PrimaryKeyConstraint('review_id'),
+    sa.UniqueConstraint('review_id'),
+    mysql_character_set='utf8mb4'
+    )
+    op.create_table('votes',
+    sa.Column('vote_id', sa.Integer(), nullable=False),
+    sa.Column('date_created', sa.DateTime(), nullable=False),
+    sa.Column('review_id', sa.Integer(), nullable=True),
+    sa.Column('user_id', sa.Integer(), nullable=True),
+    sa.Column('val', sa.Integer(), nullable=True),
+    sa.ForeignKeyConstraint(['review_id'], ['reviews.review_id'], ),
+    sa.ForeignKeyConstraint(['user_id'], ['users.user_id'], ),
+    sa.PrimaryKeyConstraint('vote_id'),
+    sa.UniqueConstraint('vote_id'),
+    mysql_character_set='utf8mb4'
+    )
+    # ### end Alembic commands ###
+
+
+def downgrade():
+    # ### commands auto generated by Alembic - please adjust! ###
+    op.drop_table('votes')
+    op.drop_table('reviews')
+    op.drop_table('moderators')
+    op.drop_index('message_idx', table_name='eventlog', mysql_length=8)
+    op.drop_index('date_created_idx', table_name='eventlog')
+    op.drop_table('eventlog')
+    op.drop_index('users_hash_idx', table_name='users')
+    op.drop_table('users')
+    op.drop_index(op.f('ix_taboos_locale'), table_name='taboos')
+    op.drop_table('taboos')
+    op.drop_table('components')
+    op.drop_index('datestr', table_name='analytics')
+    op.drop_table('analytics')
+    # ### end Alembic commands ###


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]