[优化] 修复数据库迁移问题

This commit is contained in:
ryan
2026-06-01 17:15:17 +08:00
parent df63b0113a
commit 663da01bda
5 changed files with 46 additions and 8 deletions
+6 -1
View File
@@ -64,7 +64,12 @@ func migrateV19(ctx Context, db *gorm.DB, backend string) error {
slog.Info("dropped tunnel_id column from proxy_routes")
}
return ctx.ApplyCurrentSchema(db, backend)
slog.Info("v19: starting ApplyCurrentSchema (auto-migrate all tables)")
if err := ctx.ApplyCurrentSchema(db, backend); err != nil {
return err
}
slog.Info("v19: ApplyCurrentSchema completed")
return nil
}
func validateV19(ctx Context, db *gorm.DB, backend string) error {
+27 -4
View File
@@ -38,15 +38,38 @@ func migrateV21(ctx Context, db *gorm.DB, backend string) error {
}
}
// Rename agent_version → version (target may already exist if AutoMigrate ran earlier)
if migrator.HasColumn(&nodeV21{}, "agent_version") {
if err := migrator.RenameColumn(&nodeV21{}, "agent_version", "version"); err != nil {
return fmt.Errorf("failed to rename agent_version to version: %w", err)
if migrator.HasColumn(&nodeV21{}, "version") {
// version already created by AutoMigrate with empty default; backfill from agent_version
slog.Info("v21: version column already exists, backfilling from agent_version")
if err := db.Exec(`UPDATE nodes SET version = agent_version WHERE version = ''`).Error; err != nil {
return fmt.Errorf("failed to backfill version from agent_version: %w", err)
}
if err := migrator.DropColumn(&nodeV21{}, "agent_version"); err != nil {
slog.Warn("failed to drop agent_version after backfill", "error", err)
}
} else {
if err := migrator.RenameColumn(&nodeV21{}, "agent_version", "version"); err != nil {
return fmt.Errorf("failed to rename agent_version to version: %w", err)
}
}
}
// Rename nginx_version → ext_version (target may already exist if AutoMigrate ran earlier)
if migrator.HasColumn(&nodeV21{}, "nginx_version") {
if err := migrator.RenameColumn(&nodeV21{}, "nginx_version", "ext_version"); err != nil {
return fmt.Errorf("failed to rename nginx_version to ext_version: %w", err)
if migrator.HasColumn(&nodeV21{}, "ext_version") {
slog.Info("v21: ext_version column already exists, backfilling from nginx_version")
if err := db.Exec(`UPDATE nodes SET ext_version = nginx_version WHERE ext_version IS NULL OR ext_version = ''`).Error; err != nil {
return fmt.Errorf("failed to backfill ext_version from nginx_version: %w", err)
}
if err := migrator.DropColumn(&nodeV21{}, "nginx_version"); err != nil {
slog.Warn("failed to drop nginx_version after backfill", "error", err)
}
} else {
if err := migrator.RenameColumn(&nodeV21{}, "nginx_version", "ext_version"); err != nil {
return fmt.Errorf("failed to rename nginx_version to ext_version: %w", err)
}
}
}