Files
0pt/sql/004_iceberg_write_boundary.sql
T

63 lines
2.9 KiB
SQL

-- 0pt Iceberg write-path ownership repair (docs/ICEBERG_WRITE_BOUNDARY.md)
-- Two tables: a REFERENCE from a 0pt dataset to its Iceberg table in ST0RE's
-- table registry, and 0pt's own source-rate/crawl-compliance policy (moved
-- out of N0D per ADR-0030's audit -- see docs/10-lakehouse-decision.md).
--
-- opt_dataset_iceberg_mappings.iceberg_table_id is intentionally NOT a
-- foreign key -- it references ST0RE's st0re_iceberg_tables.table_id, a
-- different service's database. 0pt holds only the reference, never a copy
-- of ST0RE's table/catalog/snapshot state (see the module doc comment on
-- src/clients/st0re-lakehouse-client.ts).
--
-- Ordering note (added by the Source Registry Postgres-persistence pass):
-- opt_source_rate_policies below has always forward-referenced
-- `opt_sources (source_id)`, a table no sql/ file defined until
-- sql/002_source_registry.sql landed -- applying this file to a fresh
-- database before 002 would fail on that FK. 002 is now the one that must
-- run first (001 -> 002 -> 004 -> 005). Flagging, not fixing here: this
-- file's OTHER forward reference, `opt_dataset_iceberg_mappings.dataset_id
-- REFERENCES opt_datasets (dataset_id)` below, has the identical problem --
-- no sql/ file defines opt_datasets either -- and remains open; it is a
-- dataset-registry gap, unrelated to and out of scope for the Source
-- Registry work that added 002.
CREATE TABLE IF NOT EXISTS opt_dataset_iceberg_mappings (
dataset_iceberg_mapping_id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
project_id TEXT NOT NULL,
dataset_id TEXT NOT NULL REFERENCES opt_datasets (dataset_id),
iceberg_table_id TEXT NOT NULL,
namespace TEXT,
table_name TEXT,
mapping_status TEXT NOT NULL DEFAULT 'active',
mapped_by TEXT,
correlation_id TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ
);
CREATE INDEX IF NOT EXISTS opt_dataset_iceberg_mappings_dataset_idx ON opt_dataset_iceberg_mappings (dataset_id);
CREATE INDEX IF NOT EXISTS opt_dataset_iceberg_mappings_tenant_project_idx ON opt_dataset_iceberg_mappings (tenant_id, project_id);
CREATE INDEX IF NOT EXISTS opt_dataset_iceberg_mappings_table_idx ON opt_dataset_iceberg_mappings (iceberg_table_id);
CREATE TABLE IF NOT EXISTS opt_source_rate_policies (
source_rate_policy_id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
project_id TEXT NOT NULL,
source_id TEXT NOT NULL REFERENCES opt_sources (source_id),
rate_limit_per_minute NUMERIC NOT NULL,
crawl_budget NUMERIC NOT NULL,
compliance_notes TEXT,
domain TEXT,
robots_ok BOOLEAN,
access TEXT,
status TEXT NOT NULL DEFAULT 'active',
correlation_id TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ,
created_by TEXT
);
CREATE INDEX IF NOT EXISTS opt_source_rate_policies_source_idx ON opt_source_rate_policies (source_id);
CREATE INDEX IF NOT EXISTS opt_source_rate_policies_tenant_project_idx ON opt_source_rate_policies (tenant_id, project_id);