SuperAGI

Форк
0
116 строк · 3.4 Кб
1
from logging.config import fileConfig
2

3
from sqlalchemy import engine_from_config
4
from sqlalchemy import pool
5
from alembic import context
6
from urllib.parse import urlparse
7

8
# this is the Alembic Config object, which provides
9
# access to the values within the .ini file in use.
10
config = context.config
11

12
# Interpret the config file for Python logging.
13
# This line sets up loggers basically.
14
if config.config_file_name is not None:
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
from superagi.models.base_model import DBBaseModel
22
target_metadata = DBBaseModel.metadata
23
from superagi.models import *
24
from superagi.config.config import get_config
25

26
# other values from the config, defined by the needs of env.py,
27
# can be acquired:
28
# my_important_option = config.get_main_option("my_important_option")
29
# ... etc.
30

31
db_host = get_config('DB_HOST', 'super__postgres')
32
db_username = get_config('DB_USERNAME')
33
db_password = get_config('DB_PASSWORD')
34
db_name = get_config('DB_NAME')
35
database_url = get_config('DB_URL', None)
36

37
def run_migrations_offline() -> None:
38
    """Run migrations in 'offline' mode.
39

40
    This configures the context with just a URL
41
    and not an Engine, though an Engine is acceptable
42
    here as well.  By skipping the Engine creation
43
    we don't even need a DBAPI to be available.
44

45
    Calls to context.execute() here emit the given string to the
46
    script output.
47

48
    """
49

50
    db_url = database_url
51
    if db_url is None:
52
        if db_username is None:
53
            db_url = f'postgresql://{db_host}/{db_name}'
54
        else:
55
            db_url = f'postgresql://{db_username}:{db_password}@{db_host}/{db_name}'
56
    else:
57
        db_url = urlparse(db_url)
58
        db_url = db_url.scheme + "://" + db_url.netloc + db_url.path
59

60
    config.set_main_option("sqlalchemy.url", db_url)
61

62
    url = config.get_main_option("sqlalchemy.url")
63
    context.configure(
64
        url=url,
65
        target_metadata=target_metadata,
66
        literal_binds=True,
67
        dialect_opts={"paramstyle": "named"},
68
    )
69

70
    with context.begin_transaction():
71
        context.run_migrations()
72

73

74
def run_migrations_online() -> None:
75
    """Run migrations in 'online' mode.
76

77
    In this scenario we need to create an Engine
78
    and associate a connection with the context.
79

80
    """
81

82
    db_host = get_config('DB_HOST', 'super__postgres')
83
    db_username = get_config('DB_USERNAME')
84
    db_password = get_config('DB_PASSWORD')
85
    db_name = get_config('DB_NAME')
86
    db_url = get_config('DB_URL', None)
87

88
    if db_url is None:
89
        if db_username is None:
90
            db_url = f'postgresql://{db_host}/{db_name}'
91
        else:
92
            db_url = f'postgresql://{db_username}:{db_password}@{db_host}/{db_name}'
93
    else:
94
        db_url = urlparse(db_url)
95
        db_url = db_url.scheme + "://" + db_url.netloc + db_url.path
96
        
97
    config.set_main_option('sqlalchemy.url', db_url)
98
    connectable = engine_from_config(
99
        config.get_section(config.config_ini_section, {}),
100
        prefix="sqlalchemy.",
101
        poolclass=pool.NullPool,
102
    )
103

104
    with connectable.connect() as connection:
105
        context.configure(
106
            connection=connection, target_metadata=target_metadata
107
        )
108

109
        with context.begin_transaction():
110
            context.run_migrations()
111

112

113
if context.is_offline_mode():
114
    run_migrations_offline()
115
else:
116
    run_migrations_online()
117

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

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

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

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