Files
webmail-client/backend/app/plugins/migrate.py
T
lukasandClaude Opus 4.8 4e077f1a37 M3 plugin platform: SDK + loader, RLS-isolated tables, migrations, jobs
Backend foundation for the plugin platform (SPEC §11), Phases 1-3.

Phase 1 — SDK + loader:
- app/plugins/types.py: frozen Plugin/UiManifest/Slot/Page/JobSpec contract.
- app/plugins/sdk.py: sdk(name) facade — the only app.* import allowed for plugins
  (scoped session, plugin.{name}.* publish, blob, outbox, settings, SSRF http_get, KV, enqueue).
- app/plugins/loader.py: entry-point discovery (webmail.plugins), hook forwarding
  (API + worker), router mounting at /api/plugins/{name}, ui-manifest aggregation.
- plugin_kv core table (migration 0013) + GET /api/plugins/ui-manifest.

Phase 2 — non-removable core plugins + DB-enforced privacy:
- builtin flag (calendar/contacts will be builtin=True; uninstall refuses them).
- RLS per-user isolation on plg_* tables: least-privilege webmail_plugin role
  (migration 0014), plugin_session(user_id) sets LOCAL ROLE + app.current_user_id,
  rls.enable_rls() (ENABLE+FORCE RLS + USING/WITH CHECK policy). Mail untouched.
- Per-plugin Alembic runner (shared env, dedicated version table) + install/uninstall
  --drop-data CLI; skeleton fixture proves install -> run -> uninstall --drop-data
  leaves zero residue (§12 M3 acceptance).

Phase 3 — plugin jobs (§11.2):
- JobSpec registered as arq function plg.{plugin}.{func} + cron (5-field parser);
  singleton=True guarded by a per-(plugin,user) Redis lock; wired onto the shared
  interactive worker.

Gates: ruff, mypy --strict (94 files + tests), full suite 326 passed. RLS isolation,
migration round-trip, and singleton lock covered by tests.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Rcj3TpydMTzTSmU7kunzf8
2026-07-26 21:42:35 +02:00

99 lines
3.6 KiB
Python

"""Per-plugin Alembic migration runner + install/uninstall lifecycle (SPEC §11.1).
Each plugin ships its own revision directory (``Plugin.migrations``) and runs
against a dedicated version table, so its schema history is independent of core
and of other plugins. ``install`` upgrades to head; ``uninstall --drop-data``
runs the plugin's downgrade chain and removes the version table, leaving zero
residue (the §12 M3 acceptance round-trip). Builtin (core) plugins refuse removal.
"""
from __future__ import annotations
import asyncio
import logging
from pathlib import Path
from alembic import command
from alembic.config import Config
from sqlalchemy import text
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.pool import NullPool
from app.config import get_settings
from app.plugins import loader
from app.plugins.types import Plugin
logger = logging.getLogger(__name__)
_SHARED_ENV = str(Path(__file__).parent / "alembic")
class PluginMigrationError(Exception):
"""Raised when a plugin's migrations can't be run."""
def _version_table(plugin: Plugin) -> str:
# plugin.name is validated [a-z][a-z0-9_]* at construction, so this is safe to
# interpolate into DDL.
return f"alembic_version_plg_{plugin.name}"
def _config(plugin: Plugin) -> Config:
if not plugin.migrations:
raise PluginMigrationError(f"plugin {plugin.name!r} declares no migrations")
cfg = Config()
cfg.set_main_option("script_location", _SHARED_ENV)
cfg.set_main_option("version_locations", plugin.migrations)
cfg.set_main_option("path_separator", "os") # split version_locations on os.pathsep only
cfg.set_main_option("version_table", _version_table(plugin))
return cfg
def upgrade(plugin: Plugin, revision: str = "head") -> None:
"""Run the plugin's migrations up to ``revision`` (default: head)."""
command.upgrade(_config(plugin), revision)
def downgrade(plugin: Plugin, revision: str = "base") -> None:
"""Run the plugin's downgrades to ``revision`` (default: base = drop all)."""
command.downgrade(_config(plugin), revision)
async def _drop_version_table(plugin: Plugin) -> None:
# Fresh engine (not the shared global one) so this is safe to call from any loop.
engine = create_async_engine(get_settings().database_url, poolclass=NullPool)
try:
async with engine.begin() as conn:
await conn.execute(text(f"DROP TABLE IF EXISTS {_version_table(plugin)}"))
finally:
await engine.dispose()
def install(name: str) -> None:
"""Apply a plugin's migrations (create its ``plg_*`` tables). No-op if none."""
plugin = loader.get_plugin(name)
if plugin is None:
raise PluginMigrationError(f"unknown plugin {name!r}")
if plugin.migrations is None:
logger.info("plugin %s has no migrations; nothing to install", name)
return
logger.info("installing plugin %s: upgrade -> head", name)
upgrade(plugin, "head")
def uninstall(name: str, *, drop_data: bool = False) -> None:
"""Uninstall a plugin. Refuses builtin plugins.
Without ``drop_data`` the schema/data are kept (a reversible disable). With
``drop_data`` the downgrade chain runs and the version table is removed, so
nothing remains in the database (SPEC §12 M3 acceptance)."""
plugin = loader.assert_removable(name) # raises PluginRemovalError for builtin/unknown
if not drop_data:
logger.info("uninstalling plugin %s (data kept)", name)
return
if plugin.migrations is None:
return
logger.info("uninstalling plugin %s --drop-data: downgrade -> base", name)
downgrade(plugin, "base")
asyncio.run(_drop_version_table(plugin))