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