"""Alembic environment. Always runs as the owner role (``WP_OWNER_DATABASE_URL``).""" from __future__ import annotations import importlib import os from alembic import context from sqlalchemy import create_engine, pool import core.db.models_registry # noqa: F401 (populates metadata) from core.config import get_settings from core.db.base import Base # Pack models (each pack's ``models`` module). Set WP_ALEMBIC_SKIP_PACKS=1 to autogenerate # core-only revisions. if not os.environ.get("WP_ALEMBIC_SKIP_PACKS"): from importlib.metadata import entry_points for _ep in entry_points(group="workpilot.packs"): _pkg = _ep.value.split(":")[0].rsplit(".", 1)[0] # "packs.demo.pack" -> "packs.demo" try: importlib.import_module(f"{_pkg}.models") except ModuleNotFoundError: pass # pack without its own tables config = context.config target_metadata = Base.metadata EXTERNAL_TABLES = {"checkpoints", "checkpoint_writes", "checkpoint_blobs", "checkpoint_migrations"} def include_object(obj, name, type_, reflected, compare_to): # type: ignore[no-untyped-def] """LangGraph owns its checkpoint tables; never autogenerate changes for them.""" table = obj.table.name if type_ in ("index", "column") else name return table not in EXTERNAL_TABLES def _url() -> str: return config.get_main_option("sqlalchemy.url") or get_settings().owner_database_url def run_migrations_offline() -> None: context.configure( url=_url(), target_metadata=target_metadata, literal_binds=True, include_object=include_object, ) with context.begin_transaction(): context.run_migrations() def run_migrations_online() -> None: engine = create_engine(_url(), poolclass=pool.NullPool) with engine.connect() as connection: context.configure( connection=connection, target_metadata=target_metadata, include_object=include_object ) with context.begin_transaction(): context.run_migrations() if context.is_offline_mode(): run_migrations_offline() else: run_migrations_online()