Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore foreign keys and add constraints #1562

Merged
merged 5 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading