FastApi

Форк
0
92 строки · 2.6 Кб
1
import os
2
import sys
3
from logging.config import fileConfig
4

5
from alembic import context
6
from sqlalchemy import engine_from_config, pool
7

8
sys.path.append(os.path.join(sys.path[0], 'src'))
9

10
from src.auth.models import *
11
from src.config import DB_HOST, DB_NAME, DB_PASS, DB_PORT, DB_USER
12
from src.database import metadata
13
from 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.
17
config = context.config
18

19
section = config.config_ini_section
20
config.set_section_option(section, "DB_HOST", DB_HOST)
21
config.set_section_option(section, "DB_PORT", DB_PORT)
22
config.set_section_option(section, "DB_USER", DB_USER)
23
config.set_section_option(section, "DB_NAME", DB_NAME)
24
config.set_section_option(section, "DB_PASS", DB_PASS)
25

26
# Interpret the config file for Python logging.
27
# This line sets up loggers basically.
28
if config.config_file_name is not None:
29
    fileConfig(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
35
target_metadata = metadata
36

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

43
def run_migrations_offline() -> None:
44
    """Run migrations in 'offline' mode.
45

46
    This configures the context with just a URL
47
    and not an Engine, though an Engine is acceptable
48
    here as well.  By skipping the Engine creation
49
    we don't even need a DBAPI to be available.
50

51
    Calls to context.execute() here emit the given string to the
52
    script output.
53

54
    """
55
    url = config.get_main_option("sqlalchemy.url")
56
    context.configure(
57
        url=url,
58
        target_metadata=target_metadata,
59
        literal_binds=True,
60
        dialect_opts={"paramstyle": "named"},
61
    )
62

63
    with context.begin_transaction():
64
        context.run_migrations()
65

66

67
def run_migrations_online() -> None:
68
    """Run migrations in 'online' mode.
69

70
    In this scenario we need to create an Engine
71
    and associate a connection with the context.
72

73
    """
74
    connectable = engine_from_config(
75
        config.get_section(config.config_ini_section),
76
        prefix="sqlalchemy.",
77
        poolclass=pool.NullPool,
78
    )
79

80
    with connectable.connect() as connection:
81
        context.configure(
82
            connection=connection, target_metadata=target_metadata
83
        )
84

85
        with context.begin_transaction():
86
            context.run_migrations()
87

88

89
if context.is_offline_mode():
90
    run_migrations_offline()
91
else:
92
    run_migrations_online()
93

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.