Редактировать env.py
This commit is contained in:
@@ -1,46 +1,109 @@
|
|||||||
from logging.config import fileConfig
|
# rag/alembic/env.py
|
||||||
from sqlalchemy import pool
|
# -*- coding: utf-8 -*-
|
||||||
from sqlalchemy.ext.asyncio import create_async_engine
|
"""
|
||||||
from alembic import context
|
Среда выполнения Alembic для управления схемой PostgreSQL.
|
||||||
import asyncio
|
Загружает конфигурацию из проекта (AppConfig) для получения строки подключения.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
from logging.config import fileConfig
|
||||||
|
|
||||||
|
from sqlalchemy import engine_from_config, pool
|
||||||
|
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine
|
||||||
|
from alembic import context
|
||||||
|
|
||||||
|
# Добавляем путь к проекту (чтобы импортировать модули)
|
||||||
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
|
||||||
|
|
||||||
|
from rag.config_models import AppConfig
|
||||||
|
from rag.utils.config_loader import load_config
|
||||||
|
|
||||||
|
# Это объект конфигурации Alembic, предоставляющий доступ к значениям из alembic.ini
|
||||||
config = context.config
|
config = context.config
|
||||||
|
|
||||||
|
# Настройка логирования (если указано в alembic.ini)
|
||||||
if config.config_file_name is not None:
|
if config.config_file_name is not None:
|
||||||
fileConfig(config.config_file_name)
|
fileConfig(config.config_file_name)
|
||||||
|
|
||||||
|
# Метаданные моделей (если используются SQLAlchemy ORM) – пока None
|
||||||
target_metadata = None
|
target_metadata = None
|
||||||
|
|
||||||
def run_migrations_offline():
|
|
||||||
url = config.get_main_option("sqlalchemy.url")
|
def get_app_config() -> AppConfig:
|
||||||
|
"""
|
||||||
|
Загружает конфигурацию проекта из переменной окружения или из стандартного пути.
|
||||||
|
"""
|
||||||
|
profile_dir = os.environ.get('FCKBOT_PROFILE_DIR')
|
||||||
|
if profile_dir:
|
||||||
|
return load_config(profile_dir)
|
||||||
|
|
||||||
|
# Если не задано, пробуем стандартные пути (для разработки)
|
||||||
|
possible_paths = [
|
||||||
|
'/usr/local/etc/fckbot/bots/metabot', # по умолчанию методолог
|
||||||
|
'/usr/local/etc/fckbot/profiles/metabot',
|
||||||
|
'./profiles/metabot',
|
||||||
|
]
|
||||||
|
for path in possible_paths:
|
||||||
|
if os.path.exists(path):
|
||||||
|
try:
|
||||||
|
return load_config(path)
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
raise RuntimeError(
|
||||||
|
"Не удалось загрузить конфигурацию проекта. "
|
||||||
|
"Установите переменную окружения FCKBOT_PROFILE_DIR с путём к профилю бота."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def run_migrations_offline() -> None:
|
||||||
|
"""
|
||||||
|
Запуск миграций в офлайн-режиме (без подключения к БД).
|
||||||
|
"""
|
||||||
|
app_config = get_app_config()
|
||||||
|
url = app_config.db_url # должно быть свойство, которое формирует URL
|
||||||
context.configure(
|
context.configure(
|
||||||
url=url,
|
url=url,
|
||||||
target_metadata=target_metadata,
|
target_metadata=target_metadata,
|
||||||
literal_binds=True,
|
literal_binds=True,
|
||||||
dialect_opts={"paramstyle": "named"},
|
dialect_opts={"paramstyle": "named"},
|
||||||
)
|
)
|
||||||
|
|
||||||
with context.begin_transaction():
|
with context.begin_transaction():
|
||||||
context.run_migrations()
|
context.run_migrations()
|
||||||
|
|
||||||
def run_migrations_online():
|
|
||||||
|
async def run_migrations_online() -> None:
|
||||||
|
"""
|
||||||
|
Запуск миграций в онлайн-режиме (с подключением к БД).
|
||||||
|
"""
|
||||||
|
app_config = get_app_config()
|
||||||
|
url = app_config.db_url
|
||||||
|
|
||||||
|
# Создаём асинхронный движок
|
||||||
connectable = create_async_engine(
|
connectable = create_async_engine(
|
||||||
config.get_main_option("sqlalchemy.url"),
|
url,
|
||||||
poolclass=pool.NullPool,
|
poolclass=pool.NullPool,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def run_async_migrations():
|
|
||||||
async with connectable.connect() as connection:
|
async with connectable.connect() as connection:
|
||||||
await connection.run_sync(do_run_migrations)
|
await connection.run_sync(do_run_migrations)
|
||||||
|
|
||||||
await connectable.dispose()
|
await connectable.dispose()
|
||||||
|
|
||||||
|
|
||||||
def do_run_migrations(connection):
|
def do_run_migrations(connection):
|
||||||
|
"""
|
||||||
|
Выполняет миграции на синхронном соединении (обёртка для асинхронного).
|
||||||
|
"""
|
||||||
context.configure(connection=connection, target_metadata=target_metadata)
|
context.configure(connection=connection, target_metadata=target_metadata)
|
||||||
|
|
||||||
with context.begin_transaction():
|
with context.begin_transaction():
|
||||||
context.run_migrations()
|
context.run_migrations()
|
||||||
|
|
||||||
asyncio.run(run_async_migrations())
|
|
||||||
|
|
||||||
if context.is_offline_mode():
|
if context.is_offline_mode():
|
||||||
run_migrations_offline()
|
run_migrations_offline()
|
||||||
else:
|
else:
|
||||||
run_migrations_online()
|
asyncio.run(run_migrations_online())
|
||||||
Reference in New Issue
Block a user