Workaround to identify proper alchemy url
[pithos] / snf-pithos-backend / pithos / backends / lib / sqlalchemy / alembic / env.py
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 def run_migrations_offline():
40     """Run migrations in 'offline' mode.
41
42     This configures the context with just a URL
43     and not an Engine, though an Engine is acceptable
44     here as well.  By skipping the Engine creation
45     we don't even need a DBAPI to be available.
46
47     Calls to context.execute() here emit the given string to the
48     script output.
49
50     """
51     url = config.get_main_option("sqlalchemy.url")
52     context.configure(url=url)
53
54     with context.begin_transaction():
55         context.run_migrations()
56
57 def run_migrations_online():
58     """Run migrations in 'online' mode.
59
60     In this scenario we need to create an Engine
61     and associate a connection with the context.
62
63     """
64     engine = engine_from_config(
65                 config.get_section(config.config_ini_section),
66                 prefix='sqlalchemy.',
67                 poolclass=pool.NullPool)
68
69     connection = engine.connect()
70     context.configure(
71                 connection=connection,
72                 target_metadata=target_metadata
73                 )
74
75     try:
76         with context.begin_transaction():
77             context.run_migrations()
78     finally:
79         connection.close()
80
81 if context.is_offline_mode():
82     run_migrations_offline()
83 else:
84     run_migrations_online()
85