Skip to content

Commit

Permalink
fix #1482, restore foregin keys, add constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
vsychov committed Sep 26, 2023
1 parent 01b85e5 commit 074d2fa
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ State management has been improved [#1492](https://github.com/juanfont/headscale
Use error group handling to ensure tests actually pass [#1535](https://github.com/juanfont/headscale/pull/1535) based on [#1460](https://github.com/juanfont/headscale/pull/1460)
Fix hang on SIGTERM [#1492](https://github.com/juanfont/headscale/pull/1492) taken from [#1480](https://github.com/juanfont/headscale/pull/1480)
Send logs to stderr by default [#1524](https://github.com/juanfont/headscale/pull/1524)

Restore foreign keys and add constraints [#1562](https://github.com/juanfont/headscale/pull/1562)
## 0.22.3 (2023-05-12)

### Changes
Expand Down
7 changes: 3 additions & 4 deletions hscontrol/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ func NewHeadscaleDatabase(
// node was registered.
_ = dbConn.Migrator().RenameColumn(&types.Node{}, "nickname", "given_name")

dbConn.Model(&types.Node{}).Where("auth_key_id = ?", 0).Update("auth_key_id", nil)
// If the MacNodehine table has a column for registered,
// find all occourences of "false" and drop them. Then
// remove the column.
Expand Down Expand Up @@ -273,8 +274,7 @@ func openDB(dbType, connectionAddr string, debug bool) (*gorm.DB, error) {
db, err := gorm.Open(
sqlite.Open(connectionAddr+"?_synchronous=1&_journal_mode=WAL"),
&gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
Logger: dbLogger,
Logger: dbLogger,
},
)

Expand All @@ -292,8 +292,7 @@ func openDB(dbType, connectionAddr string, debug bool) (*gorm.DB, error) {

case Postgres:
return gorm.Open(postgres.Open(connectionAddr), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
Logger: dbLogger,
Logger: dbLogger,
})
}

Expand Down
2 changes: 1 addition & 1 deletion hscontrol/db/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ func (hsdb *HSDatabase) deleteNode(node *types.Node) error {
}

// Unscoped causes the node to be fully removed from the database.
if err := hsdb.db.Unscoped().Delete(&node).Error; err != nil {
if err := hsdb.db.Unscoped().Delete(&types.Node{}, node.ID).Error; err != nil {
return err
}

Expand Down
6 changes: 3 additions & 3 deletions hscontrol/types/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ type Node struct {
// parts of headscale.
GivenName string `gorm:"type:varchar(63);unique_index"`
UserID uint
User User `gorm:"foreignKey:UserID"`
User User `gorm:"constraint:OnDelete:CASCADE;"`

RegisterMethod string

ForcedTags StringList

// TODO(kradalby): This seems like irrelevant information?
AuthKeyID uint
AuthKey *PreAuthKey
AuthKey *PreAuthKey `gorm:"constraint:OnDelete:SET NULL;"`

LastSeen *time.Time
Expiry *time.Time

HostInfo HostInfo
Endpoints StringList

Routes []Route
Routes []Route `gorm:"constraint:OnDelete:CASCADE;"`

CreatedAt time.Time
UpdatedAt time.Time
Expand Down
8 changes: 4 additions & 4 deletions hscontrol/types/preauth_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ type PreAuthKey struct {
ID uint64 `gorm:"primary_key"`
Key string
UserID uint
User User
User User `gorm:"constraint:OnDelete:CASCADE;"`
Reusable bool
Ephemeral bool `gorm:"default:false"`
Used bool `gorm:"default:false"`
ACLTags []PreAuthKeyACLTag
Ephemeral bool `gorm:"default:false"`
Used bool `gorm:"default:false"`
ACLTags []PreAuthKeyACLTag `gorm:"constraint:OnDelete:CASCADE;"`

CreatedAt *time.Time
Expiration *time.Time
Expand Down

0 comments on commit 074d2fa

Please sign in to comment.