Files
knowledge-governance/internal/releaseadapter/v071/library_resource_references.go
Zhero & Rin 73413c59ee
build / build (push) Successful in 2m3s
feat(library): 适配 WeKnora 0.9.1 并搬运编辑历史
- profile 新增 137(由 v090 克隆),声明知识域关联表 copied/derived/excluded 分类。
- 导出前与导入写事务内各跑漏表门禁;未声明有数据拒绝,空表警告写入 WriteSet。
- chunk_revisions/wiki_page_revisions 接入范围改写、目标身份映射、自然键与回滚顺序。
2026-11-30 18:58:01 +08:00

91 lines
3.8 KiB
Go

package v071
import (
"context"
"errors"
"fmt"
"strings"
"knowledge-governance/internal/libraryrelease"
)
// libraryResourceBodyTable 报告该表是否参与"正文引用资源"的发现与作用域。
//
// 编辑历史按人类裁定(Q2=B)原样保留 resource:// 文本,不参与资源发现:历史版本
// 引用的对象可能早已删除,不能让它决定本次发布必须携带哪些对象。资源发现、目标
// 资源作用域、恢复保护三处必须用同一个判定,否则导出与校验会各算一套集合。
func libraryResourceBodyTable(table string) bool {
switch table {
case "resources", "resource_bindings", "knowledge_tag_relations", "chunk_revisions", "wiki_page_revisions":
return false
}
return true
}
// Keep capture/verification and deletion protection on the same set of bodies.
// predicate is an internal SQL fragment, never caller-supplied content; tables comes
// from the resolved schema profile (0.9.0 has no wiki_log_entries to scan).
func libraryResourceBodyReferenceSQL(tables []string, predicate string) string {
queries := []string{}
for _, table := range tables {
if !libraryResourceBodyTable(table) {
continue
}
queries = append(queries, fmt.Sprintf(`SELECT 1 FROM %s c WHERE (%s) AND coalesce(resources.handle,'')<>'' AND position('resource://'||resources.handle IN to_jsonb(c)::text)>0`, table, predicate))
}
return strings.Join(queries, " UNION ALL ")
}
// This must precede *all* restore mutations, including ES and object deletes.
// A created row is not permission to invalidate a later out-of-plan reader.
func ensureRestorableResourceReferences(ctx context.Context, q libraryClosureReader, tables []string, set libraryrelease.WriteSet) error {
ids := set.CreatedIDs("resources")
if len(ids) == 0 {
return nil
}
if e := verifyLibraryResourceOwners(ctx, q, ids); e != nil {
return e
}
bindings := []string{}
for _, row := range set.Rows {
if row.Table == "resource_bindings" {
bindings = append(bindings, row.ID)
}
}
var referenced bool
err := q.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM resource_bindings b WHERE b.resource_id=ANY($1::text[]) AND
(NOT(b.id=ANY($2::text[])) OR b.tenant_id IS DISTINCT FROM $3 OR NOT (
(b.owner_type='knowledge' AND EXISTS(SELECT 1 FROM knowledges k WHERE k.id=b.owner_id AND k.tenant_id=$3 AND k.knowledge_base_id=$4)) OR
(b.owner_type='chunk' AND EXISTS(SELECT 1 FROM chunks c WHERE c.id=b.owner_id AND c.tenant_id=$3 AND c.knowledge_base_id=$4)))))`, ids, bindings, set.TenantID, set.KnowledgeBaseID).Scan(&referenced)
if err != nil {
return err
}
if referenced {
return errors.New("created resource has out-of-plan bindings; restore held before writes")
}
bodySQL := libraryResourceBodyReferenceSQL(tables, "c.tenant_id IS DISTINCT FROM $2 OR c.knowledge_base_id IS DISTINCT FROM $3")
err = q.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM resources WHERE id=ANY($1::text[]) AND EXISTS (`+bodySQL+`))`, ids, set.TenantID, set.KnowledgeBaseID).Scan(&referenced)
if err != nil {
return err
}
if referenced {
return errors.New("created resource has foreign body/metadata references; restore held before writes")
}
return nil
}
// Recheck under the already-held resource parent locks. This does not claim
// cross-store isolation from uncontrolled writers; deployed exclusivity is separate.
func ensureCreatedResourcesUnreferenced(ctx context.Context, q libraryClosureReader, tables, ids []string) error {
var referenced bool
err := q.QueryRow(ctx, `SELECT EXISTS(SELECT 1 FROM resources WHERE id=ANY($1::text[]) AND
(EXISTS(SELECT 1 FROM resource_bindings b WHERE b.resource_id=resources.id) OR EXISTS (`+libraryResourceBodyReferenceSQL(tables, "TRUE")+`)))`, ids).Scan(&referenced)
if err != nil {
return err
}
if referenced {
return errors.New("created resource is still referenced; refusing row deletion")
}
return nil
}