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