75 lines
2.4 KiB
Python
Executable File
75 lines
2.4 KiB
Python
Executable File
import asyncio
|
|
from logging.config import fileConfig
|
|
import os
|
|
import sys
|
|
|
|
from sqlalchemy import pool
|
|
from sqlalchemy.ext.asyncio import async_engine_from_config
|
|
from alembic import context
|
|
|
|
# --- ÚTVONAL JAVÍTÁS ---
|
|
# Az aktuális fájl (env.py) helyéből kiindulva meghatározzuk a könyvtárakat
|
|
current_dir = os.path.dirname(os.path.realpath(__file__))
|
|
project_root = os.path.realpath(os.path.join(current_dir, '..'))
|
|
backend_dir = os.path.join(project_root, 'backend')
|
|
|
|
# Mindkét útvonalat betesszük a keresőbe, hogy a 'backend.app' és a sima 'app' is működjön
|
|
sys.path.insert(0, project_root)
|
|
sys.path.insert(0, backend_dir)
|
|
|
|
# Most már az Alembic megtalálja a konfigurációt és a modelleket
|
|
try:
|
|
from app.core.config import settings
|
|
from app.db.base import Base
|
|
from app.models import * # Fontos, hogy minden modell be legyen importálva!
|
|
except ImportError as e:
|
|
print(f"Hiba az importálásnál: {e}")
|
|
print(f"Próbált útvonalak: {sys.path}")
|
|
raise
|
|
|
|
config = context.config
|
|
|
|
# Dinamikus adatbázis URL a .env alapján (App User jelszavával)
|
|
config.set_main_option("sqlalchemy.url", settings.DATABASE_URL)
|
|
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
def do_run_migrations(connection):
|
|
context.configure(
|
|
connection=connection,
|
|
target_metadata=target_metadata,
|
|
include_schemas=True, # Adatbázis sémák (pl. 'data') támogatása
|
|
version_table_schema='public' # Alembic tábla a public-ban marad
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
async def run_migrations_online() -> None:
|
|
"""Aszinkron kapcsolat felépítése és migráció futtatása"""
|
|
connectable = async_engine_from_config(
|
|
config.get_section(config.config_ini_section, {}),
|
|
prefix="sqlalchemy.",
|
|
poolclass=pool.NullPool,
|
|
)
|
|
|
|
async with connectable.connect() as connection:
|
|
await connection.run_sync(do_run_migrations)
|
|
|
|
await connectable.dispose()
|
|
|
|
if context.is_offline_mode():
|
|
url = config.get_main_option("sqlalchemy.url")
|
|
context.configure(
|
|
url=url,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
include_schemas=True
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
else:
|
|
asyncio.run(run_migrations_online()) |