0d9091e464
build / build (push) Successful in 1m41s
空 mode 计划不映射;索引 source_id 按语义映射;relation 按字段表解析。 隔离回归按持久映射 ID 断言,DS02 故障注入真实命中。
189 lines
8.4 KiB
Go
189 lines
8.4 KiB
Go
package v071
|
|
|
|
import (
|
|
"encoding/json"
|
|
"knowledge-governance/internal/libraryrelease"
|
|
"knowledge-governance/internal/librarysnapshot"
|
|
"os"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestR11CatalogBindingIsolated(t *testing.T) {
|
|
for _, mode := range []string{"matching", "default", "unique-predicate", "sequence-definition", "sequence-ownership", "incoming-fk", "outgoing-fk", "internal-local-fk", "generated", "trigger"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
fx := openWriteSetFixtureWith(t, writeSetFixtureOptions{reset: true, embeddingDimension: 1024})
|
|
seedTargetData(t, fx)
|
|
r11Mirror(t, fx)
|
|
target := fx.target(t)
|
|
pg, e := ExportLibrary(fx.ctx, fx.pool, PostgresIdentity{TenantID: wsTargetTenant, KnowledgeBaseID: wsTargetKB})
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
catalog, digest, e := target.migrationCatalog(fx.ctx)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if !reflect.DeepEqual(catalog, pg.Catalog) {
|
|
t.Fatal("captured source and target catalogs differ")
|
|
}
|
|
expected := r11CatalogData(t)
|
|
var cols []r11Column
|
|
if e = json.Unmarshal(catalog["columns"], &cols); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if !reflect.DeepEqual(cols, expected.Columns) {
|
|
t.Fatal("real v071 columns/defaults/types differ from reconstructed target")
|
|
}
|
|
var indexes []map[string]any
|
|
json.Unmarshal(catalog["indexes"], &indexes)
|
|
if len(indexes) != len(expected.Indexes) {
|
|
t.Fatal("missing actual indexes")
|
|
}
|
|
expectedRaw, e := os.ReadFile("testdata/catalog-v071-r11.json")
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
var original map[string]json.RawMessage
|
|
json.Unmarshal(expectedRaw, &original)
|
|
for _, kind := range []string{"columns", "constraints", "indexes", "triggers", "relations"} {
|
|
var a, b any
|
|
json.Unmarshal(original[kind], &a)
|
|
json.Unmarshal(catalog[kind], &b)
|
|
if !reflect.DeepEqual(a, b) {
|
|
t.Fatal("catalog mismatch", kind)
|
|
}
|
|
}
|
|
r10Save(t, "source-catalog", pg.Catalog)
|
|
r10Save(t, "schema-digest", digest)
|
|
es, e := target.Search().ExportLibrary(fx.ctx, SearchIdentity{TenantID: wsTargetTenant, KnowledgeBaseID: wsTargetKB}, 100)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
archive, e := librarysnapshot.Open(fx.archiveRoot, 128*1024*1024)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
defer archive.Close()
|
|
compatibility := librarysnapshot.Compatibility{WeKnoraVersion: "v0.7.1", SchemaVersion: 74, RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "text-embedding-v4", EmbeddingDimension: 1024, IndexMappingSHA256: es.MappingSHA256}
|
|
snapshot, e := ArchiveLibraryCandidate(fx.ctx, archive, "internal", PostgresIdentity{TenantID: wsTargetTenant, KnowledgeBaseID: wsTargetKB}, compatibility, pg, es, fx.objects, time.Now().UTC())
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if mode == "matching" {
|
|
before := r10State(t, fx)
|
|
manifest, e := archive.Load(snapshot)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
got, e := target.preflightMigrationCatalog(fx.ctx, archive, manifest)
|
|
if e != nil || got != digest {
|
|
t.Fatal(got, e)
|
|
}
|
|
r10Unchanged(t, fx, before)
|
|
return
|
|
}
|
|
statements := map[string]string{
|
|
"default": `ALTER TABLE chunks ALTER COLUMN flags SET DEFAULT 7`,
|
|
"unique-predicate": `DROP INDEX idx_wiki_pages_kb_slug;CREATE UNIQUE INDEX idx_wiki_pages_kb_slug ON wiki_pages(knowledge_base_id,slug) WHERE deleted_at IS NOT NULL`,
|
|
"sequence-definition": `ALTER SEQUENCE chunks_seq_id_seq INCREMENT 2`,
|
|
"sequence-ownership": `ALTER SEQUENCE chunks_seq_id_seq OWNED BY chunks.seq_id`,
|
|
"incoming-fk": `CREATE TABLE outside_cursor(log_id bigint REFERENCES wiki_log_entries(id))`,
|
|
"outgoing-fk": `CREATE TABLE outside_models(id varchar(64) PRIMARY KEY);ALTER TABLE knowledges ADD FOREIGN KEY(embedding_model_id) REFERENCES outside_models(id)`,
|
|
"internal-local-fk": `ALTER TABLE wiki_page_issues ADD COLUMN log_id bigint REFERENCES wiki_log_entries(id)`,
|
|
"generated": `ALTER TABLE wiki_pages ADD COLUMN lower_slug text GENERATED ALWAYS AS(lower(slug)) STORED`,
|
|
"trigger": `CREATE FUNCTION r11_noop() RETURNS trigger LANGUAGE plpgsql AS 'BEGIN RETURN NEW; END';CREATE TRIGGER r11_noop BEFORE INSERT ON wiki_pages FOR EACH ROW EXECUTE FUNCTION r11_noop()`,
|
|
}
|
|
r10Exec(t, fx, statements[mode])
|
|
r11RejectNatural(t, fx, snapshot)
|
|
// An identically extended source/target must not make an unreviewed FK safe.
|
|
if strings.Contains(mode, "fk") {
|
|
now, _, e := target.migrationCatalog(fx.ctx)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = validateMigrationCatalog(now); e == nil {
|
|
t.Fatal("unreviewed FK admitted when both sides match")
|
|
}
|
|
r10Save(t, "unknown-fk-rejection", e.Error())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
func TestR11ReferenceAndCursorIsolated(t *testing.T) {
|
|
for _, mode := range []string{"valid", "linked-valid", "legacy-pages", "historical-log", "chunk-parent", "chunk-pre", "chunk-next", "chunk-tag", "wiki-folder", "folder-parent", "folder-cycle", "wiki-source", "wiki-chunk", "log-object-id", "log-array-number"} {
|
|
t.Run(mode, func(t *testing.T) {
|
|
fx := openWriteSetFixture(t)
|
|
seedTargetData(t, fx)
|
|
r11Mirror(t, fx)
|
|
snapshot := r11Source(t, fx, func(p map[string][]map[string]any) {
|
|
switch mode {
|
|
case "linked-valid":
|
|
p["chunks"] = append(p["chunks"], map[string]any{"id": "chunk-src-2", "tenant_id": wsSourceTenant, "knowledge_base_id": wsSourceKB, "knowledge_id": "doc-src", "content": "second", "seq_id": 11, "pre_chunk_id": "chunk-src", "parent_chunk_id": "chunk-src"})
|
|
p["chunks"][0]["next_chunk_id"] = "chunk-src-2"
|
|
p["chunks"][0]["tag_id"] = "tag-src"
|
|
p["wiki_pages"][0]["source_refs"] = []string{"doc-src|Source title"}
|
|
p["wiki_pages"][0]["chunk_refs"] = []string{"chunk-src-2"}
|
|
p["wiki_pages"][0]["page_metadata"] = map[string]any{"literal_number": 100, "tags": []string{"literal"}}
|
|
case "legacy-pages":
|
|
p["wiki_log_entries"][0]["pages_affected"] = []string{"index", "historic-removed-page"}
|
|
case "historical-log":
|
|
p["wiki_log_entries"][0]["knowledge_id"] = "deleted-knowledge"
|
|
p["wiki_log_entries"][0]["pages_affected"] = []any{map[string]any{"slug": "deleted-page", "title": "historic"}}
|
|
case "chunk-parent":
|
|
p["chunks"][0]["parent_chunk_id"] = "chunk-other"
|
|
case "chunk-pre":
|
|
p["chunks"][0]["pre_chunk_id"] = "chunk-other"
|
|
case "chunk-next":
|
|
p["chunks"][0]["next_chunk_id"] = "chunk-other"
|
|
case "chunk-tag":
|
|
p["chunks"][0]["tag_id"] = "tag-outside"
|
|
case "wiki-folder":
|
|
p["wiki_pages"][0]["folder_id"] = "foreign-folder"
|
|
case "folder-parent":
|
|
p["wiki_folders"][0]["parent_id"] = "foreign-folder"
|
|
case "folder-cycle":
|
|
p["wiki_folders"][0]["parent_id"] = "folder-src"
|
|
case "wiki-source":
|
|
p["wiki_pages"][0]["source_refs"] = []string{"doc-other"}
|
|
case "wiki-chunk":
|
|
p["wiki_pages"][0]["chunk_refs"] = []string{"chunk-other"}
|
|
case "log-object-id":
|
|
p["wiki_log_entries"][0]["pages_affected"] = []any{map[string]any{"slug": "index", "log_id": 100}}
|
|
case "log-array-number":
|
|
p["wiki_log_entries"][0]["pages_affected"] = []int{100}
|
|
}
|
|
})
|
|
if mode != "valid" && mode != "linked-valid" && mode != "legacy-pages" && mode != "historical-log" {
|
|
r11RejectNatural(t, fx, snapshot)
|
|
return
|
|
}
|
|
target := fx.target(t)
|
|
out := libraryrelease.Run(fx.ctx, requestFor(snapshot), target, fx.backups(t, target), nil)
|
|
if out.State != "succeeded" {
|
|
t.Fatal(out)
|
|
}
|
|
var actual int64
|
|
if e := fx.pool.QueryRow(fx.ctx, `SELECT id FROM wiki_log_entries WHERE knowledge_base_id=$1`, wsTargetKB).Scan(&actual); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
var sourceCursor, validCursor int
|
|
if e := fx.pool.QueryRow(fx.ctx, `SELECT count(*) FROM wiki_log_entries WHERE knowledge_base_id=$1 AND id<100`, wsTargetKB).Scan(&sourceCursor); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := fx.pool.QueryRow(fx.ctx, `SELECT count(*) FROM wiki_log_entries WHERE knowledge_base_id=$1 AND id<$2`, wsTargetKB, actual).Scan(&validCursor); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if actual == 100 || sourceCursor == validCursor {
|
|
t.Fatal("expected evidence of DB-local old cursor not transferable")
|
|
}
|
|
r10Save(t, "cursor-boundary", map[string]any{"source_cursor": 100, "mapped_id": actual, "source_cursor_count": sourceCursor, "mapped_cursor_count": validCursor, "policy": "fresh target cursor works; cross-source or pre-replace external cursor is not automatically translated"})
|
|
if e := target.Restore(fx.ctx, out.Backup); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
})
|
|
}
|
|
}
|