[优化] 更新数据库迁移逻辑,添加 v19 版本验证,重构隧道相关表结构

This commit is contained in:
ryan
2026-06-01 14:06:36 +08:00
parent af36676e2e
commit 394e3c4855
4 changed files with 48 additions and 12 deletions
+13 -6
View File
@@ -11,7 +11,9 @@ type nodeV16 struct {
RelayBindPort int `gorm:"column:relay_bind_port"`
}
type tunnelV16 struct{}
type tunnelV16 struct {
ID uint `gorm:"primaryKey"`
}
type proxyRouteV16 struct {
UpstreamType string `gorm:"column:upstream_type;not null;default:'direct'"`
@@ -47,6 +49,9 @@ func migrateV16(ctx Context, db *gorm.DB, backend string) error {
if err := ctx.ApplyCurrentSchema(db, backend); err != nil {
return err
}
if err := db.AutoMigrate(&tunnelV16{}); err != nil {
return fmt.Errorf("auto migrate tunnelV16: %w", err)
}
if err := db.Exec("UPDATE nodes SET node_type = 'edge_node' WHERE node_type = '' OR node_type IS NULL").Error; err != nil {
return fmt.Errorf("backfill nodes.node_type: %w", err)
}
@@ -60,8 +65,13 @@ func validateV16(ctx Context, db *gorm.DB, backend string) error {
if err := ctx.ValidateDatabaseSchemaVersion(db, backend, 15); err != nil {
return err
}
if db == nil || !db.Migrator().HasTable(&tunnelV16{}) {
return fmt.Errorf("table tunnels is missing")
if !db.Migrator().HasColumn(&proxyRouteV16{}, "tunnel_node_id") {
if db == nil || !db.Migrator().HasTable(&tunnelV16{}) {
return fmt.Errorf("table tunnels is missing")
}
if !db.Migrator().HasColumn(&proxyRouteV16{}, "tunnel_id") {
return fmt.Errorf("column proxy_routes.tunnel_id is missing")
}
}
if !db.Migrator().HasColumn(&nodeV16{}, "node_type") {
return fmt.Errorf("column nodes.node_type is missing")
@@ -72,8 +82,5 @@ func validateV16(ctx Context, db *gorm.DB, backend string) error {
if !db.Migrator().HasColumn(&nodeV16{}, "relay_bind_port") {
return fmt.Errorf("column nodes.relay_bind_port is missing")
}
if !db.Migrator().HasColumn(&proxyRouteV16{}, "tunnel_id") {
return fmt.Errorf("column proxy_routes.tunnel_id is missing")
}
return nil
}