From 394e3c485501601a286563f05a97abeb58f8c55c Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 1 Jun 2026 14:06:36 +0800 Subject: [PATCH] =?UTF-8?q?[=E4=BC=98=E5=8C=96]=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=81=E7=A7=BB=E9=80=BB=E8=BE=91?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=20v19=20=E7=89=88=E6=9C=AC=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=EF=BC=8C=E9=87=8D=E6=9E=84=E9=9A=A7=E9=81=93=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E8=A1=A8=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- openflare_server/controller/agent.go | 2 +- openflare_server/model/migrate/v16.go | 19 +++++++++++++------ openflare_server/model/migrate/v19.go | 12 +++++++++--- openflare_server/model/migrations.go | 27 +++++++++++++++++++++++++-- 4 files changed, 48 insertions(+), 12 deletions(-) diff --git a/openflare_server/controller/agent.go b/openflare_server/controller/agent.go index 00e71a6b..c1ef29eb 100644 --- a/openflare_server/controller/agent.go +++ b/openflare_server/controller/agent.go @@ -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 diff --git a/openflare_server/model/migrate/v16.go b/openflare_server/model/migrate/v16.go index 1e949619..9a087ee2 100644 --- a/openflare_server/model/migrate/v16.go +++ b/openflare_server/model/migrate/v16.go @@ -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 } diff --git a/openflare_server/model/migrate/v19.go b/openflare_server/model/migrate/v19.go index ea6d28fb..917b2860 100644 --- a/openflare_server/model/migrate/v19.go +++ b/openflare_server/model/migrate/v19.go @@ -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") } diff --git a/openflare_server/model/migrations.go b/openflare_server/model/migrations.go index 0be1b9a2..96352520 100644 --- a/openflare_server/model/migrations.go +++ b/openflare_server/model/migrations.go @@ -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{}