~sirn/fanboi2

ref: 0e52a2d9448755b13e9989cf85879c52b7bc7b10 fanboi2/migration/env.py -rw-r--r-- 1.3 KiB View raw
0e52a2d9Kridsada Thanabulpong [build] fix broken dependencies version lock 10 months ago
                                                                                        
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import logging
from alembic import context
from sqlalchemy import engine_from_config, pool

from fanboi2 import setup_logger, settings_from_env
from fanboi2.models import Base

settings = settings_from_env()


# Logger

setup_logger(settings)

log_level = logging.WARN
if settings['server.development']:
    log_level = logging.INFO

logger = logging.getLogger('sqlalchemy.engine.base.Engine')
logger.setLevel(log_level)


# SQLAlchemy

target_metadata = Base.metadata
config = context.config
config.set_main_option('sqlalchemy.url', settings['sqlalchemy.url'])


def run_migrations_offline():
    """Run migrations in offline mode."""
    url = config.get_main_option('sqlalchemy.url')
    context.configure(url=url)

    with context.begin_transaction():
        context.run_migrations()


def run_migrations_online():
    """Run migrations in online mode."""
    engine = engine_from_config(
        config.get_section(config.config_ini_section),
        prefix='sqlalchemy.',
        poolclass=pool.NullPool)

    connection = engine.connect()
    context.configure(connection=connection, target_metadata=target_metadata)

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()


if context.is_offline_mode():
    run_migrations_offline()
else:
    run_migrations_online()