forked from quantifiedcode/quantifiedcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_env.py
More file actions
56 lines (42 loc) · 1.66 KB
/
base_env.py
File metadata and controls
56 lines (42 loc) · 1.66 KB
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
from quantifiedcode.settings import backend, settings
from alembic import context
config = context.config
def run_migrations_offline(plugin_name):
"""Run migrations in 'offline' mode.
This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.
Calls to context.execute() here emit the given string to the
script output.
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
connection=backend.connection,
version_table='{}_version_table'.format(plugin_name),
url=url, target_metadata=backend.metadata,
literal_binds=True)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online(plugin_name):
"""Run migrations in 'online' mode.
In this scenario we need to create an Engine
and associate a connection with the context.
"""
connectables_and_metadata = [(backend.engine, backend.metadata)]
for connectable, metadata in connectables_and_metadata:
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=metadata,
version_table='{}_version_table'.format(plugin_name),
)
with context.begin_transaction():
context.run_migrations()
def main(plugin_name):
settings.initialize(backend)
with backend.transaction():
if context.is_offline_mode():
run_migrations_offline(plugin_name)
else:
run_migrations_online(plugin_name)