Skip to content

Commit

Permalink
enforce stricter model name uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
kian99 committed Jul 16, 2024
1 parent 4bfd2a9 commit 03107b0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
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".*`)

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';
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("[email protected]").String(),
CloudTag: names.NewCloudTag(jimmtest.TestCloudName).String(),
CloudCredentialTag: "cloudcred-" + jimmtest.TestCloudName + "[email protected]_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

0 comments on commit 03107b0

Please sign in to comment.