109 lines
3.8 KiB
Python
109 lines
3.8 KiB
Python
"""Alembic environment configuration for StemEngine.
|
|
|
|
This module is invoked by ``alembic`` when running migration commands.
|
|
It wires the StemEngine SQLAlchemy models and DB URL into the Alembic
|
|
migration context so that:
|
|
|
|
* ``alembic upgrade head`` — applies pending migrations
|
|
* ``alembic revision --autogenerate`` — generates a new migration by diffing
|
|
models against the live DB schema
|
|
|
|
Usage (from the ``backend/`` directory)::
|
|
|
|
alembic -c alembic.ini upgrade head
|
|
alembic -c alembic.ini revision --autogenerate -m "describe the change"
|
|
|
|
Or from the project root (``docker/appdata/stemengine/``)::
|
|
|
|
alembic -c backend/alembic.ini upgrade head
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import sys
|
|
from logging.config import fileConfig
|
|
from pathlib import Path
|
|
|
|
from alembic import context
|
|
from sqlalchemy import engine_from_config, pool
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Make the backend package importable regardless of CWD.
|
|
# ---------------------------------------------------------------------------
|
|
_backend_parent = Path(__file__).resolve().parents[1] # stemengine/
|
|
if str(_backend_parent) not in sys.path:
|
|
sys.path.insert(0, str(_backend_parent))
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Import application metadata so Alembic can diff the ORM models.
|
|
# ---------------------------------------------------------------------------
|
|
from backend.db import Base, engine as _app_engine # noqa: E402
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Alembic Config object — gives access to alembic.ini values.
|
|
# ---------------------------------------------------------------------------
|
|
config = context.config
|
|
|
|
# Apply logging from alembic.ini (if present; may be absent in programmatic use).
|
|
if config.config_file_name is not None:
|
|
fileConfig(config.config_file_name)
|
|
|
|
# Expose the ORM metadata so --autogenerate can compare model vs. DB schema.
|
|
target_metadata = Base.metadata
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# DB URL resolution
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def _get_url() -> str:
|
|
"""Return the SQLAlchemy URL for the live database.
|
|
|
|
Prefers the running app's already-configured engine URL so that env-var
|
|
overrides (DB_PATH, MEDIA_DIR) are honoured automatically.
|
|
"""
|
|
return str(_app_engine.url)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Migration runners
|
|
# ---------------------------------------------------------------------------
|
|
|
|
def run_migrations_offline() -> None:
|
|
"""Run migrations in 'offline' mode (emit SQL without connecting).
|
|
|
|
This is useful for generating migration SQL scripts that can be reviewed
|
|
and applied manually.
|
|
"""
|
|
url = _get_url()
|
|
context.configure(
|
|
url=url,
|
|
target_metadata=target_metadata,
|
|
literal_binds=True,
|
|
dialect_opts={"paramstyle": "named"},
|
|
render_as_batch=True, # required for SQLite ALTER TABLE support
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
def run_migrations_online() -> None:
|
|
"""Run migrations in 'online' mode (apply directly to the DB)."""
|
|
# Re-use the app engine so we share WAL mode / pragma settings.
|
|
connectable = _app_engine
|
|
|
|
with connectable.connect() as connection:
|
|
context.configure(
|
|
connection=connection,
|
|
target_metadata=target_metadata,
|
|
render_as_batch=True, # required for SQLite ALTER TABLE support
|
|
)
|
|
with context.begin_transaction():
|
|
context.run_migrations()
|
|
|
|
|
|
if context.is_offline_mode():
|
|
run_migrations_offline()
|
|
else:
|
|
run_migrations_online()
|