Files
knowledge-governance/internal/releaseadapter/v071/library_beforeimage.go
T
姚章浩 1a0f99e346
build / build (push) Successful in 1m44s
fix(release): 修复发布管理F01-F11资源闭包与状态呈现
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 验收。
2026-11-05 12:50:11 +08:00

87 lines
2.9 KiB
Go

package v071
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/jackc/pgx/v5"
)
// Recovery bytes are not a source candidate. The selection must equal the
// destructive target predicate, including tombstones and their relationships.
// Semantic source validation would discard/reject exactly those historical
// rows that an exact rollback must preserve.
func exportLibraryBeforeImage(ctx context.Context, db librarySnapshotDB, identity PostgresIdentity) (PostgresSnapshot, error) {
tx, err := db.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.RepeatableRead, AccessMode: pgx.ReadOnly})
if err != nil {
return PostgresSnapshot{}, err
}
defer tx.Rollback(context.WithoutCancel(ctx))
var version int
var dirty bool
if err := tx.QueryRow(ctx, `SELECT version,dirty FROM schema_migrations LIMIT 1`).Scan(&version, &dirty); err != nil || version != 74 || dirty {
return PostgresSnapshot{}, errors.New("before-image schema contract mismatch")
}
exporter := PostgresExporter{db: tx}
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
}
rowsByTable[table] = rows
if table != "resource_bindings" && table != "knowledge_tag_relations" {
bodies = append(bodies, rows...)
}
if table == "resource_bindings" {
bindings = 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 {
return PostgresSnapshot{}, err
}
resourceHandles := map[string]bool{}
resourceIDs := []string{}
for _, raw := range snapshot.Table("resources").Rows {
var row struct {
ID string `json:"id"`
Handle string `json:"handle"`
}
if err := json.Unmarshal(raw, &row); err != nil {
return PostgresSnapshot{}, err
}
resourceHandles[row.Handle] = true
resourceIDs = append(resourceIDs, row.ID)
}
for _, handle := range handles {
if !resourceHandles[handle] {
return PostgresSnapshot{}, errors.New("before-image contains unresolved resource reference")
}
}
if err := verifyLibraryResourceOwners(ctx, tx, resourceIDs); err != nil {
return PostgresSnapshot{}, err
}
if err := tx.Commit(ctx); err != nil {
return PostgresSnapshot{}, err
}
return snapshot, nil
}