fba2713b4e
build / build (push) Successful in 1m54s
整库发布写入路径原先按 0.7.1 的固定表集合工作:0.9.0 没有 wiki_log_entries, Current/清理/编目/整实例指纹都会在不存在表上失败,跨版本闸门也因快照丢掉 SchemaVersion 而失效(未知版本被当作 74)。 改为在装配时解析目标 profile,表集合与清理顺序按解析结果;快照复制与范围改写 带上 SchemaVersion/Profile;跨版本发布显式拒绝并同时点名两个版本。单文档发布包 仍只支持 0.7.1,但改为明确拒绝并说明原因。 本机隔离验证:真实 0.9.0 schema 源库→导出→封存→写入干净 134 目标→逐表核对→ 写前镜像回滚;74→134 拒绝与 134 缺表不报错两个负例通过。make verify 17 项全绿。
152 lines
6.2 KiB
Go
152 lines
6.2 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"knowledge-governance/internal/libraryprofile"
|
|
"knowledge-governance/internal/librarysnapshot"
|
|
)
|
|
|
|
type libraryClosureReader interface {
|
|
QueryRow(context.Context, string, ...any) pgx.Row
|
|
}
|
|
|
|
// Check edges before JOIN-based export can erase a scoped orphan. Either parent
|
|
// can establish scope; an edge is not safe merely because its other parent is absent.
|
|
func verifyLibraryRelationalClosure(ctx context.Context, db libraryClosureReader, identity PostgresIdentity, includeDeleted bool) error {
|
|
var invalid bool
|
|
err := db.QueryRow(ctx, `/* library-relational-closure */ SELECT
|
|
EXISTS(SELECT 1 FROM chunks c LEFT JOIN knowledges k ON k.id=c.knowledge_id
|
|
WHERE c.tenant_id=$1 AND c.knowledge_base_id=$2 AND ($3 OR c.deleted_at IS NULL)
|
|
AND (k.id IS NULL OR k.tenant_id IS DISTINCT FROM c.tenant_id OR k.knowledge_base_id IS DISTINCT FROM c.knowledge_base_id OR (NOT $3 AND k.deleted_at IS NOT NULL)))
|
|
OR EXISTS(SELECT 1 FROM knowledge_tag_relations r LEFT JOIN knowledges k ON k.id=r.knowledge_id LEFT JOIN knowledge_tags t ON t.id=r.tag_id
|
|
WHERE ((k.tenant_id=$1 AND k.knowledge_base_id=$2 AND ($3 OR k.deleted_at IS NULL)) OR (t.tenant_id=$1 AND t.knowledge_base_id=$2 AND ($3 OR t.deleted_at IS NULL)))
|
|
AND (k.id IS NULL OR t.id IS NULL OR k.tenant_id IS DISTINCT FROM t.tenant_id OR k.knowledge_base_id IS DISTINCT FROM t.knowledge_base_id OR (NOT $3 AND (k.deleted_at IS NOT NULL OR t.deleted_at IS NOT NULL))))
|
|
OR EXISTS(SELECT 1 FROM resource_bindings b WHERE b.tenant_id=$1
|
|
AND (EXISTS(SELECT 1 FROM knowledges k WHERE k.id=b.owner_id AND k.tenant_id=$1 AND k.knowledge_base_id=$2 AND ($3 OR k.deleted_at IS NULL)) OR EXISTS(SELECT 1 FROM chunks c WHERE c.id=b.owner_id AND c.tenant_id=$1 AND c.knowledge_base_id=$2 AND ($3 OR c.deleted_at IS NULL)))
|
|
AND (b.owner_type NOT IN ('knowledge','chunk') OR b.owner_type IS NULL))`, identity.TenantID, identity.KnowledgeBaseID, includeDeleted).Scan(&invalid)
|
|
if err != nil {
|
|
return fmt.Errorf("read library reference closure: %w", err)
|
|
}
|
|
if invalid {
|
|
return errors.New("unresolved or cross-parent library relationship")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func verifyLibraryResourceOwners(ctx context.Context, db libraryClosureReader, ids []string) error {
|
|
var invalid bool
|
|
err := db.QueryRow(ctx, `/* library-resource-owner-closure */ SELECT EXISTS(
|
|
SELECT 1 FROM resource_bindings b JOIN resources r ON r.id=b.resource_id WHERE r.id=ANY($1::text[]) AND (
|
|
b.tenant_id IS DISTINCT FROM r.tenant_id OR b.owner_type IS NULL OR NOT (
|
|
(b.owner_type='knowledge' AND EXISTS(SELECT 1 FROM knowledges k WHERE k.id=b.owner_id AND k.tenant_id=b.tenant_id)) OR
|
|
(b.owner_type='chunk' AND EXISTS(SELECT 1 FROM chunks c JOIN knowledges k ON k.id=c.knowledge_id AND k.tenant_id=c.tenant_id AND k.knowledge_base_id=c.knowledge_base_id WHERE c.id=b.owner_id AND c.tenant_id=b.tenant_id)))))`, ids).Scan(&invalid)
|
|
if err != nil {
|
|
return fmt.Errorf("read referenced resource owners: %w", err)
|
|
}
|
|
if invalid {
|
|
return errors.New("referenced resource has unknown or unresolved owner")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
var libraryResourceToken = regexp.MustCompile(`resource://([A-Za-z0-9_-]*)`)
|
|
|
|
// Decode JSON before scanning string leaves (including nested metadata). The
|
|
// v071 reader accepts exactly 22 URL-safe characters, not URI query/path suffixes.
|
|
// Opaque escaped/encoded representations are refused rather than silently omitted.
|
|
func libraryResourceHandles(rows []json.RawMessage) ([]string, error) {
|
|
seen := map[string]bool{}
|
|
var visit func(any) error
|
|
visit = func(value any) error {
|
|
switch v := value.(type) {
|
|
case map[string]any:
|
|
for _, x := range v {
|
|
if e := visit(x); e != nil {
|
|
return e
|
|
}
|
|
}
|
|
case []any:
|
|
for _, x := range v {
|
|
if e := visit(x); e != nil {
|
|
return e
|
|
}
|
|
}
|
|
case string:
|
|
rest := v
|
|
for strings.Contains(rest, "resource:") {
|
|
start := strings.Index(rest, "resource:")
|
|
rest = rest[start:]
|
|
match := libraryResourceToken.FindStringSubmatchIndex(rest)
|
|
if match == nil || match[0] != 0 || match[3]-match[2] != 22 {
|
|
return errors.New("unsupported resource reference representation")
|
|
}
|
|
if match[1] < len(rest) && strings.ContainsRune("/?#=%\\", rune(rest[match[1]])) {
|
|
return errors.New("unsupported resource reference suffix")
|
|
}
|
|
seen[rest[match[2]:match[3]]] = true
|
|
rest = rest[match[1]:]
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
for _, raw := range rows {
|
|
var value any
|
|
decoder := json.NewDecoder(strings.NewReader(string(raw)))
|
|
decoder.UseNumber()
|
|
if e := decoder.Decode(&value); e != nil {
|
|
return nil, e
|
|
}
|
|
if e := visit(value); e != nil {
|
|
return nil, e
|
|
}
|
|
}
|
|
handles := make([]string, 0, len(seen))
|
|
for handle := range seen {
|
|
handles = append(handles, handle)
|
|
}
|
|
sort.Strings(handles)
|
|
return handles, nil
|
|
}
|
|
|
|
// Revalidate frozen source content at the actual planning boundary. A sealed
|
|
// manifest proves bytes, not that every row has a parent or a supported owner.
|
|
func validateArchivedLibraryClosure(archive *librarysnapshot.Archive, manifest librarysnapshot.Manifest) error {
|
|
// 快照的 schema 契约来自 manifest:不带上版本号,重建出来的快照就是"版本未知",
|
|
// 而未知版本按 0.7.1 校验——0.9.0 的归档会被要求提供它根本没有的 wiki_log_entries。
|
|
pg := PostgresSnapshot{SchemaVersion: manifest.Compatibility.SchemaVersion}
|
|
if profile, err := libraryprofile.For(pg.SchemaVersion); err == nil {
|
|
pg.Profile = profile.Name
|
|
}
|
|
es := SearchSnapshot{MappingSHA256: manifest.Compatibility.IndexMappingSHA256}
|
|
for _, part := range manifest.Parts {
|
|
if part.Kind != "database" && part.Kind != "wiki" && part.Kind != "index" {
|
|
continue
|
|
}
|
|
reader, e := archive.OpenPart(part)
|
|
if e != nil {
|
|
return e
|
|
}
|
|
if part.Kind == "index" {
|
|
e = json.NewDecoder(reader).Decode(&es.Documents)
|
|
} else {
|
|
var rows []json.RawMessage
|
|
e = json.NewDecoder(reader).Decode(&rows)
|
|
pg.Tables = append(pg.Tables, newTableSnapshot(part.Name, rows))
|
|
}
|
|
reader.Close()
|
|
if e != nil {
|
|
return e
|
|
}
|
|
}
|
|
es.DocumentsSHA256, _ = searchDocumentsDigest(es.Documents)
|
|
return validateLibraryCandidate(PostgresIdentity{TenantID: manifest.TenantID, KnowledgeBaseID: manifest.KnowledgeBaseID}, pg, es)
|
|
}
|