Files
tenant-registry/migrations/0022_document_store.up.sql
T
Sharang ParnerkarandClaude Opus 5.5 94da0e0013
ci / shared (pull_request) Successful in 12s
ci / test (pull_request) Successful in 25m54s
ci / image (pull_request) Skipped
feat(store): Dokumentenablage with areas, checked serving, import
Workspace ticket #171 (decided 2026-10-20): the tenant's own documents
live next to BreakPilot's deliveries ("Unterlagen") in the same table and
bucket, in three areas, with the original and the prepared version the
Factory delivers.

- migration 0022: documents get area (legal_web / product_public /
  internal; NULL = Unterlagen), legal_kind (the 8 folders as the workspace
  and Factory name them), processing_status (none/pending/ready/failed),
  external_id (the Factory's tenant_document_id, unique per tenant);
  document_preparations (chunk file and/or cookie-banner capture with
  digest, format, revision, pin, image digests); store_assets (images per
  tenant, content-addressed)
- GET /v1/tenants/{id}/store; POST …/store/documents (customer upload:
  legal folder, product of an internal document; legal and product
  documents wait for the Factory, an internal one only on request);
  GET …/original, …/prepared, …/consent-banner and …/store/assets/{sha}:
  every byte is hashed and compared before anything is sent (fail-closed,
  502 digest_mismatch), HTML originals sandboxed
- POST /v1/admin/tenants/{id}/store/import (named staff only): a .tar.gz in
  the Factory's run layout; unsafe paths and links refused, every file
  checked against its digest, keyed on tenant_document_id so it can run
  again; per-document outcome, never content
- Unterlagen lists stay as they were (area NULL only); deleting a store
  document removes its prepared files; storage gains Get
- OpenAPI for the seven routes; tests on memory and Postgres with
  synthetic data only

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-20 15:11:52 +02: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)
);