mirror of
https://github.com/sstent/AICyclingCoach.git
synced 2026-01-25 08:34:51 +00:00
71 lines
2.0 KiB
Python
71 lines
2.0 KiB
Python
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
|
|
import sys
|
|
import os
|
|
from pathlib import Path
|
|
|
|
# Add backend directory to path
|
|
# Add project root to path for alembic
|
|
project_root = Path(__file__).parent.parent.parent
|
|
sys.path.insert(0, str(project_root))
|
|
|
|
# Import base and models
|
|
from backend.app.models.base import Base
|
|
from backend.app.config import settings
|
|
|
|
# Import all models to ensure they're registered
|
|
from backend.app.models import *
|
|
|
|
config = context.config
|
|
fileConfig(config.config_file_name)
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
def run_migrations_offline():
|
|
"""Run migrations in 'offline' mode."""
|
|
url = settings.DATABASE_URL
|
|
context.configure(
|
|
url=url,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
render_as_batch=True, # Important for SQLite
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
async def run_migrations_online():
|
|
"""Run migrations in 'online' mode."""
|
|
# Ensure data directory exists
|
|
data_dir = Path("data")
|
|
data_dir.mkdir(exist_ok=True)
|
|
|
|
connectable = create_async_engine(
|
|
settings.DATABASE_URL,
|
|
poolclass=pool.NullPool,
|
|
connect_args={"check_same_thread": False} if "sqlite" in settings.DATABASE_URL else {}
|
|
)
|
|
|
|
async with connectable.connect() as connection:
|
|
await connection.run_sync(do_run_migrations)
|
|
|
|
await connectable.dispose()
|
|
|
|
def do_run_migrations(connection):
|
|
context.configure(
|
|
connection=connection,
|
|
target_metadata=target_metadata,
|
|
render_as_batch=True, # Important for SQLite ALTER TABLE support
|
|
)
|
|
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
import asyncio
|
|
asyncio.run(run_migrations_online()) |