feat(deploy): 在线更新验收 - 新增 schema v2 迁移与备份提示

This commit is contained in:
ddCat
2026-08-30 23:21:37 +08:00
parent 3aefefc1aa
commit 3f16cf19aa
6 changed files with 19 additions and 3 deletions

View File

@@ -8,9 +8,10 @@ import (
"gorm.io/gorm"
)
const CurrentSchemaVersion int64 = 1
const CurrentSchemaVersion int64 = 2
const baselineSchemaChecksum = "sha256:open-ai-canvas-schema-v1-20260830"
const schemaMigrationAppliedAtIndexChecksum = "sha256:schema-migrations-applied-at-index-v2-20260830"
const postgresSchemaMigrationLockID int64 = 73123910420260830
@@ -38,6 +39,11 @@ type migration struct {
var schemaMigrations = []migration{
{version: 1, name: "baseline_gorm_schema", checksum: baselineSchemaChecksum, apply: migrateSchemaV1},
{version: 2, name: "schema_migrations_applied_at_index", checksum: schemaMigrationAppliedAtIndexChecksum, apply: migrateSchemaV2},
}
func migrateSchemaV2(tx *gorm.DB) error {
return tx.Exec("CREATE INDEX IF NOT EXISTS idx_schema_migrations_applied_at ON schema_migrations (applied_at)").Error
}
func MigrateSchema(db *gorm.DB) error {

View File

@@ -23,6 +23,9 @@ func TestMigrateSchemaRecordsAndValidatesVersion(t *testing.T) {
if !status.Ready || status.Current != CurrentSchemaVersion {
t.Fatalf("unexpected schema status: %#v", status)
}
if !db.Migrator().HasIndex(&schemaMigration{}, "idx_schema_migrations_applied_at") {
t.Fatal("schema migration v2 did not create the applied_at index")
}
if err := MigrateSchema(db); err != nil {
t.Fatalf("migration should be idempotent: %v", err)
}