75 lines
2.0 KiB
Python
75 lines
2.0 KiB
Python
# Created by: Claude
|
|
# Date: 2026-01-02
|
|
# Purpose: Configuration de l'environnement Alembic
|
|
# Refs: server/CLAUDE.md
|
|
|
|
from logging.config import fileConfig
|
|
from sqlalchemy import engine_from_config
|
|
from sqlalchemy import pool
|
|
from alembic import context
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Ajouter le dossier src au path
|
|
sys.path.insert(0, str(Path(__file__).parent.parent))
|
|
|
|
from src.db.base import Base
|
|
from src.db.models import User, Device, Room, RoomMember, Message, P2PSession
|
|
from src.config import settings
|
|
|
|
# Alembic Config object
|
|
config = context.config
|
|
|
|
# Interpréter le fichier de configuration pour les loggers
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
# Métadonnées des modèles pour autogenerate
|
|
target_metadata = Base.metadata
|
|
|
|
# Overrider l'URL de la DB depuis les settings
|
|
config.set_main_option("sqlalchemy.url", settings.database_url)
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
"""
|
|
Exécuter les migrations en mode 'offline'.
|
|
Configure le contexte avec juste une URL, sans créer d'Engine.
|
|
"""
|
|
url = config.get_main_option("sqlalchemy.url")
|
|
context.configure(
|
|
url=url,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def run_migrations_online() -> None:
|
|
"""
|
|
Exécuter les migrations en mode 'online'.
|
|
Crée un Engine et associe une connexion au contexte.
|
|
"""
|
|
connectable = engine_from_config(
|
|
config.get_section(config.config_ini_section, {}),
|
|
prefix="sqlalchemy.",
|
|
poolclass=pool.NullPool,
|
|
)
|
|
|
|
with connectable.connect() as connection:
|
|
context.configure(
|
|
connection=connection, target_metadata=target_metadata
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|