8204675a66
Adds alembic/ (async env.py driven by DATABASE_URL) and the 0001 users-table migration matching the model. enabled now has a DB-level server_default of false (secure default even for non-ORM inserts). Verified upgrade/downgrade on sqlite.
55 lines
1.4 KiB
Python
55 lines
1.4 KiB
Python
"""Alembic migration environment (async, driven by DATABASE_URL)."""
|
|
|
|
import asyncio
|
|
import os
|
|
from logging.config import fileConfig
|
|
|
|
from alembic import context
|
|
from sqlalchemy.ext.asyncio import create_async_engine
|
|
|
|
from intervals_mcp_server.db.models import Base
|
|
|
|
config = context.config
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
def _database_url() -> str:
|
|
url = os.environ.get("DATABASE_URL") or config.get_main_option("sqlalchemy.url")
|
|
if not url:
|
|
raise RuntimeError("DATABASE_URL is not set")
|
|
return url
|
|
|
|
|
|
def run_migrations_offline() -> None:
|
|
context.configure(
|
|
url=_database_url(),
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
compare_type=True,
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def _run(connection) -> None:
|
|
context.configure(connection=connection, target_metadata=target_metadata, compare_type=True)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
async def run_migrations_online() -> None:
|
|
engine = create_async_engine(_database_url())
|
|
async with engine.connect() as connection:
|
|
await connection.run_sync(_run)
|
|
await engine.dispose()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
asyncio.run(run_migrations_online())
|