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

CSS-9588 Enforce stricter model name uniqueness #1272

Merged
merged 3 commits into from
Jul 17, 2024
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
4 changes: 2 additions & 2 deletions internal/dbmodel/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ type Model struct {
UpdatedAt time.Time

// Name is the name of the model.
Name string
Name string `gorm:"uniqueIndex:unique_model_names;not null"`

// UUID is the UUID of the model.
UUID sql.NullString

// Owner is identity that owns the model.
OwnerIdentityName string
OwnerIdentityName string `gorm:"uniqueIndex:unique_model_names;not null"`
Owner Identity `gorm:"foreignkey:OwnerIdentityName;references:Name"`

// Controller is the controller that is hosting the model.
Expand Down
4 changes: 2 additions & 2 deletions internal/dbmodel/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestRecreateDeletedModel(t *testing.T) {
CloudRegion: cl.Regions[0],
CloudCredential: cred,
}
c.Check(db.Create(&m2).Error, qt.ErrorMatches, `.*violates unique constraint "models_controller_id_owner_identity_name_name_key".*`)
c.Check(db.Create(&m2).Error, qt.ErrorMatches, `.*violates unique constraint "unique_model_names".*`)
babakks marked this conversation as resolved.
Show resolved Hide resolved

c.Assert(db.Delete(&m1).Error, qt.IsNil)
c.Check(db.First(&m1).Error, qt.Equals, gorm.ErrRecordNotFound)
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestModelUniqueConstraint(t *testing.T) {
Level: "unsupported",
},
}
c.Assert(db.Create(&m2).Error, qt.IsNil)
c.Assert(db.Create(&m2).Error, qt.ErrorMatches, `ERROR: duplicate key value violates unique constraint .*`)

m3 := dbmodel.Model{
UUID: sql.NullString{
Expand Down
6 changes: 6 additions & 0 deletions internal/dbmodel/sql/postgres/1_10.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- 1_10.sql is a migration that enforces a stricter uniqueness
-- constraint on model names.
ALTER TABLE models DROP CONSTRAINT models_controller_id_owner_identity_name_name_key;
ALTER TABLE models ADD CONSTRAINT unique_model_names UNIQUE(owner_identity_name, name);

UPDATE versions SET major=1, minor=10 WHERE component='jimmdb';
kian99 marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 1 addition & 1 deletion internal/dbmodel/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
// Minor is the minor version of the model described in the dbmodel
// package. It should be incremented for any change made to the
// database model from database model in a released JIMM.
Minor = 9
Minor = 10
)

type Version struct {
Expand Down
18 changes: 18 additions & 0 deletions internal/jujuapi/modelmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,24 @@ func (s *modelManagerSuite) TestCreateModel(c *gc.C) {
}
}

func (s *modelManagerSuite) TestCreateDuplicateModelsFails(c *gc.C) {
conn := s.open(c, nil, "bob")
defer conn.Close()
createModel := func(mi jujuparams.ModelInfo) error {
return conn.APICall("ModelManager", 9, "", "CreateModel", jujuparams.ModelCreateArgs{
Name: "my-model",
OwnerTag: names.NewUserTag("bob@canonical.com").String(),
CloudTag: names.NewCloudTag(jimmtest.TestCloudName).String(),
CloudCredentialTag: "cloudcred-" + jimmtest.TestCloudName + "_bob@canonical.com_cred",
}, &mi)
}
var mi jujuparams.ModelInfo
err := createModel(mi)
c.Assert(err, gc.IsNil)
err = createModel(mi)
c.Assert(err, gc.ErrorMatches, `model bob@canonical\.com/my-model already exists \(already exists\)`)
}

func (s *modelManagerSuite) TestGrantAndRevokeModel(c *gc.C) {
conn := s.open(c, nil, "bob")
defer conn.Close()
Expand Down
Loading