Files
tenant-registry/migrations/0022_document_store.up.sql
T
sharangandClaude Opus 5.5 2a0a274108
ci / shared (push) Successful in 13s
ci / test (push) Successful in 25m46s
ci / image (push) Successful in 51s
feat(store): Dokumentenablage with areas, checked serving, import (#88)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 13:38:10 +00:00

54 lines
3.2 KiB
SQL

-- Dokumentenablage (workspace ticket #171, decided 2026-10-20): the tenant's
-- own documents in three areas, next to what BreakPilot delivers to them
-- ("Unterlagen", area NULL). Read-only view: the original, and the prepared
-- version the Factory delivers (chunk file in LANE_B_UNIFIED_V5, images,
-- cookie-banner capture), every piece addressed and checked by its SHA-256.
ALTER TABLE documents
ADD COLUMN area TEXT CHECK (area IN ('legal_web', 'product_public', 'internal')),
-- the folder of a legal website document, as the workspace and the
-- Factory name it
ADD COLUMN legal_kind TEXT CHECK (legal_kind IN
('impressum', 'dse', 'cookie', 'agb', 'widerruf', 'barrierefreiheit', 'entsorgung', 'vsbg')),
-- none: not sent for preparation; pending: waiting for the Factory;
-- ready: prepared version stored; failed: the Factory gave up (reason kept)
ADD COLUMN processing_status TEXT NOT NULL DEFAULT 'none'
CHECK (processing_status IN ('none', 'pending', 'ready', 'failed')),
ADD COLUMN processing_error TEXT NOT NULL DEFAULT '' CHECK (char_length(processing_error) <= 500),
-- the Factory's id of the document (tenant_document_id), for imports
ADD COLUMN external_id TEXT NOT NULL DEFAULT '' CHECK (char_length(external_id) <= 120),
ADD CONSTRAINT documents_legal_kind_in_legal_area CHECK ((area = 'legal_web') = (legal_kind IS NOT NULL));
CREATE UNIQUE INDEX documents_tenant_external_id ON documents (tenant_id, external_id) WHERE external_id <> '';
CREATE INDEX documents_processing_pending ON documents (created_at) WHERE processing_status = 'pending';
-- The prepared version of a document, as delivered: the chunk file and/or the
-- cookie-banner capture, each stored as an object and kept by its digest.
CREATE TABLE document_preparations (
document_id UUID PRIMARY KEY REFERENCES documents(id) ON DELETE CASCADE,
format TEXT NOT NULL DEFAULT '' CHECK (char_length(format) <= 60),
revision INTEGER NOT NULL DEFAULT 0 CHECK (revision >= 0),
pin TEXT NOT NULL DEFAULT '' CHECK (char_length(pin) <= 80),
chunk_object_key TEXT NOT NULL DEFAULT '',
chunk_sha256 TEXT NOT NULL DEFAULT '' CHECK (chunk_sha256 = '' OR chunk_sha256 ~ '^[0-9a-f]{64}$'),
chunk_bytes BIGINT NOT NULL DEFAULT 0,
chunk_count INTEGER NOT NULL DEFAULT 0,
consent_object_key TEXT NOT NULL DEFAULT '',
consent_sha256 TEXT NOT NULL DEFAULT '' CHECK (consent_sha256 = '' OR consent_sha256 ~ '^[0-9a-f]{64}$'),
asset_digests TEXT[] NOT NULL DEFAULT '{}',
delivered_by TEXT NOT NULL DEFAULT '',
delivered_at TIMESTAMPTZ NOT NULL DEFAULT now(),
CHECK (chunk_sha256 <> '' OR consent_sha256 <> '')
);
-- Images of prepared documents (figures, page images), content-addressed per
-- tenant: a tenant only ever reads its own.
CREATE TABLE store_assets (
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
sha256 TEXT NOT NULL CHECK (sha256 ~ '^[0-9a-f]{64}$'),
object_key TEXT NOT NULL,
content_type TEXT NOT NULL,
size_bytes BIGINT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
PRIMARY KEY (tenant_id, sha256)
);