diff --git a/hscontrol/db/db.go b/hscontrol/db/db.go index 4b8b3808596..94265c18bdf 100644 --- a/hscontrol/db/db.go +++ b/hscontrol/db/db.go @@ -127,12 +127,16 @@ func NewHeadscaleDatabase( // changed ones, when machines where renamed to nodes, which is covered // further up. This whole initial integration is a mess and if AutoMigrate // is ran on a 0.22 to 0.23 update, it will wipe all the routes. - if !tx.Migrator().HasTable(&types.Route{}) { - err = tx.AutoMigrate(&types.Route{}) + if tx.Migrator().HasTable(&types.Route{}) && tx.Migrator().HasTable(&types.Node{}) { + err := tx.Exec("delete from routes where node_id not in (select id from nodes)").Error if err != nil { return err } } + err = tx.AutoMigrate(&types.Route{}) + if err != nil { + return err + } err = tx.AutoMigrate(&types.Node{}) if err != nil {