FastApi
92 строки · 2.6 Кб
1import os2import sys3from logging.config import fileConfig4
5from alembic import context6from sqlalchemy import engine_from_config, pool7
8sys.path.append(os.path.join(sys.path[0], 'src'))9
10from src.auth.models import *11from src.config import DB_HOST, DB_NAME, DB_PASS, DB_PORT, DB_USER12from src.database import metadata13from src.operations.models import *14
15# this is the Alembic Config object, which provides
16# access to the values within the .ini file in use.
17config = context.config18
19section = config.config_ini_section20config.set_section_option(section, "DB_HOST", DB_HOST)21config.set_section_option(section, "DB_PORT", DB_PORT)22config.set_section_option(section, "DB_USER", DB_USER)23config.set_section_option(section, "DB_NAME", DB_NAME)24config.set_section_option(section, "DB_PASS", DB_PASS)25
26# Interpret the config file for Python logging.
27# This line sets up loggers basically.
28if config.config_file_name is not None:29fileConfig(config.config_file_name)30
31# add your model's MetaData object here
32# for 'autogenerate' support
33# from myapp import mymodel
34# target_metadata = mymodel.Base.metadata
35target_metadata = metadata36
37# other values from the config, defined by the needs of env.py,
38# can be acquired:
39# my_important_option = config.get_main_option("my_important_option")
40# ... etc.
41
42
43def run_migrations_offline() -> None:44"""Run migrations in 'offline' mode.45
46This configures the context with just a URL
47and not an Engine, though an Engine is acceptable
48here as well. By skipping the Engine creation
49we don't even need a DBAPI to be available.
50
51Calls to context.execute() here emit the given string to the
52script output.
53
54"""
55url = config.get_main_option("sqlalchemy.url")56context.configure(57url=url,58target_metadata=target_metadata,59literal_binds=True,60dialect_opts={"paramstyle": "named"},61)62
63with context.begin_transaction():64context.run_migrations()65
66
67def run_migrations_online() -> None:68"""Run migrations in 'online' mode.69
70In this scenario we need to create an Engine
71and associate a connection with the context.
72
73"""
74connectable = engine_from_config(75config.get_section(config.config_ini_section),76prefix="sqlalchemy.",77poolclass=pool.NullPool,78)79
80with connectable.connect() as connection:81context.configure(82connection=connection, target_metadata=target_metadata83)84
85with context.begin_transaction():86context.run_migrations()87
88
89if context.is_offline_mode():90run_migrations_offline()91else:92run_migrations_online()93