fba2713b4e
build / build (push) Successful in 1m54s
整库发布写入路径原先按 0.7.1 的固定表集合工作:0.9.0 没有 wiki_log_entries, Current/清理/编目/整实例指纹都会在不存在表上失败,跨版本闸门也因快照丢掉 SchemaVersion 而失效(未知版本被当作 74)。 改为在装配时解析目标 profile,表集合与清理顺序按解析结果;快照复制与范围改写 带上 SchemaVersion/Profile;跨版本发布显式拒绝并同时点名两个版本。单文档发布包 仍只支持 0.7.1,但改为明确拒绝并说明原因。 本机隔离验证:真实 0.9.0 schema 源库→导出→封存→写入干净 134 目标→逐表核对→ 写前镜像回滚;74→134 拒绝与 134 缺表不报错两个负例通过。make verify 17 项全绿。
86 lines
2.9 KiB
Go
86 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))
|
|
profile, err := ResolveSchemaProfile(ctx, tx, "before-image source")
|
|
if err != nil {
|
|
return PostgresSnapshot{}, err
|
|
}
|
|
exporter := PostgresExporter{db: tx}
|
|
rowsByTable := map[string][]json.RawMessage{}
|
|
var bodies, bindings []json.RawMessage
|
|
for _, table := range profile.Tables {
|
|
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, scopeWhereFor(profile.Tables, 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{SchemaVersion: profile.SchemaVersion, Profile: profile.Name}
|
|
for _, table := range profile.Tables {
|
|
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
|
|
}
|