Mercurial > libervia-backend
comparison libervia/backend/memory/migration/env.py @ 4071:4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Jun 2023 11:49:51 +0200 |
parents | sat/memory/migration/env.py@524856bd7b19 |
children | 0d7bb4df2343 |
comparison
equal
deleted
inserted
replaced
4070:d10748475025 | 4071:4b842c1fb686 |
---|---|
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 libervia.backend.memory import sqla_config | |
7 from libervia.backend.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.get_db_config() | |
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 include_name(name, type_, parent_names): | |
54 if type_ == "table": | |
55 if name.startswith("pubsub_items_fts"): | |
56 return False | |
57 return True | |
58 | |
59 | |
60 def do_run_migrations(connection): | |
61 context.configure( | |
62 connection=connection, | |
63 target_metadata=target_metadata, | |
64 render_as_batch=True, | |
65 include_name=include_name | |
66 ) | |
67 | |
68 with context.begin_transaction(): | |
69 context.run_migrations() | |
70 | |
71 | |
72 async def run_migrations_online(): | |
73 """Run migrations in 'online' mode. | |
74 | |
75 In this scenario we need to create an Engine | |
76 and associate a connection with the context. | |
77 | |
78 """ | |
79 db_config = sqla_config.get_db_config() | |
80 engine = create_async_engine( | |
81 db_config["url"], | |
82 poolclass=pool.NullPool, | |
83 future=True, | |
84 ) | |
85 | |
86 async with engine.connect() as connection: | |
87 await connection.run_sync(do_run_migrations) | |
88 | |
89 | |
90 if context.is_offline_mode(): | |
91 run_migrations_offline() | |
92 else: | |
93 asyncio.run(run_migrations_online()) |