diff options
Diffstat (limited to 'db/migrations/env.py')
| -rw-r--r-- | db/migrations/env.py | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/db/migrations/env.py b/db/migrations/env.py new file mode 100644 index 0000000..f1b3156 --- /dev/null +++ b/db/migrations/env.py | |||
| @@ -0,0 +1,82 @@ | |||
| 1 | from logging.config import fileConfig | ||
| 2 | |||
| 3 | from sqlalchemy import engine_from_config | ||
| 4 | from sqlalchemy import pool | ||
| 5 | |||
| 6 | from alembic import context | ||
| 7 | |||
| 8 | from conf import DATABASE_URL | ||
| 9 | from db.models import * | ||
| 10 | |||
| 11 | # this is the Alembic Config object, which provides | ||
| 12 | # access to the values within the .ini file in use. | ||
| 13 | config = context.config | ||
| 14 | |||
| 15 | # Interpret the config file for Python logging. | ||
| 16 | # This line sets up loggers basically. | ||
| 17 | if config.config_file_name is not None: | ||
| 18 | fileConfig(config.config_file_name) | ||
| 19 | |||
| 20 | # add your model's MetaData object here | ||
| 21 | # for 'autogenerate' support | ||
| 22 | # from myapp import mymodel | ||
| 23 | # target_metadata = mymodel.Base.metadata | ||
| 24 | target_metadata = SQLModel.metadata | ||
| 25 | |||
| 26 | # other values from the config, defined by the needs of env.py, | ||
| 27 | # can be acquired: | ||
| 28 | # my_important_option = config.get_main_option("my_important_option") | ||
| 29 | # ... etc. | ||
| 30 | config.set_main_option('sqlalchemy.url', DATABASE_URL) | ||
| 31 | |||
| 32 | |||
| 33 | def run_migrations_offline() -> None: | ||
| 34 | """Run migrations in 'offline' mode. | ||
| 35 | |||
| 36 | This configures the context with just a URL | ||
| 37 | and not an Engine, though an Engine is acceptable | ||
| 38 | here as well. By skipping the Engine creation | ||
| 39 | we don't even need a DBAPI to be available. | ||
| 40 | |||
| 41 | Calls to context.execute() here emit the given string to the | ||
| 42 | script output. | ||
| 43 | |||
| 44 | """ | ||
| 45 | url = config.get_main_option("sqlalchemy.url") | ||
| 46 | context.configure( | ||
| 47 | url=url, | ||
| 48 | target_metadata=target_metadata, | ||
| 49 | literal_binds=True, | ||
| 50 | dialect_opts={"paramstyle": "named"}, | ||
| 51 | ) | ||
| 52 | |||
| 53 | with context.begin_transaction(): | ||
| 54 | context.run_migrations() | ||
| 55 | |||
| 56 | |||
| 57 | def run_migrations_online() -> None: | ||
| 58 | """Run migrations in 'online' mode. | ||
| 59 | |||
| 60 | In this scenario we need to create an Engine | ||
| 61 | and associate a connection with the context. | ||
| 62 | |||
| 63 | """ | ||
| 64 | connectable = engine_from_config( | ||
| 65 | config.get_section(config.config_ini_section, {}), | ||
| 66 | prefix="sqlalchemy.", | ||
| 67 | poolclass=pool.NullPool, | ||
| 68 | ) | ||
| 69 | |||
| 70 | with connectable.connect() as connection: | ||
| 71 | context.configure( | ||
| 72 | connection=connection, target_metadata=target_metadata | ||
| 73 | ) | ||
| 74 | |||
| 75 | with context.begin_transaction(): | ||
| 76 | context.run_migrations() | ||
| 77 | |||
| 78 | |||
| 79 | if context.is_offline_mode(): | ||
| 80 | run_migrations_offline() | ||
| 81 | else: | ||
| 82 | run_migrations_online() | ||
