Skip to content

Commit

Permalink
initial route constraints and tests
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed Dec 14, 2024
1 parent 58d089c commit 912a546
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
16 changes: 16 additions & 0 deletions hscontrol/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,22 @@ func NewHeadscaleDatabase(
},
Rollback: func(db *gorm.DB) error { return nil },
},
{
// The unique constraint of Name has been dropped
// in favour of a unique together of name and
// provider identity.
ID: "202412131220",
Migrate: func(tx *gorm.DB) error {
// Prevent duplicate routes of a single node to be created.
err = tx.Exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_route_prefix_node_id ON users (name);").Error
if err != nil {
return fmt.Errorf("creating route unique index: %w", err)
}

return nil
},
Rollback: func(db *gorm.DB) error { return nil },
},
},
)

Expand Down
18 changes: 18 additions & 0 deletions hscontrol/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,24 @@ func TestMigrations(t *testing.T) {
}
},
},
{
dbPath: "testdata/route-23-to-24-before-unique-index-fails.sqlite",
wantErr: "creating route unique index",
},
// {
// dbPath: "testdata/route-23-to-24-before-unique-index-passes.sqlite",
// wantFunc: func(t *testing.T, h *HSDatabase) {
// routes, err := Read(h.DB, func(rx *gorm.DB) (types.Routes, error) {
// return GetRoutes(rx)
// })
// require.NoError(t, err)

// for _, route := range routes {
// t.Logf("route: %v", route)
// }
// t.Fail()
// },
// },
}

for _, tt := range tests {
Expand Down
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions hscontrol/types/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ type Node struct {
ForcedTags []string `gorm:"serializer:json"`

// TODO(kradalby): This seems like irrelevant information?
// 2024-12-13: This is relevant, the authkey has tags for
// the node, which mean we cant do a cascade delete or set null
// this needs to be changed to prevent the authkey from being
// deleted.
AuthKeyID *uint64 `sql:"DEFAULT:NULL"`
AuthKey *PreAuthKey `gorm:"constraint:OnDelete:SET NULL;"`

Expand Down

0 comments on commit 912a546

Please sign in to comment.