[优化] 更新数据库迁移逻辑,添加 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
+1 -1
View File
@@ -113,7 +113,7 @@ func AgentSyncWAFIPGroups(c *gin.Context) {
// @Success 200 {object} map[string]interface{} // @Success 200 {object} map[string]interface{}
// @Router /api/agent/config-versions/active [get] // @Router /api/agent/config-versions/active [get]
func AgentGetActiveConfig(c *gin.Context) { func AgentGetActiveConfig(c *gin.Context) {
authNode, ok := c.Get("node") authNode, ok := c.Get("agent_node")
if !ok { if !ok {
respondUnauthorized(c, "Node object missing from context") respondUnauthorized(c, "Node object missing from context")
return return
+13 -6
View File
@@ -11,7 +11,9 @@ type nodeV16 struct {
RelayBindPort int `gorm:"column:relay_bind_port"` RelayBindPort int `gorm:"column:relay_bind_port"`
} }
type tunnelV16 struct{} type tunnelV16 struct {
ID uint `gorm:"primaryKey"`
}
type proxyRouteV16 struct { type proxyRouteV16 struct {
UpstreamType string `gorm:"column:upstream_type;not null;default:'direct'"` 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 { if err := ctx.ApplyCurrentSchema(db, backend); err != nil {
return err 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 { 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) 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 { if err := ctx.ValidateDatabaseSchemaVersion(db, backend, 15); err != nil {
return err return err
} }
if db == nil || !db.Migrator().HasTable(&tunnelV16{}) { if !db.Migrator().HasColumn(&proxyRouteV16{}, "tunnel_node_id") {
return fmt.Errorf("table tunnels is missing") 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") { if !db.Migrator().HasColumn(&nodeV16{}, "node_type") {
return fmt.Errorf("column nodes.node_type is missing") 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") { if !db.Migrator().HasColumn(&nodeV16{}, "relay_bind_port") {
return fmt.Errorf("column nodes.relay_bind_port is missing") 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 return nil
} }
+9 -3
View File
@@ -14,6 +14,12 @@ type proxyRouteV19 struct {
UpstreamType string `gorm:"column:upstream_type"` UpstreamType string `gorm:"column:upstream_type"`
} }
type tunnelV19 struct{}
func (tunnelV19) TableName() string {
return "tunnels"
}
func (proxyRouteV19) TableName() string { func (proxyRouteV19) TableName() string {
return "proxy_routes" return "proxy_routes"
} }
@@ -33,8 +39,8 @@ func V19() Migration {
func migrateV19(ctx Context, db *gorm.DB, backend string) error { func migrateV19(ctx Context, db *gorm.DB, backend string) error {
// Drop tunnels table // Drop tunnels table
if db.Migrator().HasTable("tunnels") { if db.Migrator().HasTable(&tunnelV19{}) {
if err := db.Migrator().DropTable("tunnels"); err != nil { if err := db.Migrator().DropTable(&tunnelV19{}); err != nil {
return fmt.Errorf("failed to drop tunnels table: %w", err) return fmt.Errorf("failed to drop tunnels table: %w", err)
} }
slog.Info("dropped tunnels table") slog.Info("dropped tunnels table")
@@ -66,7 +72,7 @@ func validateV19(ctx Context, db *gorm.DB, backend string) error {
return err return err
} }
if db.Migrator().HasTable("tunnels") { if db.Migrator().HasTable(&tunnelV19{}) {
return fmt.Errorf("table tunnels should be dropped in v19") return fmt.Errorf("table tunnels should be dropped in v19")
} }
+25 -2
View File
@@ -82,6 +82,8 @@ func (databaseSchemaMigrationContext) ValidateDatabaseSchemaVersion(db *gorm.DB,
return validateDatabaseSchemaV17(db, backend) return validateDatabaseSchemaV17(db, backend)
case 18: case 18:
return validateDatabaseSchemaV18(db, backend) return validateDatabaseSchemaV18(db, backend)
case 19:
return validateDatabaseSchemaV19(db, backend)
default: default:
return fmt.Errorf("database schema validation for v%d is not defined", version) return fmt.Errorf("database schema validation for v%d is not defined", version)
} }
@@ -1164,8 +1166,13 @@ func validateDatabaseSchemaV16(db *gorm.DB, backend string) error {
if err := validateDatabaseSchemaV15(db, backend); err != nil { if err := validateDatabaseSchemaV15(db, backend); err != nil {
return err return err
} }
if !db.Migrator().HasTable("tunnels") { if !db.Migrator().HasColumn(&ProxyRoute{}, "tunnel_node_id") {
return fmt.Errorf("table tunnels is missing") if !db.Migrator().HasTable("tunnels") {
return fmt.Errorf("table tunnels is missing")
}
if !db.Migrator().HasColumn(&ProxyRoute{}, "tunnel_id") {
return fmt.Errorf("column proxy_routes.tunnel_id is missing")
}
} }
if !db.Migrator().HasColumn(&Node{}, "node_type") { if !db.Migrator().HasColumn(&Node{}, "node_type") {
return fmt.Errorf("column nodes.node_type is missing") return fmt.Errorf("column nodes.node_type is missing")
@@ -1202,6 +1209,22 @@ func validateDatabaseSchemaV18(db *gorm.DB, backend string) error {
return nil return nil
} }
func validateDatabaseSchemaV19(db *gorm.DB, backend string) error {
if err := validateDatabaseSchemaV18(db, backend); err != nil {
return err
}
if db.Migrator().HasTable("tunnels") {
return fmt.Errorf("table tunnels should be dropped in v19")
}
if !db.Migrator().HasColumn(&ProxyRoute{}, "tunnel_node_id") {
return fmt.Errorf("column proxy_routes.tunnel_node_id is missing")
}
if db.Migrator().HasColumn(&ProxyRoute{}, "tunnel_id") {
return fmt.Errorf("column proxy_routes.tunnel_id should be dropped in v19")
}
return nil
}
func databaseSchemaMigrations() []databaseSchemaMigration { func databaseSchemaMigrations() []databaseSchemaMigration {
ctx := databaseSchemaMigrationContext{} ctx := databaseSchemaMigrationContext{}
migrations := []databaseSchemaMigration{} migrations := []databaseSchemaMigration{}