F01:捕获、写前镜像、指纹和恢复后内容校验共用按目标精确绑定的资源闭包: 从已限定范围的 resource_bindings 与正文 handle 一次提取,再按索引取资源, 不再对每个租户资源扫描全部正文表。无绑定正文引用仍保留,缺失资源仍失败。 F02/F03:恢复验证使用 capture/verify/canary/restore/finalize 自己的阶段,登记 证据不再叫新版本生效;顶部优先在飞作业;已验证且 API reason 为空时不再同屏 显示未读到状态,报告可展开。 F04-F11:显示实际目标实例/项目/库;运行中主按钮查看当前任务并禁止重复启动; 快照历史默认已登记、可筛选分页并定位所选;无真实下次计划时显示未读取;各 scope 独立 overview;新操作清旧通知;存储行按文档/分块/资源/绑定及 Wiki 页面/目录/日志分项。高影响恢复验证/恢复增加一次影响确认。 验证:隔离 PG 20k 无关资源夹具旧扫描 683ms→新导出 41ms,资源 ID 闭包相等; Go/前端全量及 make verify 17 项通过。未部署、未做 8088 UI 验收。
This commit is contained in:
@@ -342,7 +342,11 @@ type libraryOverview struct {
|
||||
CaptureAvailable bool `json:"capture_available"`
|
||||
CaptureReason string `json:"capture_reason,omitempty"`
|
||||
Executor libraryExecutorView `json:"executor"`
|
||||
Now time.Time `json:"now"`
|
||||
// 实际目标身份:页面不能只显示"weknora-test"或旧文档级绑定名称。
|
||||
TargetInstance string `json:"target_instance,omitempty"`
|
||||
TargetProject string `json:"target_project,omitempty"`
|
||||
TargetKnowledgeBaseID string `json:"target_knowledge_base_id,omitempty"`
|
||||
Now time.Time `json:"now"`
|
||||
}
|
||||
|
||||
// libraryVersionRef 只回答"这个快照被登记成哪一类版本、由哪个作业登记"。
|
||||
@@ -686,10 +690,16 @@ func libraryOverviewHandler(w http.ResponseWriter, r *http.Request, config Confi
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
overview := libraryOverview{Scope: scope, Jobs: []governance.LibraryJob{}, Now: now,
|
||||
TargetInstance: config.LibraryTargetInstance, TargetProject: config.LibraryTargetProject,
|
||||
Gate: libraryGateView{Reasons: []string{}},
|
||||
Diff: libraryDiffView{State: "unknown", Reason: "生产与测试的文档级差异读取未接线", Added: []any{}, Updated: []any{}, Removed: []any{}}}
|
||||
// 可发起封存 = worker 真实探测到执行器可用且指向本实例;原因逐条给全。
|
||||
overview.CaptureAvailable, overview.CaptureReason, overview.Executor = libraryExecutorAvailability(r.Context(), config)
|
||||
if scope == "internal" {
|
||||
overview.TargetKnowledgeBaseID = config.LibraryProductionInternalKB
|
||||
} else {
|
||||
overview.TargetKnowledgeBaseID = config.LibraryProductionExternalKB
|
||||
}
|
||||
|
||||
// 测试封存来自归档 + 版本登记:归档里**不止**测试封存——生产预备份与目标基线
|
||||
// 快照同样带 scope,如果只按"这个 scope 最新一份"挑,页面就会把目标自己的内容
|
||||
|
||||
@@ -73,32 +73,56 @@ type tableFingerprint struct {
|
||||
// fingerprints 读取当前生产指纹;Prepare 在导出前后各取一次做一致性判断。
|
||||
func (s *ProductionBackupStore) fingerprints(ctx context.Context) (map[string]tableFingerprint, error) {
|
||||
result := map[string]tableFingerprint{}
|
||||
exporter := PostgresExporter{db: s.pool}
|
||||
var bodies, bindings []json.RawMessage
|
||||
for _, table := range productionLibraryTables {
|
||||
rows, err := s.pool.Query(ctx, fmt.Sprintf(`SELECT to_jsonb(%s) FROM %s WHERE %s`,
|
||||
if table == "resources" {
|
||||
continue
|
||||
}
|
||||
rows, err := exporter.queryRows(ctx, fmt.Sprintf(`SELECT to_jsonb(%s) FROM %s WHERE %s`,
|
||||
table, table, scopeWhere(table)), s.identity.TenantID, s.identity.KnowledgeBaseID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取生产 %s 指纹失败", table)
|
||||
}
|
||||
digests := make([]string, 0, 64)
|
||||
for rows.Next() {
|
||||
var raw map[string]json.RawMessage
|
||||
if err := rows.Scan(&raw); err != nil {
|
||||
rows.Close()
|
||||
if table != "resource_bindings" && table != "knowledge_tag_relations" {
|
||||
bodies = append(bodies, rows...)
|
||||
}
|
||||
if table == "resource_bindings" {
|
||||
bindings = rows
|
||||
}
|
||||
digests := make([]string, 0, len(rows))
|
||||
for _, raw := range rows {
|
||||
var value map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
digest, err := canonicalRowDigest(raw)
|
||||
digest, err := canonicalRowDigest(value)
|
||||
if err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
digests = append(digests, digest)
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result[table] = tableFingerprint{Count: int64(len(digests)), Digest: combineRowDigests(digests)}
|
||||
}
|
||||
// F01: resolve the resources once from exact binding ids and body handles
|
||||
// instead of scanning every tenant resource with a correlated body filter.
|
||||
rows, err := scopedResourceRows(ctx, &exporter, s.identity, true, bodies, bindings)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("读取生产 resources 指纹失败: %w", err)
|
||||
}
|
||||
digests := make([]string, 0, len(rows))
|
||||
for _, raw := range rows {
|
||||
var value map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
digest, err := canonicalRowDigest(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
digests = append(digests, digest)
|
||||
}
|
||||
result["resources"] = tableFingerprint{Count: int64(len(digests)), Digest: combineRowDigests(digests)}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -25,17 +25,34 @@ func exportLibraryBeforeImage(ctx context.Context, db librarySnapshotDB, identit
|
||||
return PostgresSnapshot{}, errors.New("before-image schema contract mismatch")
|
||||
}
|
||||
exporter := PostgresExporter{db: tx}
|
||||
snapshot := PostgresSnapshot{}
|
||||
rowsByTable := map[string][]json.RawMessage{}
|
||||
var bodies, bindings []json.RawMessage
|
||||
for _, table := range productionLibraryTables {
|
||||
if table == "resources" {
|
||||
continue
|
||||
}
|
||||
rows, err := exporter.queryRows(ctx, fmt.Sprintf(`SELECT to_jsonb(%s) FROM %s WHERE %s ORDER BY (%s)::text`, table, table, scopeWhere(table), identityExpr(table)), identity.TenantID, identity.KnowledgeBaseID)
|
||||
if err != nil {
|
||||
return PostgresSnapshot{}, err
|
||||
}
|
||||
snapshot.Tables = append(snapshot.Tables, newTableSnapshot(table, rows))
|
||||
rowsByTable[table] = rows
|
||||
if table != "resource_bindings" && table != "knowledge_tag_relations" {
|
||||
bodies = append(bodies, rows...)
|
||||
}
|
||||
if table == "resource_bindings" {
|
||||
bindings = rows
|
||||
}
|
||||
}
|
||||
var bodies []json.RawMessage
|
||||
for _, table := range snapshot.Tables {
|
||||
bodies = append(bodies, table.Rows...)
|
||||
// F01: the before-image needs exact rollback rows, including deleted
|
||||
// resources, but must not rescan every tenant resource per body table.
|
||||
resources, err := scopedResourceRows(ctx, &exporter, identity, true, bodies, bindings)
|
||||
if err != nil {
|
||||
return PostgresSnapshot{}, err
|
||||
}
|
||||
rowsByTable["resources"] = resources
|
||||
snapshot := PostgresSnapshot{}
|
||||
for _, table := range productionLibraryTables {
|
||||
snapshot.Tables = append(snapshot.Tables, newTableSnapshot(table, rowsByTable[table]))
|
||||
}
|
||||
handles, err := libraryResourceHandles(bodies)
|
||||
if err != nil {
|
||||
|
||||
@@ -81,13 +81,17 @@ func ExportLibraryWithReleases(ctx context.Context, db librarySnapshotDB, identi
|
||||
// The latter includes Wiki manual pages, which document bundles used to miss.
|
||||
var bodies []json.RawMessage
|
||||
for _, table := range snapshot.Tables {
|
||||
if table.Name == "resource_bindings" || table.Name == "knowledge_tag_relations" {
|
||||
continue
|
||||
}
|
||||
bodies = append(bodies, table.Rows...)
|
||||
}
|
||||
handles, err := libraryResourceHandles(bodies)
|
||||
if err != nil {
|
||||
return PostgresSnapshot{}, err
|
||||
}
|
||||
resources, err := exporter.queryRows(ctx, libraryResourcesSQL, identity.TenantID, identity.KnowledgeBaseID, handles)
|
||||
resources, err := scopedResourceRows(ctx, &exporter, identity, false, bodies,
|
||||
snapshot.Table("resource_bindings").Rows)
|
||||
if err != nil {
|
||||
return PostgresSnapshot{}, err
|
||||
}
|
||||
@@ -151,4 +155,48 @@ var libraryContentQueries = []struct{ name, sql string }{
|
||||
{"wiki_log_entries", `SELECT to_jsonb(w) FROM wiki_log_entries w WHERE w.tenant_id=$1 AND w.knowledge_base_id=$2 ORDER BY w.id`},
|
||||
}
|
||||
|
||||
const libraryResourcesSQL = `SELECT to_jsonb(r) FROM resources r WHERE r.tenant_id=$1 AND r.deleted_at IS NULL AND (r.handle=ANY($3::text[]) OR EXISTS (SELECT 1 FROM resource_bindings b WHERE b.resource_id=r.id AND b.tenant_id=$1 AND ((b.owner_type='knowledge' AND EXISTS (SELECT 1 FROM knowledges k WHERE k.id=b.owner_id AND k.tenant_id=$1 AND k.knowledge_base_id=$2 AND k.deleted_at IS NULL)) OR (b.owner_type='chunk' AND EXISTS (SELECT 1 FROM chunks c JOIN knowledges k ON k.id=c.knowledge_id AND k.tenant_id=c.tenant_id AND k.knowledge_base_id=c.knowledge_base_id WHERE c.id=b.owner_id AND c.tenant_id=$1 AND c.knowledge_base_id=$2 AND c.deleted_at IS NULL AND k.deleted_at IS NULL))))) ORDER BY r.id`
|
||||
// F01: resources were previously selected by an OR over a per-row EXISTS that
|
||||
// re-scanned every body table, so a small KB in a large tenant paid for all
|
||||
// unrelated resources. The caller now extracts the exact binding ids and body
|
||||
// handles first; both are indexed identities, never filename guesses.
|
||||
const libraryResourcesSQL = `SELECT to_jsonb(r) FROM resources r
|
||||
WHERE r.tenant_id=$1 AND ($4::boolean OR r.deleted_at IS NULL)
|
||||
AND (r.id=ANY($2::text[]) OR r.handle=ANY($3::text[]))
|
||||
ORDER BY r.id`
|
||||
|
||||
// scopedResourceRows is the single F01 read path: binding ids and body handles
|
||||
// are extracted once from already scoped rows, then both are resolved through
|
||||
// indexed resource identities. The old per-resource EXISTS/position filter
|
||||
// rescanned every body table for every tenant resource.
|
||||
func scopedResourceRows(ctx context.Context, exporter *PostgresExporter, identity PostgresIdentity,
|
||||
includeDeleted bool, bodies, bindings []json.RawMessage) ([]json.RawMessage, error) {
|
||||
handles, err := libraryResourceHandles(bodies)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ids, err := libraryBindingResourceIDs(bindings)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return exporter.queryRows(ctx, libraryResourcesSQL, identity.TenantID, ids, handles, includeDeleted)
|
||||
}
|
||||
|
||||
// libraryBindingResourceIDs reads the exact resource ids owned by the already
|
||||
// scoped resource_bindings rows. It does not infer ownership from names.
|
||||
func libraryBindingResourceIDs(rows []json.RawMessage) ([]string, error) {
|
||||
seen := map[string]bool{}
|
||||
ids := make([]string, 0, len(rows))
|
||||
for _, raw := range rows {
|
||||
var row struct {
|
||||
ResourceID string `json:"resource_id"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &row); err != nil || row.ResourceID == "" {
|
||||
return nil, errors.New("invalid scoped resource binding")
|
||||
}
|
||||
if !seen[row.ResourceID] {
|
||||
seen[row.ResourceID] = true
|
||||
ids = append(ids, row.ResourceID)
|
||||
}
|
||||
}
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
@@ -301,23 +301,61 @@ func identityExpr(table string) string {
|
||||
}
|
||||
|
||||
func (t *ProductionLibraryTarget) tableState(ctx context.Context, table string) (int64, string, error) {
|
||||
rows, err := t.pool.Query(ctx, fmt.Sprintf(`SELECT %s FROM %s WHERE %s ORDER BY 1`,
|
||||
"("+identityExpr(table)+")::text", table, scopeWhere(table)), t.scope.TenantID, t.scope.KnowledgeBaseID)
|
||||
if err != nil {
|
||||
return 0, "", fmt.Errorf("读取生产 %s 失败", table)
|
||||
identity := PostgresIdentity{TenantID: t.scope.TenantID, KnowledgeBaseID: t.scope.KnowledgeBaseID}
|
||||
var rows []json.RawMessage
|
||||
if table == "resources" {
|
||||
// F01: derive exact references once; do not run the per-resource body
|
||||
// scan used by the legacy scope predicate.
|
||||
exporter := PostgresExporter{db: t.pool}
|
||||
var bodies, bindings []json.RawMessage
|
||||
for _, name := range productionLibraryTables {
|
||||
if name == "resources" {
|
||||
continue
|
||||
}
|
||||
items, err := exporter.queryRows(ctx, fmt.Sprintf(`SELECT to_jsonb(%s) FROM %s WHERE %s`,
|
||||
name, name, scopeWhere(name)), identity.TenantID, identity.KnowledgeBaseID)
|
||||
if err != nil {
|
||||
return 0, "", fmt.Errorf("读取生产 %s 失败", table)
|
||||
}
|
||||
if name != "resource_bindings" && name != "knowledge_tag_relations" {
|
||||
bodies = append(bodies, items...)
|
||||
}
|
||||
if name == "resource_bindings" {
|
||||
bindings = items
|
||||
}
|
||||
}
|
||||
items, err := scopedResourceRows(ctx, &exporter, identity, true, bodies, bindings)
|
||||
if err != nil {
|
||||
return 0, "", fmt.Errorf("读取生产 %s 失败: %w", table, err)
|
||||
}
|
||||
rows = items
|
||||
} else {
|
||||
exporter := PostgresExporter{db: t.pool}
|
||||
items, err := exporter.queryRows(ctx, fmt.Sprintf(`SELECT %s FROM %s WHERE %s ORDER BY 1`,
|
||||
"("+identityExpr(table)+")::text", table, scopeWhere(table)), identity.TenantID, identity.KnowledgeBaseID)
|
||||
if err != nil {
|
||||
return 0, "", fmt.Errorf("读取生产 %s 失败", table)
|
||||
}
|
||||
rows = items
|
||||
}
|
||||
defer rows.Close()
|
||||
ids := make([]string, 0, 64)
|
||||
for rows.Next() {
|
||||
ids := make([]string, 0, len(rows))
|
||||
for _, raw := range rows {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
return 0, "", err
|
||||
if table == "resources" {
|
||||
var value struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &value); err != nil || value.ID == "" {
|
||||
return 0, "", fmt.Errorf("读取生产 %s 身份失败", table)
|
||||
}
|
||||
id = value.ID
|
||||
} else {
|
||||
if err := json.Unmarshal(raw, &id); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
return int64(len(ids)), digestIDs(ids), nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
package v071
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// F01 regression: a small target library inside a large tenant must resolve
|
||||
// resources from exact binding ids + body handles, not by scanning every
|
||||
// tenant resource with a per-row body predicate. The old predicate is kept
|
||||
// here only as an oracle for equality and timing; never as the product path.
|
||||
func TestLibraryResourceClosureIsBoundedByTargetReferences(t *testing.T) {
|
||||
dsn := os.Getenv("KG_LIBRARY_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("requires explicitly isolated kg_library_test_ PostgreSQL database")
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil || !strings.HasPrefix(cfg.ConnConfig.Database, "kg_library_test_") || os.Getenv("KG_LIBRARY_TEST_ISOLATED") != "1" {
|
||||
t.Fatal("isolated database required")
|
||||
}
|
||||
ctx := context.Background()
|
||||
db, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
if _, err = db.Exec(ctx, `DROP SCHEMA public CASCADE; CREATE SCHEMA public`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, libraryTestSchema); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, `CREATE INDEX resources_handle_f01 ON resources(handle); CREATE INDEX resources_pkey_f01 ON resources(id);`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const unrelated = 20000
|
||||
if _, err = db.Exec(ctx, `INSERT INTO resources(id,handle,tenant_id,physical_path,deleted_at)
|
||||
SELECT 'unrelated-'||i, lpad(i::text,22,'0'), 10000, 'unrelated/'||i, NULL FROM generate_series(1,$1) i`, unrelated); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// A body-only reference must still be found without a binding row.
|
||||
if _, err = db.Exec(ctx, `INSERT INTO resources(id,handle,tenant_id,physical_path,deleted_at)
|
||||
VALUES ('res-body','BBBBBBBBBBBBBBBBBBBBBB',10000,'body.txt',NULL);
|
||||
UPDATE chunks SET content='see resource://BBBBBBBBBBBBBBBBBBBBBB' WHERE id='chunk-safe'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
identity := PostgresIdentity{TenantID: 10000, KnowledgeBaseID: "kb-safe"}
|
||||
|
||||
start := time.Now()
|
||||
oldRows, err := db.Query(ctx, fmt.Sprintf(`SELECT to_jsonb(resources)::text FROM resources WHERE %s`, scopeWhere("resources")),
|
||||
identity.TenantID, identity.KnowledgeBaseID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
oldIDs := []string{}
|
||||
for oldRows.Next() {
|
||||
var raw string
|
||||
if err := oldRows.Scan(&raw); err != nil {
|
||||
oldRows.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
var row struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(raw), &row); err != nil {
|
||||
oldRows.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
oldIDs = append(oldIDs, row.ID)
|
||||
}
|
||||
oldRows.Close()
|
||||
oldElapsed := time.Since(start)
|
||||
|
||||
start = time.Now()
|
||||
snapshot, err := ExportLibrary(ctx, db, identity)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
newElapsed := time.Since(start)
|
||||
newIDs := []string{}
|
||||
for _, raw := range snapshot.Table("resources").Rows {
|
||||
var row struct {
|
||||
ID string `json:"id"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &row); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
newIDs = append(newIDs, row.ID)
|
||||
}
|
||||
if fmt.Sprint(oldIDs) != fmt.Sprint(newIDs) {
|
||||
t.Fatalf("resource closure changed: old=%v new=%v", oldIDs, newIDs)
|
||||
}
|
||||
var bodies []json.RawMessage
|
||||
for _, table := range snapshot.Tables {
|
||||
bodies = append(bodies, table.Rows...)
|
||||
}
|
||||
handles, handleErr := libraryResourceHandles(bodies)
|
||||
t.Logf("old_ids=%v new_ids=%v handles=%v handle_err=%v", oldIDs, newIDs, handles, handleErr)
|
||||
if len(newIDs) != 2 {
|
||||
t.Fatalf("expected bound + body-only resources, got %v", newIDs)
|
||||
}
|
||||
t.Logf("unrelated_resources=%d old_scan=%s new_export=%s resources=%v", unrelated, oldElapsed, newElapsed, newIDs)
|
||||
if newElapsed >= oldElapsed {
|
||||
t.Fatalf("F01 path did not improve: old=%s new=%s", oldElapsed, newElapsed)
|
||||
}
|
||||
}
|
||||
@@ -68,22 +68,52 @@ func joinLines(items []string) string {
|
||||
// 归档没有的列(生产默认值/后续新增列)不参与比对,避免"我们没管的列"造成假失败,
|
||||
// 但归档里有的每一列都必须逐字一致。
|
||||
func (t *ProductionLibraryTarget) productionTableContent(ctx context.Context, table string, columns map[string]bool) (int64, string, error) {
|
||||
// 不加表别名:scopeWhere(resources) 里引用了 resources.id,别名会让它失效。
|
||||
rows, err := t.pool.Query(ctx, fmt.Sprintf(`SELECT to_jsonb(%s) FROM %s WHERE %s`, table, table, scopeWhere(table)),
|
||||
t.scope.TenantID, t.scope.KnowledgeBaseID)
|
||||
if err != nil {
|
||||
return 0, "", fmt.Errorf("读取生产 %s 内容失败: %w", table, err)
|
||||
identity := PostgresIdentity{TenantID: t.scope.TenantID, KnowledgeBaseID: t.scope.KnowledgeBaseID}
|
||||
exporter := PostgresExporter{db: t.pool}
|
||||
var rows []json.RawMessage
|
||||
if table == "resources" {
|
||||
// F01: verification must use the same bounded reference set as capture;
|
||||
// the legacy per-resource body scan made post-sync verification minutes
|
||||
// long on a small target inside a large tenant.
|
||||
var bodies, bindings []json.RawMessage
|
||||
for _, name := range productionLibraryTables {
|
||||
if name == "resources" {
|
||||
continue
|
||||
}
|
||||
items, err := exporter.queryRows(ctx, fmt.Sprintf(`SELECT to_jsonb(%s) FROM %s WHERE %s`,
|
||||
name, name, scopeWhere(name)), identity.TenantID, identity.KnowledgeBaseID)
|
||||
if err != nil {
|
||||
return 0, "", fmt.Errorf("读取生产 %s 内容失败: %w", table, err)
|
||||
}
|
||||
if name != "resource_bindings" && name != "knowledge_tag_relations" {
|
||||
bodies = append(bodies, items...)
|
||||
}
|
||||
if name == "resource_bindings" {
|
||||
bindings = items
|
||||
}
|
||||
}
|
||||
items, err := scopedResourceRows(ctx, &exporter, identity, true, bodies, bindings)
|
||||
if err != nil {
|
||||
return 0, "", fmt.Errorf("读取生产 %s 内容失败: %w", table, err)
|
||||
}
|
||||
rows = items
|
||||
} else {
|
||||
items, err := exporter.queryRows(ctx, fmt.Sprintf(`SELECT to_jsonb(%s) FROM %s WHERE %s`, table, table, scopeWhere(table)),
|
||||
identity.TenantID, identity.KnowledgeBaseID)
|
||||
if err != nil {
|
||||
return 0, "", fmt.Errorf("读取生产 %s 内容失败: %w", table, err)
|
||||
}
|
||||
rows = items
|
||||
}
|
||||
defer rows.Close()
|
||||
digests := make([]string, 0, 64)
|
||||
for rows.Next() {
|
||||
var raw map[string]json.RawMessage
|
||||
if err := rows.Scan(&raw); err != nil {
|
||||
digests := make([]string, 0, len(rows))
|
||||
for _, raw := range rows {
|
||||
var value map[string]json.RawMessage
|
||||
if err := json.Unmarshal(raw, &value); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
projected := make(map[string]json.RawMessage, len(columns))
|
||||
for key := range columns {
|
||||
if value, ok := raw[key]; ok {
|
||||
if value, ok := value[key]; ok {
|
||||
projected[key] = value
|
||||
}
|
||||
}
|
||||
@@ -93,9 +123,6 @@ func (t *ProductionLibraryTarget) productionTableContent(ctx context.Context, ta
|
||||
}
|
||||
digests = append(digests, digest)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
return int64(len(digests)), combineRowDigests(digests), nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user