Files
ssw-ontology/server/db.go
T
admin 993f74c2b2 feat: Docker 单镜像分发 + 默认端口统一 8081 + seed 极简化
新增 Dockerfile + docker/entrypoint.sh:
- 基础 crpi-.../ssw188/postgres:16-alpine + ontology 二进制,单镜像
- 默认端口 8081 / 库 ssw-ontology / 用户 ssw / 密码 ssw
- /data/pg 自动挂 PG 数据,/data/archive 挂文档根目录
- trap 收尸保证 PG + ontology 同生命周期

seed 极简化为 1 个对象类型「合作企业」+ 3 条示例
(尚数网/腾讯/阿里巴巴),便于演示也便于清理。

默认 HTTP_PORT 统一为 8081(覆盖 server/config.go、run.sh、
vite.config.js、importer/import-from-xeo.js、docs/README.md、
docs/USAGE.md、docs/IMPORT.md、.env.example 等所有文档/代码)。

docs/README.md / .env.example 默认 DATABASE_URL 改为
ssw/[email protected]:5432/ssw-ontology(与 Docker 镜像自带 PG 一致);
run.sh 删 dacli 硬编码 fallback,改从 .env 读(不存在则报错退出)。

README 重整:分「方式 A Docker 镜像 / 方式 B 本机 ./run.sh」,
新增 AI 智能导入(-e AI_*)与管理接口鉴权(-e MANAGER_API_KEY)说明。
2026-08-13 15:59:27 +08:00

350 lines
13 KiB
Go

