fix constraints
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
parent
3780c9fd69
commit
7d9b430ec2
3 changed files with 30 additions and 11 deletions
|
@ -498,6 +498,25 @@ func NewHeadscaleDatabase(
|
|||
return err
|
||||
}
|
||||
|
||||
// Set up indexes and unique constraints outside of GORM, it does not support
|
||||
// conditional unique constraints.
|
||||
// This ensures the following:
|
||||
// - A user name and provider_identifier is unique
|
||||
// - A provider_identifier is unique
|
||||
// - A user name is unique if there is no provider_identifier is not set
|
||||
for _, idx := range []string{
|
||||
"DROP INDEX IF EXISTS `idx_provider_identifier`",
|
||||
"DROP INDEX IF EXISTS `idx_name_provider_identifier`",
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS `idx_provider_identifier` ON `users` (`provider_identifier`) WHERE provider_identifier IS NOT NULL;",
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS `idx_name_provider_identifier` ON `users` (`name`,`provider_identifier`);",
|
||||
"CREATE UNIQUE INDEX IF NOT EXISTS `idx_name_no_provider_identifier` ON `users` (`name`) WHERE provider_identifier IS NULL;",
|
||||
} {
|
||||
err = tx.Exec(idx).Error
|
||||
if err != nil {
|
||||
return fmt.Errorf("creating username index: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
Rollback: func(db *gorm.DB) error { return nil },
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue