proofchain/packages/infra/schema/73_inspections.sql
caelum0x bac8f9e662 feat: Wave C+D — agent multi-skill verification engine + infra data layer
- agent: 6 auto-collecting registries (parsers/checks/risk/scoring/tools/pipelines);
  15 doc parsers, 12 check packs, 6 risk models, scoring dimensions + reconciler,
  ~15 Claude tools, 8 pipelines (financing/underwriting/dpp/compliance/grading/esg) + routes; 380 tests
- infra: BaseRepository + ~35 typed repositories, 56 composed schema modules,
  storage/queue/notifications/cache/events adapters; 203 tests
- pnpm -r typecheck/build/test all green

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-31 23:48:46 +03:00

48 lines
2.1 KiB
SQL

-- =============================================================================
-- Data / oracle — quality inspections read model. Mirrors the QualityInspection
-- contract. Design rules mirror 00_core.sql:
-- * On-chain identifiers stored as lowercase hex `text` with a format CHECK.
-- * Basis-point scores CHECK-constrained 0..10000.
-- * `updated_at` maintained by the shared set_updated_at() trigger.
-- * RLS enabled with a public read-only SELECT policy; writes via service role.
-- * Idempotent DDL — safe to re-run on every deploy.
-- =============================================================================
create table if not exists inspections (
id text primary key,
batch_id text check (batch_id is null or batch_id ~ '^0x[0-9a-f]{64}$'),
inspector text check (inspector is null or inspector ~ '^0x[0-9a-f]{40}$'),
inspection_type text,
result text not null default 'pending'
check (result in ('pending', 'passed', 'failed', 'waived')),
score integer check (score is null or score between 0 and 10000),
report_uri text,
metadata jsonb not null default '{}'::jsonb,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
create index if not exists inspections_batch_idx on inspections (batch_id);
create index if not exists inspections_inspector_idx on inspections (inspector);
create index if not exists inspections_result_idx on inspections (result);
drop trigger if exists inspections_set_updated_at on inspections;
create trigger inspections_set_updated_at
before update on inspections
for each row execute function set_updated_at();
do $$
declare
t text;
tables text[] := array['inspections'];
begin
foreach t in array tables loop
execute format('alter table %I enable row level security', t);
execute format('drop policy if exists %I_read_all on %I', t, t);
execute format(
'create policy %I_read_all on %I for select to anon, authenticated using (true)',
t, t
);
end loop;
end
$$;