Consolidates 5 name-groups with synthetic null.<8hex> codes (minted by mig 136 from legacy submission_code IS NULL rows) onto a single canonical PE per name. 9 duplicate rows archived (is_active=false, lifecycle_state='archived'), 9 sequencing_rules reparented onto their canonical procedural_event. Worst offender: "Mängelbeseitigung / Zahlung" 6 → 1. Audit-first: per-row RAISE NOTICE before the writes, plus snapshots in paliad.procedural_events_pre_151 and paliad.sequencing_rules_pre_151 (same TX, mirrors precedent pre_091/093/095/098/140). Post-asserts that no name-group still has >1 active+published null.* row and no sr points at an archived PE. Pre-flight schema audit confirmed no audit trigger on procedural_events or sequencing_rules (only INSTEAD OF triggers on deadline_rules_unified, which don't fire on direct table writes), 0 deadlines + 0 draft_of refs to the duplicates, and lifecycle_state has no CHECK constraint blocking 'archived'. .down.sql best-effort restores sr.procedural_event_id and reactivates the archived rows from the snapshot tables. Mig already applied to youpc paliad schema via Supabase MCP within the same TX as the applied_migrations row insert (checksum matches the embedded file); deployed binary will see version 151 as applied.
32 lines
1.3 KiB
SQL
32 lines
1.3 KiB
SQL
-- 151_dedupe_null_procedural_events (down) — t-paliad-319 / m/paliad#144
|
|
--
|
|
-- Best-effort restore from paliad.procedural_events_pre_151 and
|
|
-- paliad.sequencing_rules_pre_151. Re-points the reparented
|
|
-- sequencing_rules back at their original procedural_event_id and
|
|
-- reactivates the archived duplicates with the lifecycle_state +
|
|
-- is_active they had before the up migration.
|
|
--
|
|
-- Catastrophic-recovery path only; the normal revert is to leave the
|
|
-- dedupe in place (it is purely cosmetic).
|
|
|
|
-- 1. Re-point sequencing_rules.procedural_event_id back to its
|
|
-- pre-mig-151 value. The snapshot row is keyed by sr.id so the
|
|
-- join is 1:1 and idempotent.
|
|
UPDATE paliad.sequencing_rules sr
|
|
SET procedural_event_id = s.original_procedural_event_id,
|
|
updated_at = now()
|
|
FROM paliad.sequencing_rules_pre_151 s
|
|
WHERE sr.id = s.id;
|
|
|
|
-- 2. Reactivate the archived duplicates with their snapshot lifecycle.
|
|
UPDATE paliad.procedural_events pe
|
|
SET is_active = s.is_active,
|
|
lifecycle_state = s.lifecycle_state,
|
|
updated_at = now()
|
|
FROM paliad.procedural_events_pre_151 s
|
|
WHERE pe.id = s.id;
|
|
|
|
-- 3. Drop the snapshot tables — the data is back in place.
|
|
DROP TABLE IF EXISTS paliad.sequencing_rules_pre_151;
|
|
DROP TABLE IF EXISTS paliad.procedural_events_pre_151;
|