399 lines
16 KiB
PL/PgSQL
399 lines
16 KiB
PL/PgSQL
-- =====================================================================
|
||
-- 尚数网本体管理平台 · 2026-08-24 schema 迁移脚本
|
||
-- =====================================================================
|
||
-- 适用: 把旧版库(任何 2026-08-13 之前的 schema)手动升级到当前版本
|
||
-- 等价于服务启动时 server/db.go migrate() + access_control.go
|
||
-- ensureAccessControlSchema() 的效果,但拆开便于:
|
||
-- 1. 调试(不启动服务即可单跑 DDL)
|
||
-- 2. 别人接手时手工核对 schema 演进
|
||
-- 3. 离线环境无 server 二进制时也能升级
|
||
--
|
||
-- 顺序:DDL 必须按以下顺序跑(FK 约束 + ALTER 引用都依赖前置表存在),
|
||
-- 否则 action_types.impl_function_id REFERENCES function_types(id)
|
||
-- 会失败并导致整条多语句事务回滚。
|
||
--
|
||
-- 执行: psql -U ssw -d ssw-ontology -v ON_ERROR_STOP=1 -f 2026-08-24-schema-migrate.sql
|
||
-- (-v ON_ERROR_STOP=1 让任何一条失败立刻退出,方便定位)
|
||
--
|
||
-- 配套: 跑完后跑 2026-08-24-builtin-seed.sql 灌内置 function / interface
|
||
-- / access-control 元数据(这些是数据,不是 DDL)
|
||
-- =====================================================================
|
||
|
||
BEGIN; -- 整段一个事务,失败回滚整体(IF NOT EXISTS 让重复执行幂等)
|
||
|
||
-- ---------- 1. 本体基础 7 张表 ----------
|
||
CREATE TABLE IF NOT EXISTS object_types (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
api_name TEXT UNIQUE NOT NULL,
|
||
display_name TEXT NOT NULL,
|
||
description TEXT DEFAULT '',
|
||
icon TEXT DEFAULT '',
|
||
group_name TEXT DEFAULT '未分类',
|
||
-- Palantir P1-1 (2026-08-24)
|
||
title_property TEXT DEFAULT '',
|
||
subtitle_property TEXT DEFAULT '',
|
||
color TEXT DEFAULT '',
|
||
primary_key TEXT DEFAULT 'pk',
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
updated_at TIMESTAMPTZ DEFAULT now()
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS properties (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
object_type_id BIGINT NOT NULL REFERENCES object_types(id) ON DELETE CASCADE,
|
||
api_name TEXT NOT NULL,
|
||
display_name TEXT NOT NULL,
|
||
data_type TEXT NOT NULL DEFAULT 'string',
|
||
required BOOLEAN DEFAULT FALSE,
|
||
enum_values JSONB DEFAULT '[]'::jsonb,
|
||
description TEXT DEFAULT '',
|
||
-- Palantir P1-2 (2026-08-24)
|
||
format TEXT DEFAULT '',
|
||
default_value JSONB,
|
||
indexable BOOLEAN DEFAULT FALSE,
|
||
vector_index BOOLEAN DEFAULT FALSE,
|
||
backing_column TEXT DEFAULT '',
|
||
UNIQUE(object_type_id, api_name)
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS link_types (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
api_name TEXT UNIQUE NOT NULL,
|
||
display_name TEXT NOT NULL,
|
||
source_type TEXT NOT NULL,
|
||
target_type TEXT NOT NULL,
|
||
cardinality TEXT DEFAULT 'many_to_many',
|
||
description TEXT DEFAULT '',
|
||
-- Palantir P1-3 (2026-08-24)
|
||
directional BOOLEAN DEFAULT FALSE,
|
||
source_property TEXT DEFAULT '',
|
||
target_property TEXT DEFAULT ''
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS link_properties (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
link_type_id BIGINT NOT NULL REFERENCES link_types(id) ON DELETE CASCADE,
|
||
api_name TEXT NOT NULL,
|
||
display_name TEXT NOT NULL,
|
||
data_type TEXT NOT NULL DEFAULT 'string',
|
||
required BOOLEAN DEFAULT FALSE,
|
||
enum_values JSONB DEFAULT '[]'::jsonb,
|
||
description TEXT DEFAULT '',
|
||
UNIQUE(link_type_id, api_name)
|
||
);
|
||
|
||
-- ⚠️ function_types 必须在 action_types 之前建(FK 依赖)
|
||
CREATE TABLE IF NOT EXISTS function_types (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
api_name TEXT UNIQUE NOT NULL,
|
||
display_name TEXT NOT NULL,
|
||
description TEXT DEFAULT '',
|
||
inputs JSONB DEFAULT '[]'::jsonb,
|
||
outputs JSONB DEFAULT '[]'::jsonb,
|
||
impl_kind TEXT DEFAULT 'builtin',
|
||
impl_ref TEXT DEFAULT '',
|
||
is_pure BOOLEAN DEFAULT TRUE,
|
||
builtin_fn TEXT DEFAULT '',
|
||
-- Palantir P0-2 (2026-08-24)
|
||
when_to_suggest JSONB DEFAULT '[]'::jsonb,
|
||
llm_tool_description TEXT DEFAULT '',
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
updated_at TIMESTAMPTZ DEFAULT now()
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS action_types (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
api_name TEXT UNIQUE NOT NULL,
|
||
display_name TEXT NOT NULL,
|
||
description TEXT DEFAULT '',
|
||
params JSONB DEFAULT '[]'::jsonb,
|
||
-- Palantir P0-1 (2026-08-24)
|
||
submission_criteria JSONB DEFAULT '{}'::jsonb,
|
||
side_effects JSONB DEFAULT '{}'::jsonb,
|
||
impl_function_id BIGINT REFERENCES function_types(id) ON DELETE SET NULL,
|
||
return_type TEXT DEFAULT '',
|
||
when_to_suggest JSONB DEFAULT '[]'::jsonb,
|
||
llm_tool_description TEXT DEFAULT ''
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS interface_types (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
api_name TEXT UNIQUE NOT NULL,
|
||
display_name TEXT NOT NULL,
|
||
description TEXT DEFAULT '',
|
||
protocol TEXT DEFAULT 'rest',
|
||
method TEXT DEFAULT 'GET',
|
||
path TEXT DEFAULT '',
|
||
inputs JSONB DEFAULT '[]'::jsonb,
|
||
outputs JSONB DEFAULT '{}'::jsonb,
|
||
requires_auth BOOLEAN DEFAULT TRUE,
|
||
expose_to_mcp BOOLEAN DEFAULT FALSE,
|
||
backing_ref TEXT DEFAULT '',
|
||
builtin BOOLEAN DEFAULT FALSE,
|
||
-- Palantir P1-4 (2026-08-24)
|
||
shared_properties JSONB DEFAULT '[]'::jsonb,
|
||
parent_interface_id BIGINT REFERENCES interface_types(id) ON DELETE SET NULL,
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
updated_at TIMESTAMPTZ DEFAULT now()
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS objects (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
object_type_id BIGINT NOT NULL REFERENCES object_types(id) ON DELETE CASCADE,
|
||
type_api_name TEXT NOT NULL,
|
||
pk TEXT NOT NULL,
|
||
name TEXT DEFAULT '',
|
||
properties_json JSONB DEFAULT '{}'::jsonb,
|
||
source TEXT DEFAULT '', -- 2026-08-24 新增(溯源列)
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
updated_at TIMESTAMPTZ DEFAULT now(),
|
||
UNIQUE(object_type_id, pk)
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS links (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
link_type TEXT NOT NULL,
|
||
source_type TEXT NOT NULL,
|
||
source_pk TEXT NOT NULL,
|
||
target_type TEXT NOT NULL,
|
||
target_pk TEXT NOT NULL,
|
||
source TEXT DEFAULT '', -- 2026-08-24 新增(溯源列)
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
UNIQUE(link_type, source_type, source_pk, target_type, target_pk)
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS ingestions (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
title TEXT NOT NULL,
|
||
raw_text TEXT DEFAULT '',
|
||
directive TEXT DEFAULT '',
|
||
mode TEXT DEFAULT 'auto',
|
||
status TEXT DEFAULT 'pending',
|
||
provider TEXT DEFAULT '',
|
||
result_json JSONB DEFAULT '{}'::jsonb,
|
||
summary TEXT DEFAULT '',
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
applied_at TIMESTAMPTZ
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS km_files (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
rel_path TEXT NOT NULL UNIQUE,
|
||
name TEXT NOT NULL,
|
||
folder TEXT NOT NULL DEFAULT '',
|
||
size BIGINT DEFAULT 0,
|
||
mime TEXT DEFAULT '',
|
||
ingested BOOLEAN DEFAULT FALSE,
|
||
ingestion_id BIGINT,
|
||
summary TEXT DEFAULT '',
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
updated_at TIMESTAMPTZ DEFAULT now()
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS data_connections (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
name TEXT NOT NULL,
|
||
conn_type TEXT NOT NULL DEFAULT 'file',
|
||
config_json JSONB DEFAULT '{}'::jsonb,
|
||
description TEXT DEFAULT '',
|
||
ingested BOOLEAN DEFAULT FALSE,
|
||
ingestion_id BIGINT,
|
||
summary TEXT DEFAULT '',
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
updated_at TIMESTAMPTZ DEFAULT now()
|
||
);
|
||
|
||
-- ---------- 2. 存量库兼容:补列(IF NOT EXISTS 幂等)----------
|
||
|
||
-- objects.name 列已在 CREATE TABLE 包含,但旧库没有 → 用 ALTER 补
|
||
ALTER TABLE objects ADD COLUMN IF NOT EXISTS name TEXT DEFAULT '';
|
||
ALTER TABLE objects ADD COLUMN IF NOT EXISTS source TEXT DEFAULT '';
|
||
ALTER TABLE links ADD COLUMN IF NOT EXISTS source TEXT DEFAULT '';
|
||
|
||
-- 回填 name(保障连接器名称检索精准)
|
||
UPDATE objects SET name = COALESCE(properties_json->>'name','')
|
||
WHERE name IS NULL OR name = '';
|
||
|
||
-- P0-2 提示调度字段
|
||
ALTER TABLE function_types ADD COLUMN IF NOT EXISTS when_to_suggest JSONB DEFAULT '[]'::jsonb;
|
||
ALTER TABLE function_types ADD COLUMN IF NOT EXISTS llm_tool_description TEXT DEFAULT '';
|
||
ALTER TABLE action_types ADD COLUMN IF NOT EXISTS when_to_suggest JSONB DEFAULT '[]'::jsonb;
|
||
ALTER TABLE action_types ADD COLUMN IF NOT EXISTS llm_tool_description TEXT DEFAULT '';
|
||
|
||
-- P0-1 Action 完整契约
|
||
ALTER TABLE action_types ADD COLUMN IF NOT EXISTS submission_criteria JSONB DEFAULT '{}'::jsonb;
|
||
ALTER TABLE action_types ADD COLUMN IF NOT EXISTS side_effects JSONB DEFAULT '{}'::jsonb;
|
||
ALTER TABLE action_types ADD COLUMN IF NOT EXISTS impl_function_id BIGINT REFERENCES function_types(id) ON DELETE SET NULL;
|
||
ALTER TABLE action_types ADD COLUMN IF NOT EXISTS return_type TEXT DEFAULT '';
|
||
|
||
-- P1-1 Object Type UX
|
||
ALTER TABLE object_types ADD COLUMN IF NOT EXISTS title_property TEXT DEFAULT '';
|
||
ALTER TABLE object_types ADD COLUMN IF NOT EXISTS subtitle_property TEXT DEFAULT '';
|
||
ALTER TABLE object_types ADD COLUMN IF NOT EXISTS color TEXT DEFAULT '';
|
||
ALTER TABLE object_types ADD COLUMN IF NOT EXISTS primary_key TEXT DEFAULT 'pk';
|
||
|
||
-- P1-2 Property 字段元数据
|
||
ALTER TABLE properties ADD COLUMN IF NOT EXISTS format TEXT DEFAULT '';
|
||
ALTER TABLE properties ADD COLUMN IF NOT EXISTS default_value JSONB;
|
||
ALTER TABLE properties ADD COLUMN IF NOT EXISTS indexable BOOLEAN DEFAULT FALSE;
|
||
ALTER TABLE properties ADD COLUMN IF NOT EXISTS vector_index BOOLEAN DEFAULT FALSE;
|
||
ALTER TABLE properties ADD COLUMN IF NOT EXISTS backing_column TEXT DEFAULT '';
|
||
|
||
-- P1-3 Link Type
|
||
ALTER TABLE link_types ADD COLUMN IF NOT EXISTS directional BOOLEAN DEFAULT FALSE;
|
||
ALTER TABLE link_types ADD COLUMN IF NOT EXISTS source_property TEXT DEFAULT '';
|
||
ALTER TABLE link_types ADD COLUMN IF NOT EXISTS target_property TEXT DEFAULT '';
|
||
|
||
-- P1-4 Interface 共享 properties + 多态
|
||
ALTER TABLE interface_types ADD COLUMN IF NOT EXISTS shared_properties JSONB DEFAULT '[]'::jsonb;
|
||
ALTER TABLE interface_types ADD COLUMN IF NOT EXISTS parent_interface_id BIGINT REFERENCES interface_types(id) ON DELETE SET NULL;
|
||
|
||
-- ---------- 3. 索引(核心检索路径必须建)----------
|
||
CREATE INDEX IF NOT EXISTS idx_objects_type ON objects(type_api_name);
|
||
CREATE INDEX IF NOT EXISTS idx_objects_name ON objects(name);
|
||
CREATE INDEX IF NOT EXISTS idx_objects_type_name ON objects(type_api_name, name);
|
||
CREATE INDEX IF NOT EXISTS idx_objects_source ON objects(source);
|
||
CREATE INDEX IF NOT EXISTS idx_links_source ON links(source_type, source_pk);
|
||
CREATE INDEX IF NOT EXISTS idx_links_target ON links(target_type, target_pk);
|
||
CREATE INDEX IF NOT EXISTS idx_links_source_tag ON links(source);
|
||
|
||
-- ---------- 4. 审计日志 2 张 ----------
|
||
CREATE TABLE IF NOT EXISTS access_log (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
principal TEXT DEFAULT '',
|
||
method TEXT DEFAULT '',
|
||
path TEXT DEFAULT '',
|
||
target_type TEXT DEFAULT '',
|
||
target_pk TEXT DEFAULT '',
|
||
action TEXT DEFAULT '',
|
||
decision TEXT DEFAULT '', -- allow | deny | redact
|
||
matched_grants JSONB DEFAULT '[]'::jsonb,
|
||
matched_markings JSONB DEFAULT '[]'::jsonb,
|
||
duration_ms INT DEFAULT 0
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_access_log_principal ON access_log(principal);
|
||
CREATE INDEX IF NOT EXISTS idx_access_log_created ON access_log(created_at);
|
||
|
||
CREATE TABLE IF NOT EXISTS ontology_audit_log (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
actor TEXT DEFAULT '', -- manager / connector / ...
|
||
action TEXT DEFAULT '', -- analyze | merge_preview | merge_execute | delete_zero_inst
|
||
target_type TEXT DEFAULT '',
|
||
details JSONB DEFAULT '{}'::jsonb,
|
||
affected_rows INT DEFAULT 0
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_audit_log_created ON ontology_audit_log(created_at);
|
||
CREATE INDEX IF NOT EXISTS idx_audit_log_actor ON ontology_audit_log(actor);
|
||
|
||
-- ---------- 5. 物化层 P2-1 (Materialization) ----------
|
||
CREATE TABLE IF NOT EXISTS materializations (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
api_name TEXT UNIQUE NOT NULL,
|
||
display_name TEXT NOT NULL,
|
||
description TEXT DEFAULT '',
|
||
source_object_type TEXT NOT NULL,
|
||
-- projection: JSONB 数组,每条 = {property, alias, transform?}
|
||
projection JSONB DEFAULT '[]'::jsonb,
|
||
refresh_strategy TEXT DEFAULT 'manual', -- manual / interval / on_commit
|
||
refresh_interval_seconds INT DEFAULT 0,
|
||
is_active BOOLEAN DEFAULT TRUE,
|
||
last_refresh_at TIMESTAMPTZ,
|
||
last_refresh_rows INT DEFAULT 0,
|
||
last_refresh_duration_ms INT DEFAULT 0,
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
updated_at TIMESTAMPTZ DEFAULT now()
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS materialization_rows (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
materialization_id BIGINT NOT NULL REFERENCES materializations(id) ON DELETE CASCADE,
|
||
source_pk TEXT NOT NULL,
|
||
source_type TEXT NOT NULL,
|
||
row_json JSONB DEFAULT '{}'::jsonb,
|
||
refreshed_at TIMESTAMPTZ DEFAULT now(),
|
||
UNIQUE(materialization_id, source_pk)
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_mat_rows_materialization ON materialization_rows(materialization_id);
|
||
CREATE INDEX IF NOT EXISTS idx_mat_rows_source ON materialization_rows(source_type, source_pk);
|
||
|
||
-- ---------- 6. Backing Datasets P2-2 ----------
|
||
CREATE TABLE IF NOT EXISTS backing_sources (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
api_name TEXT UNIQUE NOT NULL,
|
||
display_name TEXT NOT NULL,
|
||
description TEXT DEFAULT '',
|
||
source_kind TEXT NOT NULL DEFAULT 'other', -- postgres / mysql / csv / api / s3 / other
|
||
config_json JSONB DEFAULT '{}'::jsonb,
|
||
field_mapping JSONB DEFAULT '[]'::jsonb, -- [{source_column, target_property, transform?}]
|
||
materialization_id BIGINT REFERENCES materializations(id) ON DELETE SET NULL,
|
||
is_active BOOLEAN DEFAULT TRUE,
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
updated_at TIMESTAMPTZ DEFAULT now()
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_backing_sources_mat ON backing_sources(materialization_id);
|
||
|
||
-- ---------- 7. 访问控制 5 张(Dynamic Security)----------
|
||
CREATE TABLE IF NOT EXISTS principals (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
name TEXT UNIQUE NOT NULL,
|
||
description TEXT DEFAULT '',
|
||
api_key TEXT DEFAULT '',
|
||
password_hash TEXT DEFAULT NULL, -- 2026-08-24 新增(账号密码登录)
|
||
is_builtin BOOLEAN DEFAULT FALSE,
|
||
created_at TIMESTAMPTZ DEFAULT now()
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS principal_clearances (
|
||
principal TEXT NOT NULL,
|
||
marking TEXT NOT NULL,
|
||
PRIMARY KEY (principal, marking)
|
||
);
|
||
|
||
CREATE TABLE IF NOT EXISTS object_markings (
|
||
object_type TEXT NOT NULL,
|
||
object_pk TEXT NOT NULL,
|
||
marking TEXT NOT NULL,
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
PRIMARY KEY (object_type, object_pk, marking)
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_obj_markings_type_pk ON object_markings(object_type, object_pk);
|
||
CREATE INDEX IF NOT EXISTS idx_obj_markings_marking ON object_markings(marking);
|
||
|
||
CREATE TABLE IF NOT EXISTS grants (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
principal TEXT NOT NULL,
|
||
action TEXT NOT NULL,
|
||
scope_type TEXT DEFAULT '*',
|
||
scope_pk TEXT DEFAULT '*',
|
||
effect TEXT DEFAULT 'allow',
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
UNIQUE (principal, action, scope_type, scope_pk)
|
||
);
|
||
CREATE INDEX IF NOT EXISTS idx_grants_principal ON grants(principal);
|
||
|
||
ALTER TABLE principals ADD COLUMN IF NOT EXISTS password_hash TEXT DEFAULT NULL;
|
||
|
||
CREATE TABLE IF NOT EXISTS field_policies (
|
||
id BIGSERIAL PRIMARY KEY,
|
||
object_type TEXT NOT NULL,
|
||
property TEXT NOT NULL,
|
||
principal TEXT NOT NULL,
|
||
effect TEXT NOT NULL DEFAULT 'redact', -- redact / hide / full
|
||
created_at TIMESTAMPTZ DEFAULT now(),
|
||
UNIQUE (object_type, property, principal)
|
||
);
|
||
|
||
COMMIT;
|
||
|
||
-- ---------- 8. 验证 ----------
|
||
-- 跑完应该返回 22(基础 17 + link_properties + materializations + materialization_rows + backing_sources + ontology_audit_log + access_log = 22;如果 link_properties 重复计算,则 21)
|
||
SELECT count(*) AS table_count
|
||
FROM information_schema.tables
|
||
WHERE table_schema='public';
|
||
-- 期望: table_count >= 21
|
||
|
||
-- 应该返回 0(旧库本就没有 source 列,新库都建好了)
|
||
SELECT count(*) AS missing_object_source FROM objects WHERE source IS NULL;
|
||
SELECT count(*) AS missing_link_source FROM links WHERE source IS NULL; |