| 1 |
# Copyright (C) 2013 Yahoo! Inc. All Rights Reserved. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
from alembic import context |
| 15 |
from sqlalchemy import engine_from_config, pool |
| 16 |
|
| 17 |
# this is the Alembic Config object, which provides |
| 18 |
# access to the values within the .ini file in use. |
| 19 |
config = context.config |
| 20 |
|
| 21 |
# add your model's MetaData object here |
| 22 |
# for 'autogenerate' support |
| 23 |
# from myapp import mymodel |
| 24 |
# target_metadata = mymodel.Base.metadata |
| 25 |
target_metadata = None |
| 26 |
|
| 27 |
# other values from the config, defined by the needs of env.py, |
| 28 |
# can be acquired: |
| 29 |
# my_important_option = config.get_main_option("my_important_option") |
| 30 |
# ... etc. |
| 31 |
|
| 32 |
|
| 33 |
def run_migrations_offline(): |
| 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(url=url) |
| 47 |
|
| 48 |
with context.begin_transaction(): |
| 49 |
context.run_migrations() |
| 50 |
|
| 51 |
|
| 52 |
def run_migrations_online(): |
| 53 |
"""Run migrations in 'online' mode. |
| 54 |
|
| 55 |
In this scenario we need to create an Engine |
| 56 |
and associate a connection with the context. |
| 57 |
|
| 58 |
""" |
| 59 |
connectable = config.attributes.get('connection', None) |
| 60 |
if connectable is None: |
| 61 |
connectable = engine_from_config( |
| 62 |
config.get_section(config.config_ini_section), |
| 63 |
prefix='sqlalchemy.', poolclass=pool.NullPool) |
| 64 |
with connectable.connect() as connection: |
| 65 |
context.configure(connection=connection, |
| 66 |
target_metadata=target_metadata) |
| 67 |
with context.begin_transaction(): |
| 68 |
context.run_migrations() |
| 69 |
else: |
| 70 |
context.configure( |
| 71 |
connection=connectable, |
| 72 |
target_metadata=target_metadata) |
| 73 |
with context.begin_transaction(): |
| 74 |
context.run_migrations() |
| 75 |
|
| 76 |
if context.is_offline_mode(): |
| 77 |
run_migrations_offline() |
| 78 |
else: |
| 79 |
run_migrations_online() |