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

models: use partial index for unique constraints #736

Merged
merged 1 commit into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 13 additions & 2 deletions internal/registry/data/destination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
)

var (
destinationDevelop = models.Destination{Name: "develop", Kind: "kubernetes", Endpoint: "dev.kubernetes.com", Kubernetes: models.DestinationKubernetes{CA: "notsosecret"}, NodeID: "one"}
destinationProduction = models.Destination{Name: "production", Kind: "kubernetes", Endpoint: "prod.kubernetes.com", Kubernetes: models.DestinationKubernetes{CA: "supersecret"}, NodeID: "two"}
destinationDevelop = models.Destination{Name: "develop", Kind: "kubernetes", Endpoint: "dev.kubernetes.com", Kubernetes: models.DestinationKubernetes{CA: "notsosecret"}, NodeID: "develop"}
destinationProduction = models.Destination{Name: "production", Kind: "kubernetes", Endpoint: "prod.kubernetes.com", Kubernetes: models.DestinationKubernetes{CA: "supersecret"}, NodeID: "production"}

labelUSWest1 = models.Label{Value: "us-west-1"}
labelUSEast1 = models.Label{Value: "us-east-1"}
Expand Down Expand Up @@ -297,3 +297,14 @@ func TestDeleteDestinations(t *testing.T) {
_, err = GetDestination(db, &models.Destination{Name: "production"})
require.NoError(t, err)
}

func TestRecreateDestinationSameNodeID(t *testing.T) {
db := setup(t)
createDestinations(t, db, destinationDevelop, destinationProduction)

err := DeleteDestinations(db, &models.Destination{NodeID: "develop"})
require.NoError(t, err)

_, err = CreateDestination(db, &models.Destination{NodeID: "develop"})
require.NoError(t, err)
}
11 changes: 11 additions & 0 deletions internal/registry/data/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,14 @@ func TestDeleteGroup(t *testing.T) {
_, err = GetGroup(db, &models.Group{Name: engineers.Name})
require.NoError(t, err)
}

func TestRecreateGroupSameName(t *testing.T) {
db := setup(t)
createGroups(t, db, everyone, engineers, product)

err := DeleteGroups(db, &models.Group{Name: everyone.Name})
require.NoError(t, err)

_, err = CreateGroup(db, &models.Group{Name: everyone.Name})
require.NoError(t, err)
}
13 changes: 13 additions & 0 deletions internal/registry/data/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,16 @@ func TestDeleteProviders(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 0, len(okta))
}

func TestRecreateProviderSameDomain(t *testing.T) {
db := setup(t)
createProviders(t, db, providerDevelop, providerProduction)

err := DeleteProviders(db, func(db *gorm.DB) *gorm.DB {
return db.Where(&models.Provider{Domain: "dev.okta.com"})
})
require.NoError(t, err)

_, err = CreateProvider(db, &models.Provider{Domain: "dev.okta.com"})
require.NoError(t, err)
}
11 changes: 11 additions & 0 deletions internal/registry/data/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,14 @@ func TestDeleteUser(t *testing.T) {
_, err = GetUser(db, &models.User{Email: bourne.Email})
require.NoError(t, err)
}

func TestRecreateUserSameEmail(t *testing.T) {
db := setup(t)
createUsers(t, db, bond, bourne, bauer)

err := DeleteUsers(db, &models.User{Email: bond.Email})
require.NoError(t, err)

_, err = CreateUser(db, &models.User{Email: bond.Email})
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion internal/registry/models/destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Destination struct {

Name string
Kind DestinationKind
NodeID string `gorm:"uniqueIndex" validate:"required"`
NodeID string `gorm:"uniqueIndex:,where:deleted_at is NULL" validate:"required"`
Endpoint string

Labels []Label
Expand Down
2 changes: 1 addition & 1 deletion internal/registry/models/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
type Group struct {
Model

Name string
Name string `gorm:"uniqueIndex:,where:deleted_at is NULL"`

Grants []Grant `gorm:"many2many:groups_grants"`
Providers []Provider `gorm:"many2many:groups_providers"`
Expand Down
2 changes: 1 addition & 1 deletion internal/registry/models/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Model struct {
ID uuid.UUID
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `gorm:"index"`
DeletedAt gorm.DeletedAt
}

// Set an ID if one does not already exist. Unfortunately, we can use `gorm:"default"`
Expand Down
2 changes: 1 addition & 1 deletion internal/registry/models/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Provider struct {

Kind ProviderKind

Domain string `gorm:"unique"`
Domain string `gorm:"uniqueIndex:,where:deleted_at is NULL"`
ClientID string
ClientSecret EncryptedAtRest

Expand Down
4 changes: 2 additions & 2 deletions internal/registry/models/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type Token struct {
APIToken APIToken
APITokenID uuid.UUID

Key string `gorm:"<-:create;uniqueIndex"`
Key string `gorm:"<-:create;uniqueIndex:,where:deleted_at is NULL"`
Secret string `gorm:"-"`
Checksum []byte

Expand All @@ -45,7 +45,7 @@ func (t *Token) SessionToken() string {
type APIToken struct {
Model

Name string `gorm:"unique"`
Name string `gorm:"uniqueIndex:,where:deleted_at is NULL"`
Permissions string
TTL time.Duration
}
Expand Down
2 changes: 1 addition & 1 deletion internal/registry/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type User struct {
Model

Name string
Email string `gorm:"uniqueIndex"`
Email string `gorm:"uniqueIndex:,where:deleted_at is NULL"`
Permissions string

Grants []Grant `gorm:"many2many:users_grants"`
Expand Down