mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-09-29 05:56:38 +08:00
[优化] 更新数据库迁移逻辑,添加 v19 版本验证,重构隧道相关表结构
This commit is contained in:
@@ -113,7 +113,7 @@ func AgentSyncWAFIPGroups(c *gin.Context) {
|
||||
// @Success 200 {object} map[string]interface{}
|
||||
// @Router /api/agent/config-versions/active [get]
|
||||
func AgentGetActiveConfig(c *gin.Context) {
|
||||
authNode, ok := c.Get("node")
|
||||
authNode, ok := c.Get("agent_node")
|
||||
if !ok {
|
||||
respondUnauthorized(c, "Node object missing from context")
|
||||
return
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -14,6 +14,12 @@ type proxyRouteV19 struct {
|
||||
UpstreamType string `gorm:"column:upstream_type"`
|
||||
}
|
||||
|
||||
type tunnelV19 struct{}
|
||||
|
||||
func (tunnelV19) TableName() string {
|
||||
return "tunnels"
|
||||
}
|
||||
|
||||
func (proxyRouteV19) TableName() string {
|
||||
return "proxy_routes"
|
||||
}
|
||||
@@ -33,8 +39,8 @@ func V19() Migration {
|
||||
|
||||
func migrateV19(ctx Context, db *gorm.DB, backend string) error {
|
||||
// Drop tunnels table
|
||||
if db.Migrator().HasTable("tunnels") {
|
||||
if err := db.Migrator().DropTable("tunnels"); err != nil {
|
||||
if db.Migrator().HasTable(&tunnelV19{}) {
|
||||
if err := db.Migrator().DropTable(&tunnelV19{}); err != nil {
|
||||
return fmt.Errorf("failed to drop tunnels table: %w", err)
|
||||
}
|
||||
slog.Info("dropped tunnels table")
|
||||
@@ -66,7 +72,7 @@ func validateV19(ctx Context, db *gorm.DB, backend string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if db.Migrator().HasTable("tunnels") {
|
||||
if db.Migrator().HasTable(&tunnelV19{}) {
|
||||
return fmt.Errorf("table tunnels should be dropped in v19")
|
||||
}
|
||||
|
||||
|
||||
@@ -82,6 +82,8 @@ func (databaseSchemaMigrationContext) ValidateDatabaseSchemaVersion(db *gorm.DB,
|
||||
return validateDatabaseSchemaV17(db, backend)
|
||||
case 18:
|
||||
return validateDatabaseSchemaV18(db, backend)
|
||||
case 19:
|
||||
return validateDatabaseSchemaV19(db, backend)
|
||||
default:
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
if !db.Migrator().HasTable("tunnels") {
|
||||
return fmt.Errorf("table tunnels is missing")
|
||||
if !db.Migrator().HasColumn(&ProxyRoute{}, "tunnel_node_id") {
|
||||
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") {
|
||||
return fmt.Errorf("column nodes.node_type is missing")
|
||||
@@ -1202,6 +1209,22 @@ func validateDatabaseSchemaV18(db *gorm.DB, backend string) error {
|
||||
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 {
|
||||
ctx := databaseSchemaMigrationContext{}
|
||||
migrations := []databaseSchemaMigration{}
|
||||
|
||||
Reference in New Issue
Block a user