mirror of
https://github.com/sstent/AICyclingCoach.git
synced 2026-01-25 16:41:58 +00:00
56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
from logging.config import fileConfig
|
|
from sqlalchemy import engine_from_config, pool
|
|
from sqlalchemy.ext.asyncio import AsyncEngine
|
|
from alembic import context
|
|
import sys
|
|
import os
|
|
|
|
# Add app directory to path
|
|
sys.path.append(os.getcwd())
|
|
|
|
# Import base and models
|
|
from app.models import Base
|
|
from app.database import DATABASE_URL
|
|
|
|
config = context.config
|
|
fileConfig(config.config_file_name)
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
def run_migrations_offline():
|
|
"""Run migrations in 'offline' mode."""
|
|
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():
|
|
"""Run migrations in 'online' mode."""
|
|
connectable = AsyncEngine(
|
|
engine_from_config(
|
|
config.get_section(config.config_ini_section),
|
|
prefix="sqlalchemy.",
|
|
poolclass=pool.NullPool,
|
|
future=True,
|
|
url=DATABASE_URL,
|
|
)
|
|
)
|
|
|
|
async with connectable.connect() as connection:
|
|
await connection.run_sync(do_run_migrations)
|
|
|
|
async def do_run_migrations(connection):
|
|
context.configure(connection=connection, target_metadata=target_metadata)
|
|
await connection.run_sync(context.run_migrations)
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
import asyncio
|
|
asyncio.run(run_migrations_online()) |