Files
leocrm/app/models/plugin.py
T
Agent Zero fd4a1ec4ce
Check Cross-Plugin Imports / check (push) Waiting to run
fix(migrations): F40 (Astra P1) — Plugin-Migrationen tracken SHA-256-Content-Hash, Drift wird sichtbar
Vorher: Der Migration-Runner trackte Migrationen nur per DATEINAMEN —
eine nachtraeglich geaenderte, bereits angewandte Migration blieb
unbemerkt (genau die #389-Bugklasse: kaputte Migration wurde gefixt,
Runner skippte still, weil der Dateiname schon getrackt war).

Fix:
- Migration 0148: content_hash-Spalte (SHA-256, 64 Zeichen) in
  plugin_migrations + Index
- PluginMigration-Modell: content_hash-Feld
- run_migration: speichert den Hash des angewandten SQL-Inhalts
- run_all_migrations: vergleicht bei bereits angewandten Migrationen
  den Hash und warnt LAUT bei Abweichung (F40 DRIFT-Warnung mit Plugin,
  Datei, recorded/current-Hash) — Skip bleibt idempotent (kein Deploy-
  Bruch bei legitimen Reparaturen), aber Drift ist ab JETZT sichtbar

Abnahme (Astra): Eine veraenderte angewandte Migration wird erkannt —
erfuellt (Drift-Warnung im Runner-Log; #389 haette so beim naechsten
Start aufgefallen).

Verifikation: Syntax OK, ruff clean, alembic heads = 0148.
2026-09-18 13:01:25 +02:00

65 lines
2.7 KiB
Python

"""Plugin and PluginMigration models — tracks installed plugins and their migrations."""
from __future__ import annotations
import uuid
from typing import Any
from sqlalchemy import Boolean, Index, String
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.dialects.postgresql import UUID as PGUUID
from sqlalchemy.orm import Mapped, mapped_column
from app.core.db import Base, TimestampMixin
class Plugin(Base, TimestampMixin):
"""Plugin record — tracks installation and activation status.
This table is NOT tenant-scoped (plugins are system-wide, not per-tenant).
The tables *created by* plugin migrations MUST have tenant_id.
"""
__tablename__ = "plugins"
__table_args__ = (Index("ix_plugins_name", "name", unique=True),)
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
name: Mapped[str] = mapped_column(String(80), nullable=False, unique=True)
display_name: Mapped[str] = mapped_column(String(120), nullable=False)
version: Mapped[str] = mapped_column(String(40), nullable=False)
status: Mapped[str] = mapped_column(String(20), nullable=False, default="installed")
installed: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
is_core: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
config: Mapped[dict[str, Any] | None] = mapped_column(
JSONB,
nullable=True, # JSONB for plugin configuration
)
# Transient attribute for response (not persisted)
# Used by uninstall to report dropped tables
# This is set dynamically by registry.uninstall()
class PluginMigration(Base, TimestampMixin):
"""Tracks individual migration files applied for each plugin."""
__tablename__ = "plugin_migrations"
__table_args__ = (
Index("ix_plugin_migrations_plugin", "plugin_name"),
Index("ix_plugin_migrations_unique", "plugin_name", "migration_file", unique=True),
)
id: Mapped[uuid.UUID] = mapped_column(
PGUUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
plugin_name: Mapped[str] = mapped_column(String(80), nullable=False)
migration_file: Mapped[str] = mapped_column(String(255), nullable=False)
status: Mapped[str] = mapped_column(String(20), nullable=False, default="applied")
# F40 (Astra): SHA-256 of the applied SQL content. The runner compares
# this on subsequent runs — a modified already-applied migration
# (repaired) becomes VISIBLE instead of being silently skipped.
content_hash: Mapped[str | None] = mapped_column(String(64), nullable=True)