Files
archivdms/internal/storage/migrations/011_classification_templates.sql
T
patrick 9a24ea29e1 FDN-01: repository & projektgerüst
Git-Repository für bestehenden archivdms-Code initialisiert, Branch-/Commit-Konvention (feature/<ticket>-<slug>-Branches, Ticket-Prefix in Commit-Nachricht) etabliert.
2026-08-11 21:27:53 +02:00

53 lines
2.6 KiB
SQL

-- PROJ: classification-templates
-- Doku-only (siehe README.md in diesem Verzeichnis) — die tatsächliche
-- Ausführung passiert idempotent über
-- internal/storage/classification_templates.go
-- initClassificationTemplatesSchema(), aufgerufen aus (*Store).initSchema()
-- NACH initTaxonomySchema/initCustomFieldsSchema (FK auf document_types und
-- custom_field_defs).
--
-- Klassifizierungsvorlagen (classification templates): tenant-skopierte,
-- benannte Bündel aus Dokumenttyp, Tags, Custom-Field-Defaultwerten und einer
-- Aufbewahrungsdauer, die in einem Schritt auf ein Dokument angewendet werden
-- können. Bewusst KEINE persistente Kopplung template<->document (keine
-- template_id-Spalte auf documents): eine spätere Vorlagen-Änderung darf
-- Bestandsdokumente niemals rückwirkend verändern (GoBD-Nachvollziehbarkeit).
-- Die Anwendung wird ausschliesslich über einen Audit-Log-Eintrag festgehalten.
--
-- Retention-Regel (absolut, ohne Ausnahme): eine Vorlage kann retain_until nur
-- verlängern, niemals verkürzen — auch nicht mit overwrite=true. Ein
-- abgelehnter Verkürzungsversuch wird als Audit-Eintrag protokolliert.
CREATE TABLE IF NOT EXISTS classification_templates (
id BIGSERIAL PRIMARY KEY,
tenant_id BIGINT NOT NULL,
name TEXT NOT NULL,
description TEXT,
doc_type_id BIGINT REFERENCES document_types(id) ON DELETE SET NULL,
retain_years INT,
active BOOLEAN NOT NULL DEFAULT true,
created_by BIGINT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE(tenant_id, name)
);
CREATE INDEX IF NOT EXISTS idx_classification_templates_tenant ON classification_templates(tenant_id);
CREATE INDEX IF NOT EXISTS idx_classification_templates_doc_type ON classification_templates(doc_type_id);
CREATE TABLE IF NOT EXISTS classification_template_tags (
template_id BIGINT NOT NULL REFERENCES classification_templates(id) ON DELETE CASCADE,
tag_id BIGINT NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
PRIMARY KEY (template_id, tag_id)
);
CREATE TABLE IF NOT EXISTS classification_template_field_defaults (
template_id BIGINT NOT NULL REFERENCES classification_templates(id) ON DELETE CASCADE,
field_id BIGINT NOT NULL REFERENCES custom_field_defs(id) ON DELETE CASCADE,
value_text TEXT,
value_number NUMERIC,
value_date DATE,
value_bool BOOLEAN,
overwrite BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY (template_id, field_id)
);