From 343a005db15a628c0a4709a5833a57e9cf6bba22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Fri, 9 Oct 2026 13:55:35 +0800 Subject: [PATCH] =?UTF-8?q?test(backup):=20=E8=BF=81=E7=A7=BB=E8=AE=A1?= =?UTF-8?q?=E6=95=B0=E6=9B=B4=E6=96=B0=E5=88=B0=2045=20=E5=B9=B6=E8=A1=A5?= =?UTF-8?q?=2044/45=20=E5=86=85=E5=AE=B9=E6=96=AD=E8=A8=80=EF=BC=9B?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20backup=5Fschedule=20=E7=9C=9F=E5=AE=9E?= =?UTF-8?q?=E5=BA=93=E5=BE=80=E8=BF=94=E9=9B=86=E6=88=90=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - migrations_test.go 之前硬编码 44:新增 000045 后该包测试失败(全量套件里才暴露) - 现在断言 migration 44 = production_kb_bindings、45 = backup_schedule - 新增 TestBackupScheduleRoundTripIntegration:默认值 → 保存(upsert 单行) → 读回(含 updated_by/at) → 记录下发结果(不改计划);实测在 governance 库隔离 schema 上 PASS(0.95s) --- .../backup_schedule_integration_test.go | 90 +++++++++++++++++++ internal/platform/postgres/migrations_test.go | 11 ++- 2 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 internal/platform/postgres/backup_schedule_integration_test.go diff --git a/internal/platform/postgres/backup_schedule_integration_test.go b/internal/platform/postgres/backup_schedule_integration_test.go new file mode 100644 index 0000000..48a9dfc --- /dev/null +++ b/internal/platform/postgres/backup_schedule_integration_test.go @@ -0,0 +1,90 @@ +package postgres + +import ( + "context" + "os" + "testing" + "time" + + "github.com/jackc/pgx/v5/pgxpool" + + "knowledge-governance/internal/governance" +) + +// 备份定时计划的真实库往返:默认值 → 保存 → 读回 → 记录下发结果。 +// 页面靠这几步判定"库里设置"与"主机上生效计划"是否一致(待下发/已一致)。 +func TestBackupScheduleRoundTripIntegration(t *testing.T) { + databaseURL := isolatedIntegrationDatabaseURL(t, os.Getenv("TEST_DATABASE_URL")) + if databaseURL == "" { + t.Skip("TEST_DATABASE_URL is not set") + } + ctx := context.Background() + pool, err := pgxpool.New(ctx, databaseURL) + if err != nil { + t.Fatal(err) + } + defer pool.Close() + if err := ApplyMigrations(ctx, pool); err != nil { + t.Fatal(err) + } + store := NewStore(pool) + + initial, err := store.LoadBackupSchedule(ctx) + if err != nil { + t.Fatal(err) + } + if initial.Enabled || initial.ScheduleTime != governance.BackupScheduleDefaultTime || + initial.RetentionDays != 7 || initial.RetentionMonths != 3 { + t.Fatalf("迁移后的默认计划不对: %#v", initial) + } + + now := time.Date(2026, 10, 9, 12, 0, 0, 0, time.UTC) + next := governance.BackupSchedule{ + Enabled: true, ScheduleTime: "04:15", RetentionDays: 14, RetentionMonths: 6, + } + if err := store.SaveBackupSchedule(ctx, next, "admin", now); err != nil { + t.Fatal(err) + } + saved, err := store.LoadBackupSchedule(ctx) + if err != nil { + t.Fatal(err) + } + if !saved.Enabled || saved.ScheduleTime != "04:15" || saved.RetentionDays != 14 || saved.RetentionMonths != 6 { + t.Fatalf("保存后读回不对: %#v", saved) + } + if saved.UpdatedBy != "admin" || !saved.UpdatedAt.Equal(now) { + t.Fatalf("应记录操作人与时间: %#v", saved) + } + if !next.SameHostPlan(saved) { + t.Fatal("保存的计划应与主机下发字段一致(SameHostPlan)") + } + + // 重复保存走 upsert,不应产生第二行。 + if err := store.SaveBackupSchedule(ctx, next, "admin2", now.Add(time.Minute)); err != nil { + t.Fatal(err) + } + var rows int + if err := pool.QueryRow(ctx, `SELECT count(*) FROM backup_schedule`).Scan(&rows); err != nil { + t.Fatal(err) + } + if rows != 1 { + t.Fatalf("单行表出现 %d 行", rows) + } + + appliedAt := now.Add(2 * time.Minute) + if err := store.MarkBackupScheduleApplied(ctx, "每天 04:15 · 保留 14 天 · 每月归档 6 份", appliedAt); err != nil { + t.Fatal(err) + } + afterApply, err := store.LoadBackupSchedule(ctx) + if err != nil { + t.Fatal(err) + } + if afterApply.LastAppliedAt == nil || !afterApply.LastAppliedAt.Equal(appliedAt) || + afterApply.LastAppliedSummary == "" { + t.Fatalf("下发结果未落库: %#v", afterApply) + } + // 记录下发不得改动计划本身。 + if !afterApply.SameHostPlan(saved) { + t.Fatal("记录下发不应改变计划") + } +} diff --git a/internal/platform/postgres/migrations_test.go b/internal/platform/postgres/migrations_test.go index f0670e1..be69e91 100644 --- a/internal/platform/postgres/migrations_test.go +++ b/internal/platform/postgres/migrations_test.go @@ -11,8 +11,8 @@ func TestEmbeddedMigrationsAreVersionSorted(t *testing.T) { if err != nil { t.Fatalf("loadMigrations() error = %v", err) } - if len(migrations) != 44 { - t.Fatalf("migration count = %d, want 44", len(migrations)) + if len(migrations) != 45 { + t.Fatalf("migration count = %d, want 45", len(migrations)) } if migrations[0].Version != 1 || !strings.Contains(migrations[0].UpSQL, "CREATE TABLE IF NOT EXISTS jobs") { t.Fatalf("migration 1 = %#v", migrations[0]) @@ -108,6 +108,13 @@ func TestEmbeddedMigrationsAreVersionSorted(t *testing.T) { if migrations[42].Version != 43 || !strings.Contains(migrations[42].UpSQL, "test_weknora_base_url") || !strings.Contains(migrations[42].UpSQL, "production_tenant_id") || !strings.Contains(migrations[42].UpSQL, "production_knowledge_base_id") { t.Fatalf("migration 43 = %#v", migrations[42]) } + if migrations[43].Version != 44 || !strings.Contains(migrations[43].UpSQL, "CREATE TABLE production_kb_bindings") { + t.Fatalf("migration 44 = %#v", migrations[43]) + } + // 000045:整库逻辑备份的定时计划(系统设置 → 存储与备份)。 + if migrations[44].Version != 45 || !strings.Contains(migrations[44].UpSQL, "CREATE TABLE backup_schedule") { + t.Fatalf("migration 45 = %#v", migrations[44]) + } } func TestProcessingConfirmationMigrationSeparatesPlanningFromUpload(t *testing.T) {