root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / alembic / env.py @ d50ed8d4
History | View | Annotate | Download (2.3 kB)
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 |
try:
|
7 |
# pithos-app case
|
8 |
from synnefo.settings import PITHOS_BACKEND_DB_CONNECTION |
9 |
except ImportError: |
10 |
try:
|
11 |
# plankton case
|
12 |
from synnefo.settings import BACKEND_DB_CONNECTION as \ |
13 |
PITHOS_BACKEND_DB_CONNECTION |
14 |
except ImportError: |
15 |
PITHOS_BACKEND_DB_CONNECTION = None
|
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 |
# Interpret the config file for Python logging.
|
22 |
# This line sets up loggers basically.
|
23 |
fileConfig(config.config_file_name) |
24 |
|
25 |
# add your model's MetaData object here
|
26 |
# for 'autogenerate' support
|
27 |
# from myapp import mymodel
|
28 |
# target_metadata = mymodel.Base.metadata
|
29 |
target_metadata = None
|
30 |
|
31 |
# other values from the config, defined by the needs of env.py,
|
32 |
# can be acquired:
|
33 |
# my_important_option = config.get_main_option("my_important_option")
|
34 |
# ... etc.
|
35 |
|
36 |
db = config.get_main_option("sqlalchemy.url", PITHOS_BACKEND_DB_CONNECTION)
|
37 |
config.set_main_option("sqlalchemy.url", db)
|
38 |
|
39 |
|
40 |
def run_migrations_offline(): |
41 |
"""Run migrations in 'offline' mode.
|
42 |
|
43 |
This configures the context with just a URL
|
44 |
and not an Engine, though an Engine is acceptable
|
45 |
here as well. By skipping the Engine creation
|
46 |
we don't even need a DBAPI to be available.
|
47 |
|
48 |
Calls to context.execute() here emit the given string to the
|
49 |
script output.
|
50 |
|
51 |
"""
|
52 |
url = config.get_main_option("sqlalchemy.url")
|
53 |
context.configure(url=url) |
54 |
|
55 |
with context.begin_transaction():
|
56 |
context.run_migrations() |
57 |
|
58 |
|
59 |
def run_migrations_online(): |
60 |
"""Run migrations in 'online' mode.
|
61 |
|
62 |
In this scenario we need to create an Engine
|
63 |
and associate a connection with the context.
|
64 |
|
65 |
"""
|
66 |
engine = engine_from_config( |
67 |
config.get_section(config.config_ini_section), |
68 |
prefix='sqlalchemy.',
|
69 |
poolclass=pool.NullPool) |
70 |
|
71 |
connection = engine.connect() |
72 |
context.configure( |
73 |
connection=connection, |
74 |
target_metadata=target_metadata |
75 |
) |
76 |
|
77 |
try:
|
78 |
with context.begin_transaction():
|
79 |
context.run_migrations() |
80 |
finally:
|
81 |
connection.close() |
82 |
|
83 |
if context.is_offline_mode():
|
84 |
run_migrations_offline() |
85 |
else:
|
86 |
run_migrations_online() |