resources 是多库共享表:采集看正文里的 resource://handle,校验只看 chunks 重算可见集, 两边口径不同,恢复得一字不差也会被判 15/19 而拒绝开放。改成按归档 id 核对存在性与内容摘要。
This commit is contained in:
@@ -1,5 +1,14 @@
|
||||
# Changelog
|
||||
|
||||
## 开发快照(恢复校验按归档身份核对共享表)
|
||||
|
||||
### Fixed
|
||||
|
||||
- **`resources` 校验改用归档身份集合**:resources 是多知识库共享表,采集口径看
|
||||
knowledges/chunks/wiki 正文里的 `resource://<handle>`,校验却只看 chunks 并重新推导
|
||||
"本库可见",两边口径不一致——恢复得一字不差也会被判"行数 15/19"而拒绝开放。
|
||||
现在按归档列出的 id 逐个核对存在性与逐行内容摘要;少一行、改一个字节都算失败。
|
||||
|
||||
## 开发快照(真实依赖跑出来的接线缺陷)
|
||||
|
||||
### Fixed
|
||||
|
||||
@@ -507,6 +507,13 @@ func (t *ProductionLibraryTarget) Verify(ctx context.Context, expect libraryrele
|
||||
if part.Kind != "database" && part.Kind != "wiki" {
|
||||
continue
|
||||
}
|
||||
var resourceIDs []string
|
||||
if part.Name == "resources" {
|
||||
resourceIDs, err = archivedResourceIDs(archive, part)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
wantCount, wantDigest, err := archivedTableState(archive, part)
|
||||
if errors.Is(err, errMissingIdentity) {
|
||||
// 个别表(例如日志表)没有代理主键:身份集合无从比,但行数与逐行内容摘要仍然必须一致。
|
||||
@@ -527,6 +534,13 @@ func (t *ProductionLibraryTarget) Verify(ctx context.Context, expect libraryrele
|
||||
return err
|
||||
}
|
||||
count, digest, err := t.tableState(ctx, part.Name)
|
||||
if part.Name == "resources" {
|
||||
// resources 是多知识库共享表,按"本库可见"重新推导会和采集口径打架
|
||||
// (采集看 knowledges/chunks/wiki 正文里的 handle,校验只看 chunks),
|
||||
// 于是恢复得一字不差也会被判失败。改成按归档的身份集合核对:
|
||||
// 归档里每一行都必须在、内容摘要也一致,缺一行就失败。
|
||||
count, digest, err = t.archivedResourceState(ctx, resourceIDs)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -538,7 +552,12 @@ func (t *ProductionLibraryTarget) Verify(ctx context.Context, expect libraryrele
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
contentCount, contentDigest, err := t.productionTableContent(ctx, part.Name, columns)
|
||||
contentCount, contentDigest := int64(0), ""
|
||||
if part.Name == "resources" {
|
||||
contentCount, contentDigest, err = t.productionResourceContent(ctx, resourceIDs, columns)
|
||||
} else {
|
||||
contentCount, contentDigest, err = t.productionTableContent(ctx, part.Name, columns)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -555,6 +574,54 @@ func (t *ProductionLibraryTarget) Verify(ctx context.Context, expect libraryrele
|
||||
return nil
|
||||
}
|
||||
|
||||
// archivedResourceState 只核对"归档里列出的这些资源"是否还在且未变。
|
||||
func (t *ProductionLibraryTarget) archivedResourceState(ctx context.Context, ids []string) (int64, string, error) {
|
||||
rows, err := t.pool.Query(ctx, `SELECT id FROM resources WHERE id = ANY($1::text[]) ORDER BY id`, ids)
|
||||
if err != nil {
|
||||
return 0, "", errors.New("读取生产 resources 失败")
|
||||
}
|
||||
defer rows.Close()
|
||||
found := make([]string, 0, len(ids))
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
found = append(found, id)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
return int64(len(found)), digestIDs(found), nil
|
||||
}
|
||||
|
||||
func archivedResourceIDs(archive *librarysnapshot.Archive, part librarysnapshot.Part) ([]string, error) {
|
||||
reader, err := archive.OpenPart(part)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer reader.Close()
|
||||
decoder := json.NewDecoder(reader)
|
||||
token, err := decoder.Token()
|
||||
if err != nil || token != json.Delim('[') {
|
||||
return nil, errors.New("归档部件不是行数组")
|
||||
}
|
||||
ids := make([]string, 0, 64)
|
||||
for decoder.More() {
|
||||
var row map[string]json.RawMessage
|
||||
if err := decoder.Decode(&row); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var id string
|
||||
if err := json.Unmarshal(row["id"], &id); err != nil || id == "" {
|
||||
return nil, errors.New("归档资源缺少身份")
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
sort.Strings(ids)
|
||||
return ids, nil
|
||||
}
|
||||
|
||||
func archivedTableState(archive *librarysnapshot.Archive, part librarysnapshot.Part) (int64, string, error) {
|
||||
reader, err := archive.OpenPart(part)
|
||||
if err != nil {
|
||||
|
||||
@@ -262,3 +262,36 @@ func (t *ProductionLibraryTarget) verifyIndexDigest(ctx context.Context, archive
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// productionResourceContent 只读归档里列出的那些资源行:resources 是多知识库
|
||||
// 共享表,按库重新推导会漏掉"只被历史上某条正文引用过"的行,恢复得一字不差
|
||||
// 也会被判失败。这里按身份集合取行,再逐行做内容摘要。
|
||||
func (t *ProductionLibraryTarget) productionResourceContent(ctx context.Context, ids []string, columns map[string]bool) (int64, string, error) {
|
||||
rows, err := t.pool.Query(ctx, `SELECT to_jsonb(r) FROM resources r WHERE r.id = ANY($1::text[]) ORDER BY r.id`, ids)
|
||||
if err != nil {
|
||||
return 0, "", fmt.Errorf("读取生产 resources 内容失败: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
digests := make([]string, 0, len(ids))
|
||||
for rows.Next() {
|
||||
var raw map[string]json.RawMessage
|
||||
if err := rows.Scan(&raw); err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
projected := make(map[string]json.RawMessage, len(columns))
|
||||
for key := range columns {
|
||||
if value, ok := raw[key]; ok {
|
||||
projected[key] = value
|
||||
}
|
||||
}
|
||||
digest, err := canonicalRowDigest(projected)
|
||||
if err != nil {
|
||||
return 0, "", err
|
||||
}
|
||||
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