fix(db): dedupe doc_check_controls 3x + unique constraint
CI / detect-changes (pull_request) Failing after 5s
CI / branch-name (pull_request) Successful in 1s
CI / guardrail-integrity (pull_request) Failing after 2s
CI / secret-scan (pull_request) Failing after 5s
CI / dep-audit (pull_request) Failing after 12s
CI / sbom-scan (pull_request) Failing after 3s
CI / build-sha-integrity (pull_request) Failing after 3s
CI / validate-canonical-controls (pull_request) Failing after 1s
CI / loc-budget (pull_request) Has been skipped
CI / go-lint (pull_request) Has been skipped
CI / python-lint (pull_request) Has been skipped
CI / test-go (pull_request) Has been skipped
CI / iace-gt-coverage (pull_request) Has been skipped
CI / nodejs-lint (pull_request) Has been skipped
CI / nodejs-build (pull_request) Has been skipped
CI / test-python-backend (pull_request) Has been skipped
CI / test-python-document-crawler (pull_request) Has been skipped
CI / test-python-dsms-gateway (pull_request) Has been skipped
CI / detect-changes (push) Successful in 10s
CI / branch-name (push) Has been skipped
CI / secret-scan (push) Has been skipped
CI / dep-audit (push) Has been skipped
CI / guardrail-integrity (push) Has been skipped
CI / sbom-scan (push) Has been skipped
CI / build-sha-integrity (push) Successful in 8s
CI / validate-canonical-controls (push) Successful in 6s
CI / loc-budget (push) Successful in 19s
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / nodejs-build (push) Has been skipped
CI / test-go (push) Has been skipped
CI / test-python-backend (push) Successful in 25s
CI / test-python-document-crawler (push) Has been skipped
CI / test-python-dsms-gateway (push) Has been skipped
CI / iace-gt-coverage (push) Has been skipped

compliance.doc_check_controls war auf prod historisch trippliziert
(Dump-Artefakt ohne PK/Unique: jede (doc_type, control_id)-Zeile 3x,
5622 statt 1874 ueber alle 8 doc_types). Die Migration dedupt idempotent
(kleinste ctid behalten) und setzt UNIQUE(doc_type, control_id), damit
sich die Triplikation nicht wiederholen kann.

Auf prod bereits direkt angewandt und in _migration_history registriert
(read-only verifiziert: 1874, alle doc_types total=distinct, Constraint
aktiv); dieser Commit codifiziert die Migration in der Deploy-Kette,
damit ein Restore aus einem aelteren Dump sie automatisch re-appliziert.

[migration-approved]

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Benjamin Admin
2026-06-20 14:25:03 +02:00
parent b664d73ffc
commit 76d1dc5e00
@@ -0,0 +1,27 @@
-- 158_dedup_doc_check_controls.sql
-- Behebt die historische Triplikation von compliance.doc_check_controls
-- (Dump-Artefakt: kein PK/Unique -> identische Zeilen mehrfach; prod 3x).
-- Idempotent + verhindert Wiederkehr. macmini ist bereits dedupt (no-op),
-- prod wird 1713->571 / 1143->381 / 225->75 dedupt.
-- [migration-approved]
-- 1) Dedup: pro (doc_type, control_id) nur die Zeile mit kleinster ctid behalten.
DELETE FROM compliance.doc_check_controls a
USING compliance.doc_check_controls b
WHERE a.ctid > b.ctid
AND a.doc_type IS NOT DISTINCT FROM b.doc_type
AND a.control_id IS NOT DISTINCT FROM b.control_id;
-- 2) Wiederkehr verhindern: Unique-Constraint auf (doc_type, control_id).
DO $do$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'uq_doc_check_controls_doctype_control'
) THEN
ALTER TABLE compliance.doc_check_controls
ADD CONSTRAINT uq_doc_check_controls_doctype_control
UNIQUE (doc_type, control_id);
END IF;
END
$do$;