Mercurial > libervia-backend
comparison sat/memory/migration/env.py @ 3715:b9718216a1c0 0.9
merge bookmark 0.9
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 01 Dec 2021 16:13:31 +0100 |
parents | 71516731d0aa |
children | 54c249ec35ce |
comparison
equal
deleted
inserted
replaced
3714:af09b5aaa5d7 | 3715:b9718216a1c0 |
---|---|
1 import asyncio | |
2 from logging.config import fileConfig | |
3 from sqlalchemy import pool | |
4 from sqlalchemy.ext.asyncio import create_async_engine | |
5 from alembic import context | |
6 from sat.memory import sqla_config | |
7 from sat.memory.sqla_mapping import Base | |
8 | |
9 # this is the Alembic Config object, which provides | |
10 # access to the values within the .ini file in use. | |
11 config = context.config | |
12 | |
13 # Interpret the config file for Python logging. | |
14 # This line sets up loggers basically. | |
15 fileConfig(config.config_file_name) | |
16 | |
17 # add your model's MetaData object here | |
18 # for 'autogenerate' support | |
19 # from myapp import mymodel | |
20 # target_metadata = mymodel.Base.metadata | |
21 target_metadata = Base.metadata | |
22 | |
23 # other values from the config, defined by the needs of env.py, | |
24 # can be acquired: | |
25 # my_important_option = config.get_main_option("my_important_option") | |
26 # ... etc. | |
27 | |
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 db_config = sqla_config.getDbConfig() | |
42 context.configure( | |
43 url=db_config["url"], | |
44 target_metadata=target_metadata, | |
45 literal_binds=True, | |
46 dialect_opts={"paramstyle": "named"}, | |
47 ) | |
48 | |
49 with context.begin_transaction(): | |
50 context.run_migrations() | |
51 | |
52 | |
53 def do_run_migrations(connection): | |
54 context.configure( | |
55 connection=connection, | |
56 target_metadata=target_metadata, | |
57 render_as_batch=True | |
58 ) | |
59 | |
60 with context.begin_transaction(): | |
61 context.run_migrations() | |
62 | |
63 | |
64 async def run_migrations_online(): | |
65 """Run migrations in 'online' mode. | |
66 | |
67 In this scenario we need to create an Engine | |
68 and associate a connection with the context. | |
69 | |
70 """ | |
71 db_config = sqla_config.getDbConfig() | |
72 engine = create_async_engine( | |
73 db_config["url"], | |
74 poolclass=pool.NullPool, | |
75 future=True, | |
76 ) | |
77 | |
78 async with engine.connect() as connection: | |
79 await connection.run_sync(do_run_migrations) | |
80 | |
81 | |
82 if context.is_offline_mode(): | |
83 run_migrations_offline() | |
84 else: | |
85 asyncio.run(run_migrations_online()) |