papertrail/db/migrations/0037_bulk-import.sql
caelum0x 695a365ae4 feat: PaperTrail — enterprise evidence-verification platform
Multi-tenant platform (182 pages, 228 API routes, ~30 modules) built on a
deterministic verification core: clinical-trial claims checked against
ClinicalTrials.gov registered results and raw event counts (ARR/NNT/RR+CI
recomputed in code, oracle-tested), code-enforced exact-span grounding, and
multi-source cross-verification. Auth/RBAC/multi-tenancy, document processing
at scale (unpdf + Docling), agentic workflows, Claude Science connector,
compliance/audit, billing, public API, and more.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-08 07:21:22 +03:00

48 lines
2.0 KiB
SQL

-- Bulk import/export center. An import_batch captures one upload of CSV / BibTeX /
-- RIS text destined for a target table (claims | evidence | references), together
-- with the column mapping that projects parsed source rows onto the target's
-- fields. Each parsed record becomes an import_row (staged, not yet committed);
-- committing the batch inserts the mapped rows into the real target tables and
-- records per-row success/failure so a partial import is auditable.
-- Idempotent: safe to run repeatedly.
create table if not exists import_batches (
id uuid primary key default gen_random_uuid(),
org_id uuid not null references orgs(id) on delete cascade,
target text not null
check (target in ('claims', 'evidence', 'references')),
format text not null
check (format in ('csv', 'bibtex', 'ris')),
status text not null default 'pending'
check (status in ('pending', 'committing', 'committed', 'failed')),
total integer not null default 0,
succeeded integer not null default 0,
failed integer not null default 0,
mapping jsonb not null default '{}'::jsonb,
error text,
created_by uuid references users(id) on delete set null,
created_at timestamptz not null default now()
);
create index if not exists import_batches_org_id_idx
on import_batches(org_id, created_at desc);
create index if not exists import_batches_status_idx
on import_batches(org_id, status);
create table if not exists import_rows (
id uuid primary key default gen_random_uuid(),
org_id uuid not null references orgs(id) on delete cascade,
batch_id uuid not null references import_batches(id) on delete cascade,
row_index integer not null,
data jsonb not null default '{}'::jsonb,
status text not null default 'pending'
check (status in ('pending', 'succeeded', 'failed', 'skipped')),
error text,
created_at timestamptz not null default now()
);
create index if not exists import_rows_batch_idx
on import_rows(batch_id, row_index);
create index if not exists import_rows_org_id_idx
on import_rows(org_id, created_at desc);