Files
paliad/internal/db/migrations/095_fristen_gap_fill.down.sql
mAi af30c06d9b feat(t-paliad-205): mig 095 — ingest t-paliad-203 fristen gap-fill deltas
Codifies curie's 4 new rules + 4 patches from
docs/proposals/fristen-gap-fill-2026-05-18.md § 0.3 (m's decisions).

NEW (4):
  inf.prelim         UPC_INF  parent=inf.soc      1mo  RoP.019.1  flag=with_po
  rev.prelim         UPC_REV  parent=rev.app      1mo  RoP.019.1  flag=with_po
  inf.appeal_spawn   UPC_INF  parent=inf.decision 2mo  RoP.220.1.a  always-fire  → UPC_APP
  rev.appeal_spawn   UPC_REV  parent=rev.decision 2mo  RoP.220.1.a  always-fire  → UPC_APP

PATCH (4):
  de_inf.klage       legal_source NULL → 'DE.ZPO.253'
  de_inf.anzeige     no change (already correct — explicit in audit log)
  de_inf.erwidg      is_court_set false → true + §276 Abs.1 S.2 description
  de_inf.berufung    defensive verify legal_source = 'DE.ZPO.517'

Idempotent via WHERE NOT EXISTS (no unique index on (proceeding_type_id,
code) — mig 093 left archived rows sharing codes with their published
successors, so ON CONFLICT isn't available). UPDATEs guarded by clauses
that only fire when the row still has the old value.

Backup snapshot in paliad.deadline_rules_pre_095 (CREATE TABLE IF NOT
EXISTS); down migration restores from it. Hard assertions verify all 4
new rules landed active+published, de_inf.erwidg flipped to court-set,
both spawn rules chain to a valid proceeding_type id=11.

Dry-run verified end-to-end against the live Supabase corpus inside
BEGIN/ROLLBACK; idempotency confirmed by running INSERT+UPDATE twice
in the same transaction.
2026-05-18 11:46:12 +02:00

62 lines
2.4 KiB
SQL

-- Reverses mig 095. Restores the 4 patched de_inf.* rows from
-- paliad.deadline_rules_pre_095 and removes the 4 new rules
-- (inf.prelim, rev.prelim, inf.appeal_spawn, rev.appeal_spawn).
--
-- The audit_reason is required by the mig 079 trigger for UPDATE +
-- DELETE; set_config at top supplies it.
SELECT set_config(
'paliad.audit_reason',
'mig 095 (down): revert t-paliad-205 fristen gap-fill — restore de_inf.* patches from deadline_rules_pre_095, delete 4 new rules',
true);
-- =============================================================================
-- 1. Delete the 4 new rules. Idempotent — if a rule is already missing
-- the DELETE matches zero rows.
-- =============================================================================
DELETE FROM paliad.deadline_rules
WHERE code IN ('inf.prelim', 'rev.prelim',
'inf.appeal_spawn', 'rev.appeal_spawn')
AND lifecycle_state = 'published';
-- =============================================================================
-- 2. Restore the 4 patched rows from the pre_095 snapshot. The snapshot
-- captured the rows at first up-migration run; the restore copies
-- each tracked field back. If the snapshot table doesn't exist (down
-- run before up), the restore is a no-op.
-- =============================================================================
DO $$
DECLARE
v_snap_exists boolean;
BEGIN
SELECT EXISTS (
SELECT 1
FROM information_schema.tables
WHERE table_schema = 'paliad'
AND table_name = 'deadline_rules_pre_095'
) INTO v_snap_exists;
IF NOT v_snap_exists THEN
RAISE NOTICE
'mig 095 (down): snapshot table paliad.deadline_rules_pre_095 missing — nothing to restore';
RETURN;
END IF;
UPDATE paliad.deadline_rules dr
SET legal_source = snap.legal_source,
is_court_set = snap.is_court_set,
description = snap.description,
updated_at = now()
FROM paliad.deadline_rules_pre_095 snap
WHERE dr.id = snap.id;
END $$;
-- =============================================================================
-- 3. Drop the snapshot table so a re-applied up migration captures a
-- fresh snapshot of the current state.
-- =============================================================================
DROP TABLE IF EXISTS paliad.deadline_rules_pre_095;