发布管理:maintenanceSummary 只看 job.state === 'failed' 就给 held 处置文案; 窗口已 released 的旧作业(libsync-internal-1793399248431728863)因此自相矛盾。 改为仅 held 时给 recoveryError,已释放时给 releasedFailure(如实记录失败,不提供强制成功)。 版本记录:LoadVersionHistorySyncState 把可空的 finished_at 扫进 time.Time; 部署重启触发的同步在"进行中"时该列为 NULL,扫描失败使整页 500 (nginx access log:GET /api/v1/version-history?limit=30 → 500,74 字节错误体)。 改为指针接值再判空。前端同时区分"未配置/加载失败"并显示服务端 code。 RED/GREEN:新增 Go 集成用例(NULL finished_at)、两条前端用例; make verify 16/16 通过(local 档案)。
This commit is contained in:
@@ -171,11 +171,14 @@ WHERE repo_key = $1 AND status = 'completed' ORDER BY id DESC LIMIT 1`, repoKey)
|
||||
func (s *Store) LoadVersionHistorySyncState(ctx context.Context, repoKey string) (governance.VersionHistorySyncRun, error) {
|
||||
var run governance.VersionHistorySyncRun
|
||||
var expected *int
|
||||
// finished_at 在"进行中"的 run 上是 NULL(列可空)。用指针接再判空:
|
||||
// 直接扫进 time.Time 会让整页 500,而部署重启本身就会触发一次同步。
|
||||
var finishedAt *time.Time
|
||||
err := s.db.QueryRow(ctx, `
|
||||
SELECT id, job_id, status, source_head_sha, started_at, finished_at, complete,
|
||||
fetched_count, expected_count, error_code, triggered_by
|
||||
FROM version_history_sync_runs WHERE repo_key = $1 ORDER BY id DESC LIMIT 1`, repoKey).
|
||||
Scan(&run.ID, &run.JobID, &run.Status, &run.SourceHeadSHA, &run.StartedAt, &run.FinishedAt,
|
||||
Scan(&run.ID, &run.JobID, &run.Status, &run.SourceHeadSHA, &run.StartedAt, &finishedAt,
|
||||
&run.Complete, &run.FetchedCount, &expected, &run.ErrorCode, &run.TriggeredBy)
|
||||
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
return governance.VersionHistorySyncRun{}, fmt.Errorf("查询同步状态:%w", err)
|
||||
@@ -183,6 +186,9 @@ FROM version_history_sync_runs WHERE repo_key = $1 ORDER BY id DESC LIMIT 1`, re
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return governance.VersionHistorySyncRun{}, nil
|
||||
}
|
||||
if finishedAt != nil {
|
||||
run.FinishedAt = *finishedAt
|
||||
}
|
||||
run.ExpectedCount = expected
|
||||
if snapshot, err := s.ActiveVersionHistorySnapshot(ctx, repoKey); err != nil {
|
||||
return governance.VersionHistorySyncRun{}, err
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// 真实缺陷:同步进行中的 run(finished_at 为 NULL)会让 LoadVersionHistorySyncState
|
||||
// 扫描失败,整个版本记录页 500。部署重启会自动触发一次同步,所以页面第一次打开就撞上。
|
||||
func TestVersionHistorySyncStateToleratesRunningRunWithoutFinishTime(t *testing.T) {
|
||||
dsn := os.Getenv("KG_VERSION_HISTORY_TEST_DSN")
|
||||
if dsn == "" {
|
||||
t.Skip("explicit disposable version-history DB required")
|
||||
}
|
||||
if !strings.Contains(dsn, "127.0.0.1") || !strings.Contains(dsn, "/kg_version_history_test_") {
|
||||
t.Fatal("refusing shared database")
|
||||
}
|
||||
ctx := context.Background()
|
||||
pool, err := pgxpool.New(ctx, dsn)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer pool.Close()
|
||||
if err = ApplyMigrations(ctx, pool); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := "fixture/repo"
|
||||
if _, err = pool.Exec(ctx, `DELETE FROM version_history_sync_runs WHERE repo_key = $1`, repo); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 一条已完成的历史 run(提供 last_success_at 与快照)
|
||||
if _, err = pool.Exec(ctx, `
|
||||
INSERT INTO version_history_sync_runs
|
||||
(repo_key, job_id, status, source_head_sha, started_at, finished_at, last_success_at,
|
||||
complete, fetched_count, expected_count, error_code, triggered_by)
|
||||
VALUES ($1,'job-1','completed',$2, now() - interval '2 hours', now() - interval '2 hours',
|
||||
now() - interval '2 hours', true, 10, 10, '', 'fixture')`, repo, strings.Repeat("a", 40)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// 一条正在跑的 run:finished_at 必须是 NULL(这正是线上触发 500 的行)
|
||||
if _, err = pool.Exec(ctx, `
|
||||
INSERT INTO version_history_sync_runs
|
||||
(repo_key, job_id, status, source_head_sha, started_at, finished_at,
|
||||
complete, fetched_count, expected_count, error_code, triggered_by)
|
||||
VALUES ($1,'job-2','running',$2, now(), NULL, false, 3, 12, '', 'fixture')`, repo, strings.Repeat("b", 40)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
store := NewStore(pool)
|
||||
state, err := store.LoadVersionHistorySyncState(ctx, repo)
|
||||
if err != nil {
|
||||
t.Fatalf("同步进行中读取同步状态不应失败:%v", err)
|
||||
}
|
||||
if state.Status != "running" {
|
||||
t.Fatalf("应读到最新(进行中)的 run,实际 status=%q", state.Status)
|
||||
}
|
||||
if !state.FinishedAt.IsZero() {
|
||||
t.Fatalf("进行中的 run 不应有结束时间,实际 %v", state.FinishedAt)
|
||||
}
|
||||
if state.LastSuccessAt.IsZero() {
|
||||
t.Fatal("应保留上一次成功时间")
|
||||
}
|
||||
if !state.LastSuccessAt.Before(time.Now()) {
|
||||
t.Fatalf("上次成功时间异常:%v", state.LastSuccessAt)
|
||||
}
|
||||
|
||||
// 列表读取同样不能因为这一行而失败
|
||||
items, _, _, snapshot, err := store.LoadVersionHistoryPage(ctx, repo, "all", "", 5)
|
||||
if err != nil {
|
||||
t.Fatalf("进行中读取分页也不应失败:%v", err)
|
||||
}
|
||||
if len(items) != 0 || snapshot == 0 {
|
||||
t.Fatalf("空快照应返回空页与快照号,实际 items=%d snapshot=%d", len(items), snapshot)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user