20260723_190_rule_cleanup_leases.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. """Add expiring claims for governed-rule cleanup workers."""
  2. from alembic import op
  3. revision = "20260723_190"
  4. down_revision = "20260723_180"
  5. branch_labels = None
  6. depends_on = None
  7. def upgrade() -> None:
  8. # Revision-180 cleanup workers must be stopped during this schema upgrade.
  9. # Their owner-only claims have no lease timestamp, so making those claims
  10. # immediately expired is the only deterministic, fail-safe takeover path.
  11. op.execute(
  12. """
  13. ALTER TABLE public.rule_violation_samples
  14. ADD COLUMN cleanup_claim_expires_at TIMESTAMPTZ;
  15. UPDATE public.rule_violation_samples
  16. SET cleanup_claim_expires_at = CURRENT_TIMESTAMP
  17. WHERE cleanup_claim IS NOT NULL
  18. AND cleanup_claim_expires_at IS NULL;
  19. CREATE INDEX idx_rule_violation_sample_cleanup_lease
  20. ON public.rule_violation_samples(
  21. cleanup_claim_expires_at, expires_at, id
  22. );
  23. ALTER TABLE public.rule_sql_staging_receipts
  24. ADD COLUMN cleanup_claim_expires_at TIMESTAMPTZ;
  25. UPDATE public.rule_sql_staging_receipts
  26. SET cleanup_claim_expires_at = CURRENT_TIMESTAMP
  27. WHERE cleanup_claim IS NOT NULL
  28. AND cleanup_claim_expires_at IS NULL;
  29. CREATE INDEX idx_rule_sql_receipt_cleanup_lease
  30. ON public.rule_sql_staging_receipts(
  31. cleanup_claim_expires_at, expires_at, id
  32. );
  33. """
  34. )
  35. def downgrade() -> None:
  36. raise RuntimeError("rule cleanup leases are forward-only")