lavkach3
97 строк · 2.9 Кб
1import asyncio2import os3import sys4from logging.config import fileConfig5
6from alembic import context7from sqlalchemy import pool8from sqlalchemy.ext.asyncio import create_async_engine9
10parent_dir = os.path.abspath(os.path.join(os.getcwd(), ".."))11sys.path.append(parent_dir)12
13# this is the Alembic Config object, which provides
14# access to the values within the .ini file in use.
15config = context.config16fileConfig(config.config_file_name)17sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))18
19
20# Interpret the config file for Python logging.
21# This line sets up loggers basically.
22fileConfig(config.config_file_name)23
24# add your model's MetaData object here
25# for 'autogenerate' support
26# from myapp import mymodel
27# target_metadata = mymodel.Base.metadata
28
29# For auto generate schemas
30
31from core.db_config import config32from app.inventory.quant.models import *33from core.core_apps.base.company.models import *34from app.basic.product.models import *35from app.basic.store.models import *36from core.core_apps.base.user.models import *37from app.basic.partner.models import *38from app.basic.uom.models import *39from app.inventory.location.models import *40from app.inventory.order.models import *41from app.inventory.product_storage.models import *42from core.core_apps.bus.bus.models.bus_models import *43
44target_metadata = Base.metadata45
46# other values from the config, defined by the needs of env.py,
47# can be acquired:
48# my_important_option = config.get_main_option("my_important_option")
49# ... etc.
50
51
52def run_migrations_offline():53"""Run migrations in 'offline' mode.54This configures the context with just a URL
55and not an Engine, though an Engine is acceptable
56here as well. By skipping the Engine creation
57we don't even need a DBAPI to be available.
58Calls to context.execute() here emit the given string to the
59script output.
60"""
61url = config.get_main_option("sqlalchemy.url")62print(config.WRITER_DB_URL)63context.configure(64url=config.WRITER_DB_URL,65target_metadata=target_metadata,66literal_binds=True,67dialect_opts={"paramstyle": "named"},68)69
70with context.begin_transaction():71context.run_migrations()72
73
74def do_run_migrations(connection):75context.configure(connection=connection, target_metadata=target_metadata)76
77with context.begin_transaction():78context.run_migrations()79
80
81async def run_migrations_online():82"""Run migrations in 'online' mode.83In this scenario we need to create an Engine
84and associate a connection with the context.
85"""
86connectable = create_async_engine(config.WRITER_DB_URL, poolclass=pool.NullPool, echo=True)87
88async with connectable.connect() as connection:89await connection.run_sync(do_run_migrations)90
91await connectable.dispose()92
93
94if context.is_offline_mode():95run_migrations_offline()96else:97asyncio.run(run_migrations_online())98