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