fix(release): 修复发布管理F01-F11资源闭包与状态呈现
build / build (push) Successful in 1m44s

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:
姚章浩
2026-11-05 12:50:11 +08:00
parent 7675e2cd88
commit 1a0f99e346
18 changed files with 835 additions and 86 deletions
+50 -2
View File
@@ -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
}