40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
"""Shared Alembic environment for plugin migration branches (SPEC §11.1).
|
|||
|
|
|
||
|
|
Every plugin reuses this env; the runner (:mod:`app.plugins.migrate`) points
|
||
|
|
``version_locations`` at the plugin's own revision directory and gives it a
|
||
|
|
dedicated ``version_table`` (``alembic_version_plg_{name}``) so each plugin's
|
||
|
|
migration history is independent of core and of every other plugin.
|
||
|
|
"""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import asyncio
|
||
|
|
|
||
|
|
from alembic import context
|
||
|
|
from sqlalchemy.ext.asyncio import async_engine_from_config
|
||
|
|
from sqlalchemy.pool import NullPool
|
||
|
|
|
||
|
|
from app.config import get_settings
|
||
|
|
|
||
|
|
config = context.config
|
||
|
|
config.set_main_option("sqlalchemy.url", get_settings().database_url)
|
||
|
|
_version_table = config.get_main_option("version_table") or "alembic_version"
|
||
|
|
|
||
|
|
|
||
|
|
def _do_run_migrations(connection: object) -> None:
|
||
|
|
context.configure(connection=connection, version_table=_version_table) # type: ignore[arg-type]
|
||
|
|
with context.begin_transaction():
|
||
|
|
context.run_migrations()
|
||
|
|
|
||
|
|
|
||
|
|
async def _run_online() -> None:
|
||
|
|
engine = async_engine_from_config(
|
||
|
|
config.get_section(config.config_ini_section, {}), prefix="sqlalchemy.", poolclass=NullPool
|
||
|
|
)
|
||
|
|
async with engine.connect() as connection:
|
||
|
|
await connection.run_sync(_do_run_migrations)
|
||
|
|
await engine.dispose()
|
||
|
|
|
||
|
|
|
||
|
|
asyncio.run(_run_online())
|