Skip to content

Commit

Permalink
Add 'Namespace' to Experiment data model (#3184)
Browse files Browse the repository at this point in the history
  • Loading branch information
chensun authored Mar 8, 2020
1 parent 2f1dbbe commit b2662f2
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
5 changes: 5 additions & 0 deletions backend/src/apiserver/client_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,11 @@ func initDBClient(initConnectionTimeout time.Duration) *storage.DB {
glog.Fatalf("Failed to initialize the databases.")
}

response = db.Model(&model.Experiment{}).RemoveIndex("Name")
if response.Error != nil {
glog.Fatalf("Failed to drop unique key on experiment name. Error: %s", response.Error)
}

response = db.Model(&model.ResourceReference{}).ModifyColumn("Payload", "longtext not null")
if response.Error != nil {
glog.Fatalf("Failed to update the resource reference payload type. Error: %s", response.Error)
Expand Down
4 changes: 3 additions & 1 deletion backend/src/apiserver/model/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package model

type Experiment struct {
UUID string `gorm:"column:UUID; not null; primary_key"`
Name string `gorm:"column:Name; not null; unique"`
Name string `gorm:"column:Name; not null; unique_index:idx_name_namespace"`
Description string `gorm:"column:Description; not null"`
CreatedAtInSec int64 `gorm:"column:CreatedAtInSec; not null"`
Namespace string `gorm:"column:Namespace; not null; unique_index:idx_name_namespace"`
}

func (e Experiment) GetValueOfPrimaryKey() string {
Expand All @@ -30,6 +31,7 @@ var experimentAPIToModelFieldMap = map[string]string{
"name": "Name",
"created_at": "CreatedAtInSec",
"description": "Description",
"namespace": "Namespace",
}

// APIToModelFieldMap returns a map from API names to field names for model
Expand Down
28 changes: 15 additions & 13 deletions backend/src/apiserver/storage/experiment_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,18 @@ func (s *ExperimentStore) GetExperiment(uuid string) (*model.Experiment, error)
func (s *ExperimentStore) scanRows(rows *sql.Rows) ([]*model.Experiment, error) {
var experiments []*model.Experiment
for rows.Next() {
var uuid, name, description string
var uuid, name, description, namespace string
var createdAtInSec int64
err := rows.Scan(&uuid, &name, &description, &createdAtInSec)
err := rows.Scan(&uuid, &name, &description, &createdAtInSec, &namespace)
if err != nil {
return experiments, nil
return experiments, err
}
experiments = append(experiments, &model.Experiment{
UUID: uuid,
Name: name,
Description: description,
CreatedAtInSec: createdAtInSec,
Namespace: namespace,
})
}
return experiments, nil
Expand All @@ -150,12 +151,13 @@ func (s *ExperimentStore) CreateExperiment(experiment *model.Experiment) (*model
newExperiment.UUID = id.String()
sql, args, err := sq.
Insert("experiments").
SetMap(
sq.Eq{
"UUID": newExperiment.UUID,
"CreatedAtInSec": newExperiment.CreatedAtInSec,
"Name": newExperiment.Name,
"Description": newExperiment.Description}).
SetMap(sq.Eq{
"UUID": newExperiment.UUID,
"CreatedAtInSec": newExperiment.CreatedAtInSec,
"Name": newExperiment.Name,
"Description": newExperiment.Description,
"Namespace": newExperiment.Namespace,
}).
ToSql()
if err != nil {
return nil, util.NewInternalServerError(err, "Failed to create query to insert experiment to experiment table: %v",
Expand All @@ -165,7 +167,7 @@ func (s *ExperimentStore) CreateExperiment(experiment *model.Experiment) (*model
if err != nil {
if s.db.IsDuplicateError(err) {
return nil, util.NewInvalidInputError(
"Failed to create a new experiment. The name %v already exist. Please specify a new name.", experiment.Name)
"Failed to create a new experiment. The name %v already exists. Please specify a new name.", experiment.Name)
}
return nil, util.NewInternalServerError(err, "Failed to add experiment to experiment table: %v",
err.Error())
Expand Down Expand Up @@ -209,9 +211,9 @@ func (s *ExperimentStore) DeleteExperiment(id string) error {
// factory function for experiment store
func NewExperimentStore(db *DB, time util.TimeInterface, uuid util.UUIDGeneratorInterface) *ExperimentStore {
return &ExperimentStore{
db: db,
time: time,
uuid: uuid,
db: db,
time: time,
uuid: uuid,
resourceReferenceStore: NewResourceReferenceStore(db),
defaultExperimentStore: NewDefaultExperimentStore(db),
}
Expand Down
42 changes: 41 additions & 1 deletion backend/src/apiserver/storage/experiment_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@ const (
)

func createExperiment(name string) *model.Experiment {
return &model.Experiment{Name: name, Description: fmt.Sprintf("My name is %s", name)}
return createExperimentInNamespace(name, "")
}

func createExperimentInNamespace(name string, namespace string) *model.Experiment {
return &model.Experiment{
Name: name,
Description: fmt.Sprintf("My name is %s", name),
Namespace: namespace,
}
}

func TestListExperiments_Pagination(t *testing.T) {
Expand Down Expand Up @@ -231,6 +239,38 @@ func TestCreateExperiment(t *testing.T) {
assert.Equal(t, experimentExpected, *experiment, "Got unexpected experiment.")
}

func TestCreateExperiment_DifferentNamespaces(t *testing.T) {
db := NewFakeDbOrFatal()
defer db.Close()
experimentStore := NewExperimentStore(db, util.NewFakeTimeForEpoch(), util.NewFakeUUIDGeneratorOrFatal(fakeID, nil))
experimentExpected := model.Experiment{
UUID: fakeID,
CreatedAtInSec: 1,
Name: "experiment1",
Description: "My name is experiment1",
Namespace: "namespace1",
}

experiment := createExperimentInNamespace("experiment1", "namespace1")
experiment, err := experimentStore.CreateExperiment(experiment)
assert.Nil(t, err)
assert.Equal(t, experimentExpected, *experiment, "Got unexpected experiment.")

experimentStore = NewExperimentStore(db, util.NewFakeTimeForEpoch(), util.NewFakeUUIDGeneratorOrFatal(fakeIDTwo, nil))
experiment = createExperimentInNamespace("experiment1", "namespace2")
experimentExpected = model.Experiment{
UUID: fakeIDTwo,
CreatedAtInSec: 1,
Name: "experiment1",
Description: "My name is experiment1",
Namespace: "namespace2",
}

experiment, err = experimentStore.CreateExperiment(experiment)
assert.Nil(t, err)
assert.Equal(t, experimentExpected, *experiment, "Got unexpected experiment.")
}

func TestCreateExperiment_DuplicatedKey(t *testing.T) {
db := NewFakeDbOrFatal()
defer db.Close()
Expand Down

0 comments on commit b2662f2

Please sign in to comment.