1
0
mirror of https://github.com/juanfont/headscale.git synced 2025-10-28 10:51:44 +01:00

make it harder to insert invalid routes

Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
Kristoffer Dalby 2025-01-23 12:01:06 +01:00
parent c1f42cdf4b
commit 27bb1201f4
No known key found for this signature in database
2 changed files with 22 additions and 1 deletions

View File

@ -521,6 +521,27 @@ func NewHeadscaleDatabase(
}, },
Rollback: func(db *gorm.DB) error { return nil }, Rollback: func(db *gorm.DB) error { return nil },
}, },
{
// Add a constraint to routes ensuring they cannot exist without a node.
ID: "202501221827",
Migrate: func(tx *gorm.DB) error {
// Remove any invalid routes associated with a node that does not exist.
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
}
return nil
},
Rollback: func(db *gorm.DB) error { return nil },
},
}, },
) )

View File

@ -13,7 +13,7 @@ import (
type Route struct { type Route struct {
gorm.Model gorm.Model
NodeID uint64 NodeID uint64 `gorm:"not null"`
Node *Node Node *Node
// TODO(kradalby): change this custom type to netip.Prefix // TODO(kradalby): change this custom type to netip.Prefix