73413c59ee
build / build (push) Successful in 2m3s
- profile 新增 137(由 v090 克隆),声明知识域关联表 copied/derived/excluded 分类。 - 导出前与导入写事务内各跑漏表门禁;未声明有数据拒绝,空表警告写入 WriteSet。 - chunk_revisions/wiki_page_revisions 接入范围改写、目标身份映射、自然键与回滚顺序。
166 lines
6.3 KiB
Go
166 lines
6.3 KiB
Go
package v071
|
||
|
||
import (
|
||
"context"
|
||
"errors"
|
||
"fmt"
|
||
"sort"
|
||
|
||
"github.com/jackc/pgx/v5"
|
||
|
||
"knowledge-governance/internal/libraryprofile"
|
||
)
|
||
|
||
// 漏表门禁:profile 只声明"必须存在什么",不声明"存在但没见过的表怎么办"。
|
||
// 于是 0.9.0 起 chunk_revisions / wiki_page_revisions 一直被静默跳过。这里用
|
||
// information_schema 反向扫描所有带知识列的表,按 profile 分类放行;未分类且
|
||
// 本次范围内有数据就拒绝,空表放行但留下警告。导出前(源库)与导入前(目标库)
|
||
// 各执行一次。
|
||
//
|
||
// 为什么错误不可重试:表没分类是契约缺口,重试不会让分类长出来;必须补 profile
|
||
// 或明确排除。错误里带表名与行数,运维不用再猜是哪张表。
|
||
|
||
const ReleaseUndeclaredKBTableCode = "release_undeclared_kb_table"
|
||
|
||
type UndeclaredKBTable struct {
|
||
Table string
|
||
Rows int64
|
||
}
|
||
|
||
type UndeclaredKBTableError struct {
|
||
Tables []UndeclaredKBTable
|
||
}
|
||
|
||
func (e *UndeclaredKBTableError) Error() string {
|
||
parts := make([]string, 0, len(e.Tables))
|
||
for _, table := range e.Tables {
|
||
if table.Rows < 0 {
|
||
parts = append(parts, table.Table+"(行数无法判定)")
|
||
continue
|
||
}
|
||
parts = append(parts, fmt.Sprintf("%s(%d 行)", table.Table, table.Rows))
|
||
}
|
||
return fmt.Sprintf("%s: 未声明的知识域表在本次发布范围内仍有数据:%v;先补 profile 关联表分类并接入整库搬运,或明确排除并写明理由。此错误不可重试。",
|
||
ReleaseUndeclaredKBTableCode, parts)
|
||
}
|
||
|
||
// knowledgeTableGateMarker 让测试夹具能区分"门禁探测"与"能力探测":两者都查
|
||
// information_schema.columns,但回答的列形状不同(表+列集合 vs 表+列)。
|
||
const knowledgeTableGateMarker = "/* knowledge-table-gate */"
|
||
|
||
const knowledgeScopedColumnsSQL = knowledgeTableGateMarker + `
|
||
SELECT c.table_name, array_agg(c.column_name ORDER BY c.column_name)
|
||
FROM information_schema.columns c
|
||
JOIN (
|
||
SELECT DISTINCT table_name
|
||
FROM information_schema.columns
|
||
WHERE table_schema='public' AND column_name IN ('knowledge_base_id','knowledge_id','kb_id')
|
||
) k ON k.table_name = c.table_name
|
||
WHERE c.table_schema='public'
|
||
GROUP BY c.table_name
|
||
ORDER BY c.table_name`
|
||
|
||
// verifyDeclaredKnowledgeTables 返回放行警告(未分类但为空的表);有未分类且
|
||
// 非空的表时返回 UndeclaredKBTableError。identity 是本知识库范围。
|
||
func verifyDeclaredKnowledgeTables(ctx context.Context, db schemaProbeDB, identity PostgresIdentity,
|
||
profile libraryprofile.Profile) ([]string, error) {
|
||
if identity.TenantID == 0 || !validSearchIdentifier(identity.KnowledgeBaseID) {
|
||
return nil, errors.New("漏表门禁缺少有效的知识库范围身份")
|
||
}
|
||
// 没有分类的 profile 不允许"静默跳过门禁":那等于把契约缺口藏起来。
|
||
if len(profile.RelatedTables) == 0 {
|
||
return nil, fmt.Errorf("profile %s 没有关联表分类,拒绝跳过漏表门禁", profile.Name)
|
||
}
|
||
rows, err := db.Query(ctx, knowledgeScopedColumnsSQL)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("读取知识域表清单失败: %w", err)
|
||
}
|
||
defer rows.Close()
|
||
unclassified := make([]string, 0, 4)
|
||
columnsByTable := map[string]map[string]bool{}
|
||
for rows.Next() {
|
||
var table string
|
||
var columns []string
|
||
if err := rows.Scan(&table, &columns); err != nil {
|
||
return nil, fmt.Errorf("解析知识域表清单失败: %w", err)
|
||
}
|
||
if _, ok := profile.RelatedTableClass(table); ok {
|
||
continue
|
||
}
|
||
set := make(map[string]bool, len(columns))
|
||
for _, column := range columns {
|
||
set[column] = true
|
||
}
|
||
columnsByTable[table] = set
|
||
unclassified = append(unclassified, table)
|
||
}
|
||
if err := rows.Err(); err != nil {
|
||
return nil, fmt.Errorf("遍历知识域表清单失败: %w", err)
|
||
}
|
||
|
||
blocked := make([]UndeclaredKBTable, 0, len(unclassified))
|
||
warnings := make([]string, 0, len(unclassified))
|
||
for _, table := range unclassified {
|
||
count, ok := scopedKnowledgeTableCountSQL(table, columnsByTable[table], identity)
|
||
if !ok {
|
||
// 候选表却没有可用的范围列:无法证明"本次范围内有没有数据",
|
||
// 只能按有数据处理,避免把范围外的表误放行。
|
||
blocked = append(blocked, UndeclaredKBTable{Table: table, Rows: -1})
|
||
continue
|
||
}
|
||
var total int64
|
||
if err := db.QueryRow(ctx, count.sql, count.args...).Scan(&total); err != nil {
|
||
return nil, fmt.Errorf("统计未声明表 %s 失败: %w", table, err)
|
||
}
|
||
if total > 0 {
|
||
blocked = append(blocked, UndeclaredKBTable{Table: table, Rows: total})
|
||
continue
|
||
}
|
||
warnings = append(warnings, fmt.Sprintf(
|
||
"%s: 未分类表 %q 在本次范围内为空,已放行;请补 profile 关联表分类", ReleaseUndeclaredKBTableCode, table))
|
||
}
|
||
sort.Slice(blocked, func(i, j int) bool { return blocked[i].Table < blocked[j].Table })
|
||
sort.Strings(warnings)
|
||
if len(blocked) > 0 {
|
||
return nil, &UndeclaredKBTableError{Tables: blocked}
|
||
}
|
||
return warnings, nil
|
||
}
|
||
|
||
type scopedCount struct {
|
||
sql string
|
||
args []any
|
||
}
|
||
|
||
// scopedKnowledgeTableCountSQL 按"这张表实际有的范围列"构造计数:有
|
||
// knowledge_base_id 优先按 tenant+kb;有 kb_id 按 tenant+kb_id;只有
|
||
// knowledge_id 时走 knowledges 子查询。没有 tenant_id 的表(如 kb_shares)
|
||
// 只能按知识库身份过滤,不能硬塞一个不存在的列。
|
||
func scopedKnowledgeTableCountSQL(table string, columns map[string]bool,
|
||
identity PostgresIdentity) (scopedCount, bool) {
|
||
quoted := pgx.Identifier{"public", table}.Sanitize()
|
||
hasTenant := columns["tenant_id"]
|
||
var where string
|
||
var args []any
|
||
switch {
|
||
case columns["knowledge_base_id"]:
|
||
if hasTenant {
|
||
where, args = "tenant_id=$1 AND knowledge_base_id=$2", []any{identity.TenantID, identity.KnowledgeBaseID}
|
||
} else {
|
||
where, args = "knowledge_base_id=$1", []any{identity.KnowledgeBaseID}
|
||
}
|
||
case columns["kb_id"]:
|
||
if hasTenant {
|
||
where, args = "tenant_id=$1 AND kb_id=$2", []any{identity.TenantID, identity.KnowledgeBaseID}
|
||
} else {
|
||
where, args = "kb_id=$1", []any{identity.KnowledgeBaseID}
|
||
}
|
||
case columns["knowledge_id"]:
|
||
where = "knowledge_id IN (SELECT id FROM knowledges WHERE tenant_id=$1 AND knowledge_base_id=$2)"
|
||
args = []any{identity.TenantID, identity.KnowledgeBaseID}
|
||
default:
|
||
return scopedCount{}, false
|
||
}
|
||
return scopedCount{sql: "/* knowledge-table-gate-count */ SELECT count(*) FROM " + quoted + " WHERE " + where, args: args}, true
|
||
}
|