Files
paliad/internal/db/migrations/156_trigger_event_id_partial_deprecation.up.sql
mAi d6a5dedb2b feat(deadline-system): P4 (partial) — partial trigger_events deprecation (m/paliad#149)
Phase 2 P4 partial-scope (head approved 2026-05-27 15:24). The full
drop of paliad.trigger_events + the legacy route + 5 read sites is
gated on an editorial backfill that's not in coder scope — 73 active
sequencing_rules carry proceeding_type_id IS NULL and are addressed
ONLY via trigger_event_id today. Dropping anything would break those
73 orphans.

What this lands:

  1. Mig 156 — NULL out trigger_event_id on the 2 hybrid rules that
     carry BOTH parent_id AND trigger_event_id. Per design §2.1 /
     m's Q1, parent_id is the canonical predecessor link; the
     hybrid trigger_event_id was redundant. The 2 rules' parent_id
     chains keep the live edge. Live-DB verified post-apply: 0
     active hybrid rules remain.

  2. Deprecation + Link headers on POST /api/tools/event-deadlines
     per RFC 8594 / RFC 9745. The route stays functional so the 73
     orphans keep working until reparenting lands.

What this does NOT land (gated on editorial):

  - DROP TABLE paliad.trigger_events
  - DROP COLUMN paliad.sequencing_rules.trigger_event_id
  - Remove the legacy /api/tools/event-deadlines handler
  - Remove EventDeadlineService + ExportService::1680 sheet
  - Remove deadline_rule_service.go:226 label-fallback path
  - Remove event_type_service.go:40+414 reads (33 event_types still
    reference trigger_event_id)
  - Update cmd/gen-upc-snapshot/main.go:185-202 to skip trigger_events
  - Drop the sequencing_rules_trigger_event_id_fkey FK

All of the above lands in a follow-up mig once the orphan count
hits zero. Comment to follow on m/paliad#149 with the editorial-
backlog list.

Verified: live-DB pre/post hybrid count (0 active hybrids remain);
mig idempotent; go vet clean.

Design: docs/design-deadline-system-revision-2026-05-27.md §2.1
(parent_id canonical), §3.4 (legacy route fate), §4.3 (table fate),
§5 (slice train P5 row). t-paliad-331.
2026-05-27 15:25:53 +02:00

81 lines
3.3 KiB
PL/PgSQL

-- 156_trigger_event_id_partial_deprecation — t-paliad-331 / m/paliad#149 Phase 2 P4 (partial)
--
-- Partial deprecation step toward retiring paliad.trigger_events.
-- The full table-drop (and the route + service + 5 read-site removals
-- the design's §3.4 + §4.3 lay out) is gated on the editorial backfill
-- of the 73 orphan globals — sequencing_rules rows that carry
-- trigger_event_id NOT NULL AND proceeding_type_id IS NULL today. m
-- drives that walk via /admin/procedural-events at his cadence (no
-- coder time blocked); this mig prepares the way without breaking the
-- legacy route the orphans still depend on.
--
-- What this mig does (live-DB audited 2026-05-27 pre-flight):
--
-- 1. NULL out the 2 hybrid rules that carry BOTH parent_id AND
-- trigger_event_id. Per design §2.1 / m's Q1: parent_id is the
-- canonical predecessor link; trigger_event_id on those 2 rows is
-- redundant. The parent_id chain keeps the live edge — no data
-- loss, no route disruption (the route only reads trigger_event_id
-- for the 73 orphan globals, which have no parent_id).
--
-- 2. NOT-DROP the column or the table. Both stay live so the
-- /api/tools/event-deadlines route continues to serve the 73
-- orphan globals until editorial reparenting lands.
--
-- The full P4 (mig that DROPs paliad.trigger_events + the
-- `sequencing_rules.trigger_event_id` column + the legacy route +
-- EventDeadlineService + ExportService::1680 + cmd/gen-upc-snapshot/
-- main.go:185-202) lands AFTER the 73 orphans are reparented. Until
-- then, the legacy surface remains.
BEGIN;
SELECT set_config(
'paliad.audit_reason',
'mig 156: trigger_event_id partial deprecation — NULL out 2 hybrid rules (t-paliad-331 / m/paliad#149 Phase 2 P4 partial)',
true
);
-- ----------------------------------------------------------------
-- 1. Snapshot the 2 hybrid rows for audit + rollback.
-- ----------------------------------------------------------------
CREATE TABLE paliad.sequencing_rules_pre_156 AS
SELECT * FROM paliad.sequencing_rules
WHERE trigger_event_id IS NOT NULL
AND parent_id IS NOT NULL
AND is_active = true;
COMMENT ON TABLE paliad.sequencing_rules_pre_156 IS
'Snapshot of the 2 hybrid rules (trigger_event_id NOT NULL AND '
'parent_id NOT NULL) taken in the same TX as mig 156, before their '
'trigger_event_id is NULL''ed. Rollback aid until P4 final lands.';
-- ----------------------------------------------------------------
-- 2. NULL out trigger_event_id on hybrid rules — parent_id is the
-- canonical predecessor link per design §2.1.
-- ----------------------------------------------------------------
UPDATE paliad.sequencing_rules
SET trigger_event_id = NULL
WHERE trigger_event_id IS NOT NULL
AND parent_id IS NOT NULL
AND is_active = true;
DO $$
DECLARE
remaining_hybrids int;
BEGIN
SELECT COUNT(*) INTO remaining_hybrids
FROM paliad.sequencing_rules
WHERE trigger_event_id IS NOT NULL
AND parent_id IS NOT NULL
AND is_active = true;
IF remaining_hybrids <> 0 THEN
RAISE EXCEPTION '[mig 156] expected 0 active hybrid rules, found %', remaining_hybrids;
END IF;
RAISE NOTICE '[mig 156] hybrid-rule cleanup OK — 0 active rules carry both parent_id and trigger_event_id';
END $$;
COMMIT;