package main
import (
"context"
"fmt"
"log"
"time"
"github.com/jackc/pgx/v5/pgxpool"
)
var pool *pgxpool.Pool
// initDB 连接 Postgres 并执行 schema 迁移。
func initDB(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
cfg, err := pgxpool.ParseConfig(dsn)
if err != nil {
return nil, fmt.Errorf("解析 DSN 失败: %w", err)
}
cfg.MaxConns = 20
cfg.MaxConnIdleTime = 5 * time.Minute
p, err := pgxpool.NewWithConfig(ctx, cfg)
if err != nil {
return nil, fmt.Errorf("连接 Postgres 失败: %w", err)
}
if err := p.Ping(ctx); err != nil {
return nil, fmt.Errorf("Postgres 不可达: %w", err)
}
if err := migrate(ctx, p); err != nil {
return nil, fmt.Errorf("schema 迁移失败: %w", err)
}
return p, nil
}
func migrate(ctx context.Context, p *pgxpool.Pool) error {
ddl := `
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 '未分类',
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 '',
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 ''
);
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
);
-- 函数类型(Kinetic 层 ⑤Function):可被 Action / Function / MCP 调用的纯计算或派生
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 '',
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now()
);
-- 接口类型(Semantic 层 ⑦Interface):声明式接口契约(API/MCP tool 签名 + 授权)
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,
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,
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,
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
);
-- 知识文件:archive 目录下的文件元数据,关联提取产生的 ingestion 记录
CREATE TABLE IF NOT EXISTS km_files (
id BIGSERIAL PRIMARY KEY,
rel_path TEXT NOT NULL UNIQUE, -- 相对 archive 根的路径,如 sub/foo.md
name TEXT NOT NULL,
folder TEXT NOT NULL DEFAULT '', -- 所属文件夹(相对根),空串表示根
size BIGINT DEFAULT 0,
mime TEXT DEFAULT '',
ingested BOOLEAN DEFAULT FALSE,
ingestion_id BIGINT, -- 关联 ingestions.id
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', -- db / api / file / other
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()
);
-- 兼容存量库:补 name 列并从 properties_json->>'name' 回填,保障连接器名称检索精准
-- (必须在建索引前执行,否则索引引用未存在的列会报错)
ALTER TABLE objects ADD COLUMN IF NOT EXISTS name TEXT DEFAULT '';
UPDATE objects SET name = COALESCE(properties_json->>'name','') WHERE name IS NULL OR name = '';
ALTER TABLE objects ADD COLUMN IF NOT EXISTS source TEXT DEFAULT '';
ALTER TABLE links ADD COLUMN IF NOT EXISTS source TEXT DEFAULT '';
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);
-- Dynamic Security 审计日志:每次访问决策的轨迹
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);
`
_, err := p.Exec(ctx, ddl)
return err
}
// seedIfEmpty 首次启动且本体为空时灌入极简种子数据(1 个对象类型 + 3 条示例)。
// 设计:让别人 docker run 一开箱就能看到 "合作企业" + "尚数网/腾讯/阿里巴巴" 三条最小演示,
// 也能在管理界面一键清掉,不污染业务本体。
func seedIfEmpty(ctx context.Context, p *pgxpool.Pool) error {
var n int
if err := p.QueryRow(ctx, "SELECT count(*) FROM object_types").Scan(&n); err != nil {
return err
}
if n > 0 {
log.Println("[seed] 本体已存在,跳过种子数据")
return nil
}
log.Println("[seed] 灌入尚数网初始本体数据 ...")
type seedProp struct {
APIName, DisplayName, DataType, Description string
Required bool
Enum []string
}
type seedType struct {
APIName, DisplayName, Description, Group, Icon string
Props []seedProp
}
types := []seedType{
// 极简种子 —— 仅 1 个对象类型 + 3 条示例,方便开箱即用又便于清理
{"MemberOrg", "合作企业", "组织机构(合作企业、会员单位、生态伙伴等)。", "组织", "🤝", []seedProp{
{"name", "名称", "string", "", true, nil},
{"role", "角色", "enum", "", false, []string{"自营", "合作伙伴", "客户", "供应商"}},
{"region", "区域", "string", "", false, nil},
}},
}
for _, t := range types {
var oid int64
if err := p.QueryRow(ctx,
"INSERT INTO object_types(api_name,display_name,description,icon,group_name) VALUES($1,$2,$3,$4,$5) RETURNING id",
t.APIName, t.DisplayName, t.Description, t.Icon, t.Group).Scan(&oid); err != nil {
return fmt.Errorf("插入对象类型 %s 失败: %w", t.APIName, err)
}
for _, pr := range t.Props {
enumJSON := "[]"
if len(pr.Enum) > 0 {
enumJSON = mustJSON(pr.Enum)
}
if _, err := p.Exec(ctx,
"INSERT INTO properties(object_type_id,api_name,display_name,data_type,required,enum_values,description) VALUES($1,$2,$3,$4,$5,$6,$7)",
oid, pr.APIName, pr.DisplayName, pr.DataType, pr.Required, enumJSON, pr.Description); err != nil {
return fmt.Errorf("插入属性 %s.%s 失败: %w", t.APIName, pr.APIName, err)
}
}
}
// 链接类型 — 极简种子不内置,留空让用户按需创建
linkTypes := []struct {
api, disp, src, tgt, card, desc string
}{}
for _, l := range linkTypes {
if _, err := p.Exec(ctx,
"INSERT INTO link_types(api_name,display_name,source_type,target_type,cardinality,description) VALUES($1,$2,$3,$4,$5,$6) ON CONFLICT(api_name) DO NOTHING",
l.api, l.disp, l.src, l.tgt, l.card, l.desc); err != nil {
return fmt.Errorf("插入链接类型 %s 失败: %w", l.api, err)
}
}
// 动作类型 — 极简种子不内置,留空让用户按需创建
actionTypes := []struct{ api, disp, desc, params string }{}
for _, a := range actionTypes {
if _, err := p.Exec(ctx,
"INSERT INTO action_types(api_name,display_name,description,params) VALUES($1,$2,$3,$4::jsonb) ON CONFLICT(api_name) DO NOTHING",
a.api, a.disp, a.desc, a.params); err != nil {
return fmt.Errorf("插入动作类型 %s 失败: %w", a.api, err)
}
}
// 对象实例 — 极简种子:3 条开箱示例,部署后可一键删干净
objs := []struct {
typ, pk string
props map[string]interface{}
}{
{"MemberOrg", "demo_shangshu", m("name", "尚数网", "role", "自营", "region", "深圳")},
{"MemberOrg", "demo_tencent", m("name", "腾讯", "role", "合作伙伴", "region", "深圳")},
{"MemberOrg", "demo_alibaba", m("name", "阿里巴巴", "role", "合作伙伴", "region", "杭州")},
}
for _, o := range objs {
if err := insertObjectRaw(ctx, p, o.typ, o.pk, o.props); err != nil {
return err
}
}
// 链接实例 — 极简种子无内置链接
links := []struct {
typ, st, spk, tt, tpk string
}{}
for _, l := range links {
if _, err := p.Exec(ctx,
"INSERT INTO links(link_type,source_type,source_pk,target_type,target_pk) VALUES($1,$2,$3,$4,$5) ON CONFLICT DO NOTHING",
l.typ, l.st, l.spk, l.tt, l.tpk); err != nil {
return fmt.Errorf("插入链接 %s 失败: %w", l.typ, err)
}
}
log.Println("[seed] 种子数据完成")
return nil
}
// insertObjectRaw 在 seed 阶段直接插入对象实例(按类型名+主键)。
func insertObjectRaw(ctx context.Context, p *pgxpool.Pool, typeName, pk string, props map[string]interface{}) error {
var oid int64
if err := p.QueryRow(ctx, "SELECT id FROM object_types WHERE api_name=$1", typeName).Scan(&oid); err != nil {
return fmt.Errorf("找不到对象类型 %s: %w", typeName, err)
}
propsJSON := mustJSON(props)
nameVal, _ := props["name"].(string)
_, err := p.Exec(ctx,
"INSERT INTO objects(object_type_id,type_api_name,pk,name,properties_json) VALUES($1,$2,$3,$4,$5::jsonb) ON CONFLICT(object_type_id,pk) DO UPDATE SET name=EXCLUDED.name, properties_json=EXCLUDED.properties_json, updated_at=now()",
oid, typeName, pk, nameVal, propsJSON)
if err != nil {
return fmt.Errorf("插入对象 %s/%s 失败: %w", typeName, pk, err)
}
return nil
}