Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-backend / pithos / backends / lib / sqlalchemy / alembic / env.py @ 44d80edf

History | View | Annotate | Download (2.1 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
from synnefo.settings import PITHOS_BACKEND_DB_CONNECTION
6

    
7
# this is the Alembic Config object, which provides
8
# access to the values within the .ini file in use.
9
config = context.config
10

    
11
# Interpret the config file for Python logging.
12
# This line sets up loggers basically.
13
fileConfig(config.config_file_name)
14

    
15
# add your model's MetaData object here
16
# for 'autogenerate' support
17
# from myapp import mymodel
18
# target_metadata = mymodel.Base.metadata
19
target_metadata = None
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
db = config.get_main_option("sqlalchemy.url", PITHOS_BACKEND_DB_CONNECTION)
27
config.set_main_option("sqlalchemy.url", db)
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(url=url)
43

    
44
    with context.begin_transaction():
45
        context.run_migrations()
46

    
47
def run_migrations_online():
48
    """Run migrations in 'online' mode.
49

50
    In this scenario we need to create an Engine
51
    and associate a connection with the context.
52

53
    """
54
    engine = engine_from_config(
55
                config.get_section(config.config_ini_section),
56
                prefix='sqlalchemy.',
57
                poolclass=pool.NullPool)
58

    
59
    connection = engine.connect()
60
    context.configure(
61
                connection=connection,
62
                target_metadata=target_metadata
63
                )
64

    
65
    try:
66
        with context.begin_transaction():
67
            context.run_migrations()
68
    finally:
69
        connection.close()
70

    
71
if context.is_offline_mode():
72
    run_migrations_offline()
73
else:
74
    run_migrations_online()
75