F02:测试封存作业只展示只读导出测试版本,不再套用备份生产/同步 Wiki/ 生产验证/生产生效,也不再写目标维护;运行和失败终态都保留真实 export 状态。同一挂载覆盖 running→failed。 F03:版本列表按每个 snapshot_id 精确查询恢复验证记录,与详情使用同一 endpoint;同 scope 最新 passed 不再给其他快照染色,未演练与读取失败分开。 封存失败:已恢复写入者且确定不可重试的捕获校验错误使用永久失败,避免 第二次 attempt 的 already exists 覆盖第一因;业务 job 的 error 在失败重试 中保留第一条,成功时清除。瞬时不可达/超时仍保留重试。 验证:Go/前端全量、隔离 PG 精确快照与首因测试、临时独立 ES 的 Current→Prepare→Verify PASS、make verify 17 项。host libraryctl 仍是旧 f9c15a8f...dirty,360 镜像未替换 host 工具,本轮只单独准备包不切换。
This commit is contained in:
@@ -247,7 +247,14 @@ func (s *Store) AdvanceLibraryJob(ctx context.Context, jobID string, state gover
|
||||
UPDATE library_jobs SET
|
||||
state=$2,
|
||||
stage = CASE WHEN $3='' THEN stage ELSE $3 END,
|
||||
error = CASE WHEN $4='' THEN error ELSE $4 END,
|
||||
-- 失败重试不得把第一因覆盖成第二次“already exists”;成功时清除旧错误,
|
||||
-- 但新的 failed 状态保留已记录的第一条失败原因。
|
||||
error = CASE
|
||||
WHEN $2='succeeded' THEN ''
|
||||
WHEN $4='' THEN error
|
||||
WHEN $2='failed' AND error<>'' THEN error
|
||||
ELSE $4
|
||||
END,
|
||||
recovery = CASE WHEN $5='' THEN recovery ELSE $5 END,
|
||||
snapshot_id = CASE WHEN $6='' THEN snapshot_id ELSE $6 END,
|
||||
target_snapshot_id = CASE WHEN $7='' THEN target_snapshot_id ELSE $7 END,
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
// F02 retry regression: a second failed attempt must not overwrite the first
|
||||
// cause in the persistent business job; a later success clears it.
|
||||
func TestAdvanceLibraryJobPreservesFirstFailure(t *testing.T) {
|
||||
dsn := os.Getenv("KG_LIBRARY_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("requires explicitly isolated kg_library_test_ PostgreSQL database")
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil || !strings.HasPrefix(cfg.ConnConfig.Database, "kg_library_test_") {
|
||||
t.Fatal("isolated database required")
|
||||
}
|
||||
cfg.MaxConns = 1
|
||||
cfg.ConnConfig.RuntimeParams["search_path"] = "pg_temp"
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
if _, err := pool.Exec(ctx, `
|
||||
CREATE TEMP TABLE library_jobs(
|
||||
id text primary key, state text, stage text, error text, recovery text,
|
||||
snapshot_id text, target_snapshot_id text, updated_at timestamptz, finished_at timestamptz);
|
||||
INSERT INTO library_jobs(id,state,stage,error) VALUES('libseal-retry','running','export','');`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := NewStore(pool)
|
||||
now := time.Now().UTC()
|
||||
if err := store.AdvanceLibraryJob(ctx, "libseal-retry", governance.LibraryJobFailed, "export", "first validation failure", "", "", "", now); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := store.AdvanceLibraryJob(ctx, "libseal-retry", governance.LibraryJobFailed, "export", "second already exists", "", "", "", now.Add(time.Second)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var got string
|
||||
if err := pool.QueryRow(ctx, `SELECT error FROM library_jobs WHERE id='libseal-retry'`).Scan(&got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "first validation failure" {
|
||||
t.Fatalf("first failure overwritten: %q", got)
|
||||
}
|
||||
if err := store.AdvanceLibraryJob(ctx, "libseal-retry", governance.LibraryJobSucceeded, "export", "", "", "snap-ok", "", now.Add(2*time.Second)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := pool.QueryRow(ctx, `SELECT error FROM library_jobs WHERE id='libseal-retry'`).Scan(&got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != "" {
|
||||
t.Fatalf("success must clear old error, got %q", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"knowledge-governance/internal/governance"
|
||||
)
|
||||
|
||||
// F03 regression against real PostgreSQL: exact snapshot lookup must not fall
|
||||
// back to the target's newest passed record. Session temp table only.
|
||||
func TestLibraryRestoreVerificationForSnapshotExactPG(t *testing.T) {
|
||||
dsn := os.Getenv("KG_LIBRARY_TEST_DATABASE_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("requires explicitly isolated kg_library_test_ PostgreSQL database")
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil || !strings.HasPrefix(cfg.ConnConfig.Database, "kg_library_test_") {
|
||||
t.Fatal("isolated database required")
|
||||
}
|
||||
cfg.MaxConns = 1
|
||||
cfg.ConnConfig.RuntimeParams["search_path"] = "pg_temp"
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
_, err = pool.Exec(ctx, `
|
||||
CREATE TEMP TABLE library_restore_verifications(
|
||||
id text, target_id text, scope text, status text, snapshot_id text, job_id text,
|
||||
failure text, report jsonb, started_at timestamptz, finished_at timestamptz, created_at timestamptz DEFAULT now());
|
||||
INSERT INTO library_restore_verifications
|
||||
(id,target_id,scope,status,snapshot_id,job_id,failure,report,started_at,finished_at,created_at) VALUES
|
||||
('v-new','target-exact','internal','passed','snap-new','job-new','','{}',now()-interval '1 hour',now()-interval '1 hour',now()),
|
||||
('v-old','target-exact','internal','failed','snap-old','job-old','old restore mismatch','{}',now()-interval '2 hour',now()-interval '2 hour',now()-interval '2 hour'),
|
||||
('v-other','other-target','internal','passed','snap-old','job-other','','{}',now(),now(),now());`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
store := NewStore(pool).WithLibraryTarget("target-exact")
|
||||
item, found, err := store.LibraryRestoreVerificationForSnapshot(ctx, "internal", "snap-old")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !found || item.JobID != "job-old" || item.Status != governance.LibraryVerificationFailed {
|
||||
t.Fatalf("exact snapshot borrowed another record: found=%v item=%+v", found, item)
|
||||
}
|
||||
if _, found, err := store.LibraryRestoreVerificationForSnapshot(ctx, "internal", "snap-missing"); err != nil || found {
|
||||
t.Fatalf("missing snapshot must be explicit no-evidence: found=%v err=%v", found, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user