73413c59ee
build / build (push) Successful in 2m3s
- profile 新增 137(由 v090 克隆),声明知识域关联表 copied/derived/excluded 分类。 - 导出前与导入写事务内各跑漏表门禁;未声明有数据拒绝,空表警告写入 WriteSet。 - chunk_revisions/wiki_page_revisions 接入范围改写、目标身份映射、自然键与回滚顺序。
263 lines
8.5 KiB
Go
263 lines
8.5 KiB
Go
package v071
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
|
|
"knowledge-governance/internal/libraryprofile"
|
|
)
|
|
|
|
// gateRows 回答门禁的 information_schema 探测:table → 列集合。
|
|
type gateRows struct {
|
|
tables [][2]any
|
|
index int
|
|
}
|
|
|
|
func (r *gateRows) Close() {}
|
|
func (r *gateRows) Err() error { return nil }
|
|
func (r *gateRows) CommandTag() pgconn.CommandTag { return pgconn.CommandTag{} }
|
|
func (r *gateRows) FieldDescriptions() []pgconn.FieldDescription { return nil }
|
|
func (r *gateRows) RawValues() [][]byte { return nil }
|
|
func (r *gateRows) Conn() *pgx.Conn { return nil }
|
|
func (r *gateRows) Next() bool {
|
|
if r.index >= len(r.tables) {
|
|
return false
|
|
}
|
|
r.index++
|
|
return true
|
|
}
|
|
func (r *gateRows) Scan(dest ...any) error {
|
|
*dest[0].(*string) = r.tables[r.index-1][0].(string)
|
|
*dest[1].(*[]string) = r.tables[r.index-1][1].([]string)
|
|
_ = dest
|
|
return nil
|
|
}
|
|
func (r *gateRows) Values() ([]any, error) { return nil, nil }
|
|
|
|
type gateCountRow struct{ value *int64 }
|
|
|
|
func (r gateCountRow) Scan(dest ...any) error { *dest[0].(*int64) = *r.value; return nil }
|
|
|
|
// gateDB 只实现门禁需要的两个入口:探测表/列 + 按表计数。
|
|
type gateDB struct {
|
|
candidates []struct {
|
|
table string
|
|
columns []string
|
|
}
|
|
counts map[string]int64
|
|
}
|
|
|
|
func (d gateDB) Query(_ context.Context, sql string, _ ...any) (pgx.Rows, error) {
|
|
if !strings.Contains(sql, "knowledge-table-gate") {
|
|
return nil, errors.New("unexpected query: " + sql)
|
|
}
|
|
rows := &gateRows{}
|
|
for _, candidate := range d.candidates {
|
|
rows.tables = append(rows.tables, [2]any{candidate.table, candidate.columns})
|
|
}
|
|
return rows, nil
|
|
}
|
|
|
|
func (d gateDB) QueryRow(_ context.Context, sql string, _ ...any) pgx.Row {
|
|
for table, count := range d.counts {
|
|
if strings.Contains(sql, `"public"."`+table+`"`) {
|
|
value := count
|
|
return gateCountRow{value: &value}
|
|
}
|
|
}
|
|
return gateCountRow{value: new(int64)}
|
|
}
|
|
|
|
func gateProfile(t *testing.T) libraryprofile.Profile {
|
|
t.Helper()
|
|
profile, err := libraryprofile.For(libraryprofile.V091Version)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return profile
|
|
}
|
|
|
|
func TestDeclaredTableGateRejectsUnclassifiedTableWithRows(t *testing.T) {
|
|
profile := gateProfile(t)
|
|
db := gateDB{
|
|
candidates: []struct {
|
|
table string
|
|
columns []string
|
|
}{
|
|
{"mystery_kb_table", []string{"id", "knowledge_base_id", "tenant_id"}},
|
|
},
|
|
counts: map[string]int64{"mystery_kb_table": 7},
|
|
}
|
|
_, err := verifyDeclaredKnowledgeTables(context.Background(), db,
|
|
PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, profile)
|
|
var blocked *UndeclaredKBTableError
|
|
if !errors.As(err, &blocked) {
|
|
t.Fatalf("unclassified table with rows must be refused, got %v", err)
|
|
}
|
|
if !strings.Contains(err.Error(), ReleaseUndeclaredKBTableCode) ||
|
|
!strings.Contains(err.Error(), "mystery_kb_table") || !strings.Contains(err.Error(), "7 行") {
|
|
t.Fatalf("error must name code/table/rows, got %v", err)
|
|
}
|
|
if !strings.Contains(err.Error(), "不可重试") {
|
|
t.Fatalf("error must state non-retryable, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDeclaredTableGateAllowsEmptyUnclassifiedTableWithWarning(t *testing.T) {
|
|
profile := gateProfile(t)
|
|
db := gateDB{
|
|
candidates: []struct {
|
|
table string
|
|
columns []string
|
|
}{
|
|
{"mystery_kb_table", []string{"id", "knowledge_base_id", "tenant_id"}},
|
|
},
|
|
counts: map[string]int64{"mystery_kb_table": 0},
|
|
}
|
|
warnings, err := verifyDeclaredKnowledgeTables(context.Background(), db,
|
|
PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, profile)
|
|
if err != nil {
|
|
t.Fatalf("empty unclassified table must pass: %v", err)
|
|
}
|
|
if len(warnings) != 1 || !strings.Contains(warnings[0], "mystery_kb_table") ||
|
|
!strings.Contains(warnings[0], ReleaseUndeclaredKBTableCode) {
|
|
t.Fatalf("expected one persisted warning naming the table, got %v", warnings)
|
|
}
|
|
}
|
|
|
|
func TestDeclaredTableGateAllowsClassifiedTables(t *testing.T) {
|
|
profile := gateProfile(t)
|
|
db := gateDB{
|
|
candidates: []struct {
|
|
table string
|
|
columns []string
|
|
}{
|
|
{"chunk_images", []string{"knowledge_base_id", "knowledge_id"}},
|
|
{"sessions", []string{"knowledge_base_id", "tenant_id"}},
|
|
{"messages", []string{"knowledge_id", "tenant_id"}},
|
|
{"data_sources", []string{"knowledge_base_id"}},
|
|
{"chunks", []string{"knowledge_base_id", "knowledge_id"}},
|
|
},
|
|
counts: map[string]int64{"sessions": 27, "messages": 90, "chunk_images": 3},
|
|
}
|
|
warnings, err := verifyDeclaredKnowledgeTables(context.Background(), db,
|
|
PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, profile)
|
|
if err != nil || len(warnings) != 0 {
|
|
t.Fatalf("derived/excluded/copied tables must pass silently, got %v/%v", warnings, err)
|
|
}
|
|
}
|
|
|
|
func TestDeclaredTableGateRefusesProfileWithoutClassification(t *testing.T) {
|
|
_, err := verifyDeclaredKnowledgeTables(context.Background(), gateDB{},
|
|
PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}, libraryprofile.Profile{Name: "bare"})
|
|
if err == nil || !strings.Contains(err.Error(), "分类") {
|
|
t.Fatalf("a profile without classification must not skip the gate, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestScopedKnowledgeTableCountSQLUsesExistingColumns(t *testing.T) {
|
|
identity := PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}
|
|
cases := []struct {
|
|
name string
|
|
columns map[string]bool
|
|
wantSQL string
|
|
want []any
|
|
}{
|
|
{"kb+tenant", map[string]bool{"knowledge_base_id": true, "tenant_id": true},
|
|
`tenant_id=$1 AND knowledge_base_id=$2`, []any{uint64(10000), "kb-safe"}},
|
|
{"kb without tenant", map[string]bool{"knowledge_base_id": true},
|
|
`knowledge_base_id=$1`, []any{"kb-safe"}},
|
|
{"kb_id+tenant", map[string]bool{"kb_id": true, "tenant_id": true},
|
|
`tenant_id=$1 AND kb_id=$2`, []any{uint64(10000), "kb-safe"}},
|
|
{"kb_id without tenant", map[string]bool{"kb_id": true},
|
|
`kb_id=$1`, []any{"kb-safe"}},
|
|
{"knowledge only", map[string]bool{"knowledge_id": true},
|
|
`knowledge_id IN (SELECT id FROM knowledges WHERE tenant_id=$1 AND knowledge_base_id=$2)`,
|
|
[]any{uint64(10000), "kb-safe"}},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
count, ok := scopedKnowledgeTableCountSQL("t", tc.columns, identity)
|
|
if !ok || !strings.Contains(count.sql, tc.wantSQL) {
|
|
t.Fatalf("sql=%q ok=%v, want fragment %q", count.sql, ok, tc.wantSQL)
|
|
}
|
|
if !reflect.DeepEqual(count.args, tc.want) {
|
|
t.Fatalf("args=%v, want %v", count.args, tc.want)
|
|
}
|
|
})
|
|
}
|
|
if _, ok := scopedKnowledgeTableCountSQL("t", map[string]bool{"id": true}, identity); ok {
|
|
t.Fatal("a table without any scope column must not be treated as countable")
|
|
}
|
|
}
|
|
|
|
// 导出路径必须真的执行门禁:候选表有数据时在封存前就失败。
|
|
type gateExportDB struct {
|
|
candidates []struct {
|
|
table string
|
|
columns []string
|
|
}
|
|
counts map[string]int64
|
|
}
|
|
|
|
func (d gateExportDB) BeginTx(context.Context, pgx.TxOptions) (pgx.Tx, error) {
|
|
return &gateExportTx{db: d}, nil
|
|
}
|
|
|
|
type gateExportTx struct {
|
|
pgx.Tx
|
|
db gateExportDB
|
|
}
|
|
|
|
func (tx *gateExportTx) Commit(context.Context) error { return nil }
|
|
func (tx *gateExportTx) Rollback(context.Context) error { return nil }
|
|
func (tx *gateExportTx) QueryRow(_ context.Context, sql string, _ ...any) pgx.Row {
|
|
if strings.Contains(sql, "schema_migrations") {
|
|
return gateSchemaRow{}
|
|
}
|
|
for table, count := range tx.db.counts {
|
|
if strings.Contains(sql, `"public"."`+table+`"`) {
|
|
value := count
|
|
return gateCountRow{value: &value}
|
|
}
|
|
}
|
|
return gateCountRow{value: new(int64)}
|
|
}
|
|
func (tx *gateExportTx) Query(_ context.Context, sql string, _ ...any) (pgx.Rows, error) {
|
|
if strings.Contains(sql, "knowledge-table-gate") {
|
|
rows := &gateRows{}
|
|
for _, candidate := range tx.db.candidates {
|
|
rows.tables = append(rows.tables, [2]any{candidate.table, candidate.columns})
|
|
}
|
|
return rows, nil
|
|
}
|
|
return legacySchemaProbeRows(), nil
|
|
}
|
|
|
|
type gateSchemaRow struct{}
|
|
|
|
func (gateSchemaRow) Scan(dest ...any) error {
|
|
*dest[0].(*int) = 74
|
|
*dest[1].(*bool) = false
|
|
return nil
|
|
}
|
|
|
|
func TestExportRunsDeclaredTableGateBeforeSealing(t *testing.T) {
|
|
db := gateExportDB{candidates: []struct {
|
|
table string
|
|
columns []string
|
|
}{{"mystery_kb_table", []string{"knowledge_base_id", "tenant_id"}}},
|
|
counts: map[string]int64{"mystery_kb_table": 2}}
|
|
_, err := ExportLibrary(context.Background(), db,
|
|
PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"})
|
|
if err == nil || !strings.Contains(err.Error(), ReleaseUndeclaredKBTableCode) {
|
|
t.Fatalf("export must fail closed on undeclared table data, got %v", err)
|
|
}
|
|
}
|