3f2626f621
build / build (push) Failing after 1m32s
按 schema 版本选 profile 并以 information_schema 能力探测放行,取代写死的 version==74;未知版本、脏库、缺表缺列、跨版本搬运一律拒绝并点名原因。 0.9.0 的索引名改从部署配置解析,wiki_log_entries 按 profile 变为可选, 归档部件集合随之一致。
183 lines
6.4 KiB
Go
183 lines
6.4 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"knowledge-governance/internal/librarysnapshot"
|
|
)
|
|
|
|
// v090MappingDigest 读真实抓下来的 0.9.0 映射(KG_V090_MAPPING_JSON)。
|
|
func v090MappingDigest(t *testing.T) string {
|
|
t.Helper()
|
|
path := os.Getenv("KG_V090_MAPPING_JSON")
|
|
if path == "" {
|
|
t.Skip("requires KG_V090_MAPPING_JSON pointing at the captured 0.9.0 index mapping")
|
|
}
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
t.Fatalf("read KG_V090_MAPPING_JSON: %v", err)
|
|
}
|
|
digest, err := IndexMappingDigest(raw)
|
|
if err != nil {
|
|
t.Fatalf("mapping digest: %v", err)
|
|
}
|
|
return digest
|
|
}
|
|
|
|
// TestV090LibraryRoundTripIsolated 走完整 0.9.0 往返:
|
|
// 导出 → 封存(真实 manifest 校验)→ 导入干净 134 schema → 再导出逐行比对。
|
|
func TestV090LibraryRoundTripIsolated(t *testing.T) {
|
|
ctx := context.Background()
|
|
src := v090Pool(t, "KG_V090_SOURCE_DSN")
|
|
identity := seedV090(t, ctx, src)
|
|
mappingSHA := v090MappingDigest(t)
|
|
|
|
source, err := ExportLibrary(ctx, src, identity)
|
|
if err != nil {
|
|
t.Fatalf("export 0.9.0 library: %v", err)
|
|
}
|
|
if source.SchemaVersion != 134 || source.Profile != "v090" {
|
|
t.Fatalf("snapshot must record the 0.9.0 profile, got %d/%q", source.SchemaVersion, source.Profile)
|
|
}
|
|
// 0.9.0 没有 wiki_log_entries:不允许出现该表,也不允许空 Wiki 冒充通过。
|
|
if tablePresent(source, "wiki_log_entries") {
|
|
t.Fatal("0.9.0 export must not produce a wiki_log_entries table")
|
|
}
|
|
if source.Table("wiki_pages").RowCount < 1 || source.Table("wiki_folders").RowCount < 1 {
|
|
t.Fatalf("wiki parts must be non-empty: pages=%d folders=%d",
|
|
source.Table("wiki_pages").RowCount, source.Table("wiki_folders").RowCount)
|
|
}
|
|
if source.Table("chunks").RowCount < 2 || source.Table("knowledges").RowCount != 1 {
|
|
t.Fatalf("unexpected fixture shape: chunks=%d knowledges=%d",
|
|
source.Table("chunks").RowCount, source.Table("knowledges").RowCount)
|
|
}
|
|
|
|
es := v090IndexSnapshot(t, source, mappingSHA)
|
|
root := t.TempDir()
|
|
if err := os.Chmod(root, 0700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
archive, err := librarysnapshot.Open(root, 64<<20)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer archive.Close()
|
|
contract := librarysnapshot.Compatibility{
|
|
WeKnoraVersion: "v0.9.0", SchemaVersion: 134,
|
|
RetrieverEngine: "elasticsearch_v8", EmbeddingModel: "bge-m3",
|
|
EmbeddingDimension: 1024, IndexMappingSHA256: mappingSHA,
|
|
}
|
|
sealed, err := ArchiveLibraryCandidate(ctx, archive, "internal", identity, contract, source, es,
|
|
v090Objects{"fixture/kn-v090.bin": []byte("fixture-object-bytes")}, time.Now().UTC())
|
|
if err != nil {
|
|
t.Fatalf("seal 0.9.0 archive: %v", err)
|
|
}
|
|
manifest, err := archive.Load(sealed)
|
|
if err != nil {
|
|
t.Fatalf("load sealed manifest: %v", err)
|
|
}
|
|
if manifest.Compatibility.SchemaVersion != 134 || manifest.Compatibility.WeKnoraVersion != "v0.9.0" {
|
|
t.Fatalf("manifest must record the 0.9.0 profile, got %+v", manifest.Compatibility)
|
|
}
|
|
for _, part := range manifest.Parts {
|
|
if part.Kind == "wiki" && part.Name == "wiki_log_entries" {
|
|
t.Fatal("0.9.0 archive must not contain a wiki_log_entries part")
|
|
}
|
|
}
|
|
|
|
dst := v090Pool(t, "KG_V090_TARGET_DSN")
|
|
if err := seedV090Binding(t, ctx, dst); err != nil {
|
|
t.Fatalf("seed target binding: %v", err)
|
|
}
|
|
importer, err := NewPostgresImporter(dst)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
document := PostgresIdentity{TenantID: identity.TenantID, KnowledgeBaseID: identity.KnowledgeBaseID, KnowledgeID: v090Knowledge}
|
|
if err := importer.Replace(ctx, document, source); err != nil {
|
|
t.Fatalf("import 0.9.0 snapshot: %v", err)
|
|
}
|
|
reloaded, err := ExportLibrary(ctx, dst, identity)
|
|
if err != nil {
|
|
t.Fatalf("re-export imported library: %v", err)
|
|
}
|
|
compareSnapshotContent(t, source, reloaded)
|
|
|
|
// 0.9.0 新增的 NOT NULL 列在导入后必须仍是合法值,且 seq_id 由目标序列分配。
|
|
var defaults int
|
|
if err := dst.QueryRow(ctx, `SELECT count(*) FROM chunks
|
|
WHERE source_content IS NOT NULL AND content_revision IS NOT NULL
|
|
AND index_status IS NOT NULL AND context_header IS NOT NULL
|
|
AND last_editor_id IS NOT NULL AND seq_id IS NOT NULL`).Scan(&defaults); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if defaults != source.Table("chunks").RowCount {
|
|
t.Fatalf("0.9.0 NOT NULL columns must be satisfied on every imported chunk: got %d want %d",
|
|
defaults, source.Table("chunks").RowCount)
|
|
}
|
|
var distinctSeq int
|
|
if err := dst.QueryRow(ctx, `SELECT count(DISTINCT seq_id) FROM chunks`).Scan(&distinctSeq); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if distinctSeq != source.Table("chunks").RowCount {
|
|
t.Fatalf("target chunk seq_id must be assigned, got %d distinct", distinctSeq)
|
|
}
|
|
|
|
// 跨版本必须拒绝:0.7.1 归档不能落到 0.9.0 目标。
|
|
crossVersion := source
|
|
crossVersion.SchemaVersion = 74
|
|
crossVersion.Profile = "v071"
|
|
err = importer.Replace(ctx, document, crossVersion)
|
|
if err == nil || !strings.Contains(err.Error(), "74") || !strings.Contains(err.Error(), "134") {
|
|
t.Fatalf("cross-version import must be refused with both versions named, got %v", err)
|
|
}
|
|
}
|
|
|
|
// compareSnapshotContent 逐表逐行比对"共有列"的规范化内容。
|
|
// chunks.seq_id 是库级序列值、不是文档内容,导入时由目标分配,故排除比对。
|
|
func compareSnapshotContent(t *testing.T, source, target PostgresSnapshot) {
|
|
t.Helper()
|
|
if len(source.Tables) != len(target.Tables) {
|
|
t.Fatalf("table set changed across round trip: %d -> %d", len(source.Tables), len(target.Tables))
|
|
}
|
|
for _, table := range source.Tables {
|
|
want := tableDigests(t, table.Name, table.Rows)
|
|
got := tableDigests(t, table.Name, target.Table(table.Name).Rows)
|
|
if table.RowCount != target.Table(table.Name).RowCount || len(want) != len(got) {
|
|
t.Fatalf("%s row count changed: %d -> %d", table.Name, len(want), len(got))
|
|
}
|
|
for index := range want {
|
|
if want[index] != got[index] {
|
|
t.Fatalf("%s row %d content changed across round trip", table.Name, index)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func tableDigests(t *testing.T, name string, rows []json.RawMessage) []string {
|
|
t.Helper()
|
|
digests := make([]string, 0, len(rows))
|
|
for _, raw := range rows {
|
|
var row map[string]json.RawMessage
|
|
if err := json.Unmarshal(raw, &row); err != nil {
|
|
t.Fatalf("decode %s row: %v", name, err)
|
|
}
|
|
if name == "chunks" {
|
|
delete(row, "seq_id")
|
|
}
|
|
digest, err := canonicalRowDigest(row)
|
|
if err != nil {
|
|
t.Fatalf("digest %s row: %v", name, err)
|
|
}
|
|
digests = append(digests, digest)
|
|
}
|
|
sort.Strings(digests)
|
|
return digests
|
|
}
|