env   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 48
dl 0
loc 102
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A run_migrations_online() 0 28 4
A run_migrations_offline() 0 17 2
A load_app_ini() 0 8 2
1
import configparser
2
from logging.config import fileConfig
3
from os import path
4
5
from alembic import context
6
from skosprovider_sqlalchemy.models import Base as SkosBase
7
from sqlalchemy import engine_from_config
8
from sqlalchemy import pool
9
from sqlalchemy.schema import MetaData
10
11
from atramhasis.data.models import Base
12
13
# this is the Alembic Config object, which provides
14
# access to the values within the .ini file in use.
15
config = context.config
16
17
# Interpret the config file for Python logging.
18
# This line sets up loggers basically.
19
fileConfig(config.config_file_name)
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 = [SkosBase.metadata, Base.metadata]
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 load_app_ini(ini_file):
34
    """Load the settings for the application.ini file."""
35
    ini = configparser.ConfigParser()
36
    with open(ini_file) as f:
37
        ini.read_file(f)
38
    here = path.abspath(path.dirname(ini_file))
39
    ini.set("app:main", "here", here)
40
    return ini
41
42
43
app_ini = config.get_main_option("ini_location")
44
app_config = load_app_ini(app_ini)
45
sa_url = app_config.get("app:main", "sqlalchemy.url")
46
config.set_main_option("sqlalchemy.url", sa_url)
47
48
49
def run_migrations_offline():
50
    """Run migrations in 'offline' mode.
51
52
    This configures the context with just a URL
53
    and not an Engine, though an Engine is acceptable
54
    here as well.  By skipping the Engine creation
55
    we don't even need a DBAPI to be available.
56
57
    Calls to context.execute() here emit the given string to the
58
    script output.
59
60
    """
61
    url = config.get_main_option("sqlalchemy.url")
62
    context.configure(url=url, render_as_batch=True)
63
64
    with context.begin_transaction():
65
        context.run_migrations()
66
67
68
def run_migrations_online():
69
    """Run migrations in 'online' mode.
70
71
    In this scenario we need to create an Engine
72
    and associate a connection with the context.
73
74
    """
75
    engine = engine_from_config(
76
        config.get_section(config.config_ini_section),
77
        prefix="sqlalchemy.",
78
        poolclass=pool.NullPool,
79
    )
80
81
    metadata = MetaData()
82
    for md in target_metadata:
83
        for table in md.tables.values():
84
            table.tometadata(metadata)
85
86
    connection = engine.connect()
87
    context.configure(
88
        connection=connection, target_metadata=metadata, render_as_batch=True
89
    )
90
91
    try:
92
        with context.begin_transaction():
93
            context.run_migrations()
94
    finally:
95
        connection.close()
96
97
98
if context.is_offline_mode():
99
    run_migrations_offline()
100
else:
101
    run_migrations_online()
102