feat(bloco-a): higiene alembic heads e inventario dos 9 datasets no registry

This commit is contained in:
gti
2026-09-28 16:12:21 -03:00
parent 2ff79e3b0e
commit cd44ae637b
3 changed files with 298 additions and 3 deletions
+4 -3
View File
@@ -12,7 +12,8 @@ from sqlalchemy import select, update
from .models import Dataset
STATUSES = ("DRAFT", "APPROVED", "VIGENTE", "OBSOLETO", "REJECTED")
STATUSES = ("DRAFT", "APPROVED", "VIGENTE", "ACTIVE", "OBSOLETO", "SUPERSEDED", "REJECTED")
VIGENTE_STATUSES = ("VIGENTE", "ACTIVE")
class RegistryError(Exception):
@@ -43,7 +44,7 @@ def diff_against_vigente(session, dataset: Dataset) -> dict:
"""A summary an ADMIN reviews before approving: what the current VIGENTE of the same kind looks
like vs this DRAFT - never the row-by-row content (that lives in the kind-specific entry tables),
just the registry-level facts that matter for an approval decision."""
current = session.scalar(select(Dataset).where(Dataset.kind == dataset.kind, Dataset.status == "VIGENTE"))
current = session.scalar(select(Dataset).where(Dataset.kind == dataset.kind, Dataset.status.in_(VIGENTE_STATUSES)).order_by(Dataset.id.desc()))
return {
"kind": dataset.kind,
"draft": {"id": dataset.id, "version_label": dataset.version_label, "sha256": dataset.sha256, "record_count": dataset.record_count,
@@ -67,7 +68,7 @@ def approve_draft(session, dataset_id: int, *, admin_name: str) -> Dataset:
raise RegistryError(f"only a DRAFT dataset can be approved (current status: {dataset.status})")
if dataset.parse_errors:
raise RegistryError("dataset has recorded parse errors - re-submit a clean version")
session.execute(update(Dataset).where(Dataset.kind == dataset.kind, Dataset.status == "VIGENTE").values(status="OBSOLETO"))
session.execute(update(Dataset).where(Dataset.kind == dataset.kind, Dataset.status.in_(VIGENTE_STATUSES)).values(status="OBSOLETO"))
dataset.status = "VIGENTE"
dataset.approved_by = admin_name
dataset.approved_at = datetime.now(timezone.utc)