Files
HealthLog/prisma/migrations/0353_document_import_source/migration.sql
T
Marc-André Bombeck 55a32ff7d1 feat(documents): add a document upload token for imports from other systems
A narrow `documents:write` token, minted cookie-only at
`POST /api/tokens/documents` and accepted by the vault upload route alone.
A scoped caller draws on its own hourly bucket per token
(`DOCUMENT_UPLOAD_LIMIT_PER_HOUR`, default 120) and gets a receipt
`{ id, duplicate }` instead of the stored row.

Uploads take optional `sourceSystem` / `sourceId` fields. The source key is
unique per account across live and deleted documents, and the tombstone
purge carries it to a small ledger, so re-sending a document the owner
deleted answers 200 `deleted: true` and stores nothing. `aiRead=defer`
indexes locally and skips the summary and lab staging.

The detail sheet shows where an imported document came from; backup and
restore carry both fields; Delete All Data clears the ledger.

Refs #1038, #938
2026-09-25 21:48:04 +02:00

32 lines
1.4 KiB
SQL

-- Document import (#1038). Two optional provenance columns on the vault, a
-- per-user unique source key that covers tombstoned rows too, and a small
-- ledger the purge fills so a re-run importer cannot bring back a deleted
-- document after its tombstone is gone. Existing rows keep NULL and are not
-- touched by the index.
ALTER TABLE "inbound_documents"
ADD COLUMN "source_system" VARCHAR(16),
ADD COLUMN "source_id" VARCHAR(128);
-- Partial unique index (documented in schema.prisma; Prisma cannot express a
-- partial index). No `deleted_at` predicate on purpose: a tombstoned document
-- still owns its source key.
CREATE UNIQUE INDEX "inbound_documents_user_source_key"
ON "inbound_documents" ("user_id", "source_system", "source_id")
WHERE "source_id" IS NOT NULL;
CREATE TABLE "document_import_keys" (
"id" TEXT NOT NULL,
"user_id" TEXT NOT NULL,
"source_system" VARCHAR(16) NOT NULL,
"source_id" VARCHAR(128) NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "document_import_keys_pkey" PRIMARY KEY ("id")
);
CREATE UNIQUE INDEX "document_import_keys_user_id_source_system_source_id_key"
ON "document_import_keys" ("user_id", "source_system", "source_id");
ALTER TABLE "document_import_keys"
ADD CONSTRAINT "document_import_keys_user_id_fkey"
FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;