Files
knowledge-governance/internal/platform/postgres/library_verification.go
T
姚章浩 b81fcd07e3
build / build (push) Successful in 1m57s
fix(release): 修复tableState文本身份与快照统计归属
tableState 非 resources 分支恢复直接扫描 text 身份,避免 queryRows 把普通
doc id/UUID/复合键/数字 id 当 JSON canonical 校验而失败;resources 仍使用
F01 精确引用闭包。隔离 PG 覆盖字符串、UUID、复合键与数字 ID。

F11:快照 HTTP 投影补 bindings/wiki_log_entries;作业统计按本作业
target_snapshot_id/snapshot_id 取对应快照,取不到显示未知,不借最新封存;
running/pending/failed/unknown 不再在无 detail 时显示完成。

F03:verifications 支持 snapshot_id 精确查询,返回 snapshot_latest 并只按
该快照回答 verified;抽屉“查看校验记录”按选中快照加载,不借用最新记录。

F08:显示主机探测时间;无真实 next-run 时说明尚未核验下一次执行;最近成功
单列未知,不以最近运行冒充。

验证:Go/前端全量、隔离 PG tableState/F01 对拍与 make verify 17 项通过。
实际 Current→Prepare→Verify ES 集成被共享 ES 磁盘 watermark 阻塞,未冒充。
2026-11-05 13:19:27 +08:00

122 lines
5.2 KiB
Go

package postgres
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/jackc/pgx/v5"
"knowledge-governance/internal/governance"
)
// 恢复验证证据:只由 worker 在真实演练结束后写入;这里只做落库与按当前目标读取。
//
// 读取永远带 target_id = 当前部署目标:换库/换索引/换桶之后旧证据自动失效,
// 不需要任何人去"记得把开关关掉"。
// SaveLibraryRestoreVerification 幂等登记一次演练结果(同一 target+scope+job 只留一条)。
func (s *Store) SaveLibraryRestoreVerification(ctx context.Context, verification governance.LibraryRestoreVerification) error {
if verification.ID == "" || verification.JobID == "" {
return fmt.Errorf("restore verification requires id and job id")
}
if verification.Scope != "internal" && verification.Scope != "external" {
return fmt.Errorf("restore verification scope %q is invalid", verification.Scope)
}
if verification.Status != governance.LibraryVerificationPassed && verification.Status != governance.LibraryVerificationFailed {
return fmt.Errorf("restore verification status %q is invalid", verification.Status)
}
report, err := json.Marshal(verification.Report)
if err != nil {
return fmt.Errorf("encode restore verification report: %w", err)
}
_, err = s.db.Exec(ctx, `
INSERT INTO library_restore_verifications (id, target_id, scope, status, snapshot_id, job_id, failure, report, started_at, finished_at)
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)
ON CONFLICT (target_id, scope, job_id) DO UPDATE
SET status = EXCLUDED.status, snapshot_id = EXCLUDED.snapshot_id, failure = EXCLUDED.failure,
report = EXCLUDED.report, started_at = EXCLUDED.started_at, finished_at = EXCLUDED.finished_at`,
verification.ID, s.libraryTargetID, verification.Scope, verification.Status,
verification.SnapshotID, verification.JobID, verification.Failure, report,
verification.StartedAt.UTC(), verification.FinishedAt.UTC())
if err != nil {
return fmt.Errorf("save restore verification: %w", err)
}
return nil
}
// LatestLibraryRestoreVerification 按完成时间取当前目标该 scope 的最近一条证据。
// 没有记录返回 (zero,false,nil):那不是错误,页面据此说"还没有验证过"。
func (s *Store) LatestLibraryRestoreVerification(ctx context.Context, scope string) (governance.LibraryRestoreVerification, bool, error) {
var (
item governance.LibraryRestoreVerification
report []byte
)
err := s.db.QueryRow(ctx, `
SELECT id, scope, status, snapshot_id, job_id, failure, report, started_at, finished_at
FROM library_restore_verifications
WHERE target_id=$1 AND scope=$2
ORDER BY finished_at DESC, created_at DESC
LIMIT 1`, s.libraryTargetID, scope).Scan(&item.ID, &item.Scope, &item.Status, &item.SnapshotID,
&item.JobID, &item.Failure, &report, &item.StartedAt, &item.FinishedAt)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return governance.LibraryRestoreVerification{}, false, nil
}
return governance.LibraryRestoreVerification{}, false, fmt.Errorf("load restore verification: %w", err)
}
item.Report = decodeEvidenceObject(report)
return item, true, nil
}
// LibraryRestoreVerifications 是页面用的轻量清单(最近 N 条),按目标过滤。
// LibraryRestoreVerificationForSnapshot 只按本次精确快照查证据,不能用最近 N 条
// 扫描代替:旧快照的报告可能已经掉出列表,但仍是该快照真实的恢复演练记录。
func (s *Store) LibraryRestoreVerificationForSnapshot(ctx context.Context, scope, snapshotID string) (governance.LibraryRestoreVerification, bool, error) {
var item governance.LibraryRestoreVerification
var report []byte
err := s.db.QueryRow(ctx, `
SELECT id, scope, status, snapshot_id, job_id, failure, report, started_at, finished_at
FROM library_restore_verifications
WHERE target_id=$1 AND scope=$2 AND snapshot_id=$3
ORDER BY finished_at DESC, created_at DESC
LIMIT 1`, s.libraryTargetID, scope, snapshotID).Scan(&item.ID, &item.Scope, &item.Status,
&item.SnapshotID, &item.JobID, &item.Failure, &report, &item.StartedAt, &item.FinishedAt)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return governance.LibraryRestoreVerification{}, false, nil
}
return governance.LibraryRestoreVerification{}, false, fmt.Errorf("load snapshot restore verification: %w", err)
}
item.Report = decodeEvidenceObject(report)
return item, true, nil
}
func (s *Store) LibraryRestoreVerifications(ctx context.Context, scope string, limit int) ([]governance.LibraryRestoreVerification, error) {
if limit <= 0 {
limit = 10
}
rows, err := s.db.Query(ctx, `
SELECT id, scope, status, snapshot_id, job_id, failure, started_at, finished_at
FROM library_restore_verifications
WHERE target_id=$1 AND scope=$2
ORDER BY finished_at DESC, created_at DESC
LIMIT $3`, s.libraryTargetID, scope, limit)
if err != nil {
return nil, fmt.Errorf("list restore verifications: %w", err)
}
defer rows.Close()
items := make([]governance.LibraryRestoreVerification, 0, limit)
for rows.Next() {
var item governance.LibraryRestoreVerification
if err := rows.Scan(&item.ID, &item.Scope, &item.Status, &item.SnapshotID, &item.JobID,
&item.Failure, &item.StartedAt, &item.FinishedAt); err != nil {
return nil, err
}
items = append(items, item)
}
return items, rows.Err()
}