package v071 import ( "crypto/sha256" "encoding/hex" "encoding/json" "fmt" "os" "reflect" "sort" "strings" "testing" "github.com/jackc/pgx/v5" "knowledge-governance/internal/libraryrelease" ) type r11Column struct { Table string `json:"table_name"` Name string `json:"column_name"` Type string `json:"type"` Default string `json:"default_expr"` Required bool `json:"not_null"` } type r11Catalog struct { Columns []r11Column `json:"columns"` Constraints []struct { Table string `json:"table_name"` Name string `json:"name"` Definition string `json:"definition"` } `json:"constraints"` Indexes []struct { Name string `json:"name"` Definition string `json:"definition"` } `json:"indexes"` Sequences []struct { Name string `json:"sequence"` Start int64 `json:"seqstart"` } `json:"sequences"` } func r11CatalogData(t *testing.T) r11Catalog { t.Helper() raw, e := os.ReadFile("testdata/catalog-v071-r11.json") if e != nil { t.Fatal(e) } var c r11Catalog if e = json.Unmarshal(raw, &c); e != nil { t.Fatal(e) } return c } func r11Fill(t *testing.T, fx *writeSetFixture, table string, row map[string]any) map[string]any { t.Helper() if table == "resources" && row["location_hash"] == nil { sum := sha256.Sum256([]byte(fmt.Sprint(row["physical_path"]))) row["location_hash"] = hex.EncodeToString(sum[:]) } for _, c := range r11CatalogData(t).Columns { if c.Table != table { continue } if v, ok := row[c.Name]; ok && (v != nil || !c.Required) { continue } expr := c.Default if c.Name == "seq_id" { expr = "10" } if table == "wiki_log_entries" && c.Name == "id" { expr = "100" } if expr == "" { if !c.Required { row[c.Name] = nil continue } switch c.Type { case "bigint", "integer": expr = "0" case "boolean": expr = "false" default: expr = "''" } } var raw []byte if e := fx.pool.QueryRow(fx.ctx, "SELECT to_jsonb(("+expr+")::"+c.Type+")").Scan(&raw); e != nil { t.Fatal(table, c.Name, e) } var v any dec := json.NewDecoder(strings.NewReader(string(raw))) dec.UseNumber() if e := dec.Decode(&v); e != nil { t.Fatal(e) } row[c.Name] = v } raw, e := json.Marshal(row) if e != nil { t.Fatal(e) } var canonical []byte if e = fx.pool.QueryRow(fx.ctx, "SELECT to_jsonb(jsonb_populate_record(NULL::"+table+",$1::jsonb))", raw).Scan(&canonical); e != nil { t.Fatal(e) } dec := json.NewDecoder(strings.NewReader(string(canonical))) dec.UseNumber() if e = dec.Decode(&row); e != nil { t.Fatal(e) } return row } func r11Insert(t *testing.T, fx *writeSetFixture, table string, row map[string]any) { t.Helper() r11Fill(t, fx, table, row) keys := []string{} for k := range row { keys = append(keys, k) } sort.Strings(keys) cols := []string{} for _, k := range keys { cols = append(cols, pgx.Identifier{k}.Sanitize()) } raw, e := json.Marshal(row) if e != nil { t.Fatal(e) } if _, e = fx.pool.Exec(fx.ctx, "INSERT INTO "+table+"("+strings.Join(cols, ",")+") SELECT "+strings.Join(cols, ",")+" FROM jsonb_populate_record(NULL::"+table+",$1::jsonb)", raw); e != nil { t.Fatal(table, e) } } // Reconstruct only the ten migration tables from preserved real v071 catalog. // The grant table is an inbound-FK fixture, not a claimed whole business mirror. func r11Mirror(t *testing.T, fx *writeSetFixture) { t.Helper() old := universeDump(t, fx.pool) c := r11CatalogData(t) r10Exec(t, fx, `CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; CREATE EXTENSION IF NOT EXISTS pg_trgm; DROP TABLE IF EXISTS resource_access_grants CASCADE; DROP TABLE `+strings.Join(productionLibraryTables, ",")+` CASCADE`) for _, s := range c.Sequences { r10Exec(t, fx, fmt.Sprintf("CREATE SEQUENCE %s START %d", s.Name, s.Start)) } for _, table := range productionLibraryTables { defs := []string{} for _, col := range c.Columns { if col.Table != table { continue } d := pgx.Identifier{col.Name}.Sanitize() + " " + col.Type if col.Required { d += " NOT NULL" } if col.Default != "" { d += " DEFAULT " + col.Default } defs = append(defs, d) } r10Exec(t, fx, "CREATE TABLE "+table+"("+strings.Join(defs, ",")+")") } r10Exec(t, fx, `ALTER SEQUENCE wiki_log_entries_id_seq OWNED BY wiki_log_entries.id; CREATE TABLE resource_access_grants(id text,resource_id varchar(36))`) names := map[string]bool{} sort.SliceStable(c.Constraints, func(i, j int) bool { return !strings.HasPrefix(c.Constraints[i].Definition, "FOREIGN KEY") && strings.HasPrefix(c.Constraints[j].Definition, "FOREIGN KEY") }) for _, constraint := range c.Constraints { r10Exec(t, fx, "ALTER TABLE "+constraint.Table+" ADD CONSTRAINT "+constraint.Name+" "+constraint.Definition) names[constraint.Name] = true } for _, index := range c.Indexes { if !names[index.Name] { r10Exec(t, fx, index.Definition) } } n := int64(100000000) for _, table := range []string{"knowledges", "chunks", "knowledge_tags", "knowledge_tag_relations", "resources", "resource_bindings", "wiki_folders", "wiki_pages", "wiki_page_issues", "wiki_log_entries"} { for _, raw := range old[table] { row := map[string]any{} dec := json.NewDecoder(strings.NewReader(raw)) dec.UseNumber() if e := dec.Decode(&row); e != nil { t.Fatal(e) } allowed := map[string]bool{} for _, col := range c.Columns { if col.Table == table { allowed[col.Name] = true } } for k := range row { if !allowed[k] { delete(row, k) } } if table == "chunks" || table == "knowledge_tags" { n++ row["seq_id"] = n } r11Insert(t, fx, table, row) } } r10Exec(t, fx, fmt.Sprintf("SELECT setval('chunks_seq_id_seq',%d,true);SELECT setval('knowledge_tags_seq_id_seq',%d,true)", n, n)) } func r11Source(t *testing.T, fx *writeSetFixture, tweak func(map[string][]map[string]any)) string { return sealSourceSnapshot(t, fx, func(p map[string][]map[string]any) { p["knowledge_tags"] = []map[string]any{{"id": "tag-src", "tenant_id": wsSourceTenant, "knowledge_base_id": wsSourceKB, "name": "source-tag"}} p["knowledge_tag_relations"] = []map[string]any{{"knowledge_id": "doc-src", "tag_id": "tag-src"}} p["wiki_folders"] = []map[string]any{{"id": "folder-src", "tenant_id": wsSourceTenant, "knowledge_base_id": wsSourceKB, "name": "folder", "parent_id": ""}} p["wiki_pages"][0]["folder_id"] = "folder-src" p["wiki_page_issues"] = []map[string]any{{"id": "issue-src", "tenant_id": wsSourceTenant, "knowledge_base_id": wsSourceKB, "slug": "index", "issue_type": "review", "reported_by": "fixture", "description": "example"}} p["wiki_log_entries"] = []map[string]any{{"id": 100, "tenant_id": wsSourceTenant, "knowledge_base_id": wsSourceKB, "action": "ingest", "knowledge_id": "doc-src", "pages_affected": []any{map[string]any{"slug": "index", "title": "historic title"}}}} if tweak != nil { tweak(p) } for table, rows := range p { for _, row := range rows { r11Fill(t, fx, table, row) } } }) } func r11RejectNatural(t *testing.T, fx *writeSetFixture, snapshot string) { t.Helper() before := r10State(t, fx) target := fx.target(t) out := libraryrelease.Run(fx.ctx, requestFor(snapshot), target, fx.backups(t, target), nil) r10Save(t, "outcome", out) if out.State != "failed" || out.FailedAt != "" { t.Errorf("expected pre-stage rejection: %+v", out) } r10Unchanged(t, fx, before) } func r11Reject(t *testing.T, fx *writeSetFixture, snapshot, table, sourceID string) { t.Helper() before := r10State(t, fx) var beforeRow string if e := fx.pool.QueryRow(fx.ctx, `SELECT to_jsonb(x)::text FROM `+table+` AS x WHERE id=$1`, sourceID).Scan(&beforeRow); e != nil { t.Fatalf("read foreign row before publish: %v", e) } beforeState := map[string]string{"row:" + table: beforeRow} target := fx.target(t) out := libraryrelease.Run(fx.ctx, requestFor(snapshot), target, fx.backups(t, target), nil) r10Save(t, "outcome", out) if table == "resources" { // 资源是共享字节,不参与 UUID 映射:同 ID 属于他库必须继续拒绝。 if out.State != "failed" || out.FailedAt != "" { t.Errorf("shared resource id collision must be rejected pre-stage: %+v", out) } r10Unchanged(t, fx, before) return } if out.State != "succeeded" { t.Fatalf("mapped identity publish should succeed: %+v", out) } // 旧库原行必须保留,且计划里不得再出现源 ID。 for _, row := range out.Backup.WriteSet.Rows { if row.Table == table && row.ID == sourceID { t.Fatalf("writeset still carries source identity %s/%s", table, sourceID) } } mapped := expectedTargetID(wsTargetTenant, wsTargetKB, table, sourceID) var foreign, targetRows int if e := fx.pool.QueryRow(fx.ctx, `SELECT count(*) FROM `+table+` WHERE id=$1`, sourceID).Scan(&foreign); e != nil || foreign != 1 { t.Fatalf("foreign row %s/%s changed or lost: %d %v", table, sourceID, foreign, e) } // 旧库行必须逐列字节不变(不只是"还在")。 beforeRow, afterRow := "", "" if e := fx.pool.QueryRow(fx.ctx, `SELECT to_jsonb(x)::text FROM `+table+` AS x WHERE id=$1`, sourceID).Scan(&afterRow); e != nil { t.Fatalf("read foreign row %s: %v", table, e) } beforeRow = beforeState["row:"+table] if beforeRow != "" && beforeRow != afterRow { t.Fatalf("foreign row %s/%s content changed", table, sourceID) } if e := fx.pool.QueryRow(fx.ctx, `SELECT count(*) FROM `+table+` WHERE id=$1`, mapped).Scan(&targetRows); e != nil || targetRows != 1 { t.Fatalf("mapped row %s/%s missing: %d %v", table, mapped, targetRows, e) } if tablesWithKB[table] { var tenant int64 var kb string if e := fx.pool.QueryRow(fx.ctx, `SELECT tenant_id, knowledge_base_id FROM `+table+` WHERE id=$1`, mapped).Scan(&tenant, &kb); e != nil || tenant != int64(wsTargetTenant) || kb != wsTargetKB { t.Fatalf("mapped row %s landed at %d/%s", table, tenant, kb) } } } func TestR11StableIdentityMatrixIsolated(t *testing.T) { for _, table := range []string{"knowledges", "chunks", "knowledge_tags", "resources", "resource_bindings", "wiki_folders", "wiki_pages", "wiki_page_issues"} { for _, foreign := range []string{"same-tenant-other-kb", "other-tenant"} { t.Run(table+"/"+foreign, func(t *testing.T) { fx := openWriteSetFixture(t) seedTargetData(t, fx) r11Mirror(t, fx) tenant := uint64(wsTargetTenant) kb := wsOtherKB if foreign == "other-tenant" { tenant = 10003 kb = "kb-alien" } id := map[string]string{"knowledges": "doc-src", "chunks": "chunk-src", "knowledge_tags": "tag-src", "resources": "res-src", "resource_bindings": "bind-src", "wiki_folders": "folder-src", "wiki_pages": "wiki-src", "wiki_page_issues": "issue-src"}[table] row := map[string]any{"id": id, "tenant_id": tenant, "knowledge_base_id": kb} switch table { case "chunks": row["knowledge_id"] = "doc-other" case "resources": delete(row, "knowledge_base_id") row["handle"] = strings.Repeat("x", 22) row["provider"] = "minio" row["physical_path"] = "outside" row["location_hash"] = "outside" case "resource_bindings": delete(row, "knowledge_base_id") row["resource_id"] = "res-other" row["owner_type"] = "knowledge" row["owner_id"] = "doc-other" row["relation"] = "preview" case "wiki_pages", "wiki_page_issues": row["slug"] = "foreign" } r11Insert(t, fx, table, row) snapshot := r11Source(t, fx, nil) r11Reject(t, fx, snapshot, table, id) }) } } } func TestR11NaturalKeyMatrixIsolated(t *testing.T) { for _, mode := range []string{"valid", "tag-name", "tag-name-soft-deleted", "binding-tuple", "folder-name", "folder-deleted-positive", "folder-parent-distinct-positive", "page-slug", "page-deleted-positive", "resource-handle", "resource-location", "resource-location-deleted-positive", "physical-key"} { t.Run(mode, func(t *testing.T) { fx := openWriteSetFixture(t) seedTargetData(t, fx) r11Mirror(t, fx) if mode == "resource-handle" { r10Exec(t, fx, `UPDATE resources SET handle='`+wsHandleSource+`' WHERE id='res-other'`) } if strings.HasPrefix(mode, "resource-location") { path := wsLocator("backend-target", "minio", fx.bucket, wsTargetTenant, "res-src") sum := sha256.Sum256([]byte(path)) deleted := "NULL" if strings.HasSuffix(mode, "positive") { deleted = "now()" } r10Exec(t, fx, `UPDATE resources SET location_hash='`+hex.EncodeToString(sum[:])+`',deleted_at=`+deleted+` WHERE id='res-other'`) } if mode == "physical-key" { if e := fx.objects.s3.Put(fx.ctx, "minio", wsLocator("backend-target", "minio", fx.bucket, wsTargetTenant, "res-src"), strings.NewReader("foreign-data"), 12); e != nil { t.Fatal(e) } } snapshot := r11Source(t, fx, func(p map[string][]map[string]any) { table := "" switch mode { case "tag-name", "tag-name-soft-deleted": table = "knowledge_tags" case "binding-tuple": table = "resource_bindings" case "folder-name", "folder-deleted-positive", "folder-parent-distinct-positive": table = "wiki_folders" case "page-slug", "page-deleted-positive": table = "wiki_pages" } if table == "" { return } cp := map[string]any{} for k, v := range p[table][0] { cp[k] = v } cp["id"] = cp["id"].(string) + "-second" if table == "knowledge_tags" { cp["seq_id"] = 11 } if strings.Contains(mode, "deleted") { cp["deleted_at"] = "2026-01-01T00:00:00Z" } if mode == "folder-parent-distinct-positive" { cp["parent_id"] = "folder-src" } p[table] = append(p[table], cp) }) if mode != "valid" && !strings.HasSuffix(mode, "positive") { r11RejectNatural(t, fx, snapshot) return } before := r10State(t, fx) target := fx.target(t) out := libraryrelease.Run(fx.ctx, requestFor(snapshot), target, fx.backups(t, target), nil) r10Save(t, "outcome", out) if out.State != "succeeded" { t.Fatalf("normal admitted data must succeed: %+v", out) } if e := target.Restore(fx.ctx, out.Backup); e != nil { t.Fatal(e) } after := r10State(t, fx) for _, key := range []string{"pg", "versions", "delete_markers"} { if !reflect.DeepEqual(before[key], after[key]) { t.Fatal("restore changed", key) } } r10Save(t, "restored", after) }) } }