Skip to content

Commit

Permalink
Generate uuid in go rather than in db (flyteorg#58)
Browse files Browse the repository at this point in the history
Signed-off-by: Yee Hing Tong <[email protected]>
  • Loading branch information
wild-endeavor authored Nov 5, 2021
1 parent a47c4b5 commit 3f61f67
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 20 deletions.
1 change: 1 addition & 0 deletions datacalog/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/golang/protobuf v1.4.3
github.com/jackc/pgconn v1.8.1
github.com/mitchellh/mapstructure v1.4.1
github.com/satori/go.uuid v1.2.0
github.com/spf13/cobra v1.1.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
Expand Down
15 changes: 9 additions & 6 deletions datacalog/pkg/repositories/gormimpl/dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func getTestDataset() models.Dataset {
Domain: "testDomain",
Name: "testName",
Version: "testVersion",
UUID: "test-uuid",
},
SerializedMetadata: []byte{1, 2, 3},
PartitionKeys: []models.PartitionKey{
Expand Down Expand Up @@ -87,13 +88,14 @@ func TestCreateDatasetNoPartitions(t *testing.T) {

// Only match on queries that append expected filters
GlobalMock.NewMock().WithQuery(
`INSERT INTO "datasets" ("created_at","updated_at","deleted_at","project","name","domain","version","serialized_metadata") VALUES ($1,$2,$3,$4,$5,$6,$7,$8)`).WithCallback(
`INSERT INTO "datasets" ("created_at","updated_at","deleted_at","project","name","domain","version","uuid","serialized_metadata") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)`).WithCallback(
func(s string, values []driver.NamedValue) {
assert.EqualValues(t, dataset.Project, values[3].Value)
assert.EqualValues(t, dataset.Name, values[4].Value)
assert.EqualValues(t, dataset.Domain, values[5].Value)
assert.EqualValues(t, dataset.Version, values[6].Value)
assert.EqualValues(t, dataset.SerializedMetadata, values[7].Value)
assert.EqualValues(t, dataset.UUID, values[7].Value)
assert.EqualValues(t, dataset.SerializedMetadata, values[8].Value)
datasetCreated = true
},
)
Expand All @@ -115,13 +117,14 @@ func TestCreateDataset(t *testing.T) {

// Only match on queries that append expected filters
GlobalMock.NewMock().WithQuery(
`INSERT INTO "datasets" ("created_at","updated_at","deleted_at","project","name","domain","version","serialized_metadata") VALUES ($1,$2,$3,$4,$5,$6,$7,$8)`).WithCallback(
`INSERT INTO "datasets" ("created_at","updated_at","deleted_at","project","name","domain","version","uuid","serialized_metadata") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)`).WithCallback(
func(s string, values []driver.NamedValue) {
assert.EqualValues(t, dataset.Project, values[3].Value)
assert.EqualValues(t, dataset.Name, values[4].Value)
assert.EqualValues(t, dataset.Domain, values[5].Value)
assert.EqualValues(t, dataset.Version, values[6].Value)
assert.EqualValues(t, dataset.SerializedMetadata, values[7].Value)
assert.EqualValues(t, dataset.UUID, values[7].Value)
assert.EqualValues(t, dataset.SerializedMetadata, values[8].Value)
datasetCreated = true
},
).WithReply([]map[string]interface{}{{"dataset_uuid": getDatasetUUID()}})
Expand Down Expand Up @@ -159,7 +162,7 @@ func TestGetDataset(t *testing.T) {
GlobalMock.Logging = true

// Only match on queries that append expected filters
GlobalMock.NewMock().WithQuery(`SELECT * FROM "datasets" WHERE "datasets"."project" = $1 AND "datasets"."name" = $2 AND "datasets"."domain" = $3 AND "datasets"."version" = $4 ORDER BY "datasets"."created_at" LIMIT 1`).WithReply(expectedDatasetResponse)
GlobalMock.NewMock().WithQuery(`SELECT * FROM "datasets" WHERE "datasets"."project" = $1 AND "datasets"."name" = $2 AND "datasets"."domain" = $3 AND "datasets"."version" = $4 AND "datasets"."uuid" = $5 ORDER BY "datasets"."created_at" LIMIT 1`).WithReply(expectedDatasetResponse)

expectedPartitionKeyResponse := make([]map[string]interface{}, 0)
samplePartitionKey := make(map[string]interface{})
Expand Down Expand Up @@ -237,7 +240,7 @@ func TestCreateDatasetAlreadyExists(t *testing.T) {

// Only match on queries that append expected filters
GlobalMock.NewMock().WithQuery(
`INSERT INTO "datasets" ("created_at","updated_at","deleted_at","project","name","domain","version","serialized_metadata") VALUES ($1,$2,$3,$4,$5,$6,$7,$8)`).WithError(
`INSERT INTO "datasets" ("created_at","updated_at","deleted_at","project","name","domain","version","uuid","serialized_metadata") VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)`).WithError(
getAlreadyExistsErr(),
)

Expand Down
4 changes: 0 additions & 4 deletions datacalog/pkg/repositories/handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@ func (h *DBHandle) CreateDB(dbName string) error {
}

func (h *DBHandle) Migrate(ctx context.Context) error {
if h.db.Config.Dialector.Name() == config.Postgres {
logger.Infof(ctx, "Creating postgres extension uuid-ossp if it does not exist")
h.db.Exec("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"")
}
if err := h.db.AutoMigrate(&models.Dataset{}); err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions datacalog/pkg/repositories/mocks/artifact.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions datacalog/pkg/repositories/mocks/partition.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions datacalog/pkg/repositories/mocks/tag.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion datacalog/pkg/repositories/models/dataset.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package models

import (
uuid "github.com/satori/go.uuid"
"gorm.io/gorm"
)

type DatasetKey struct {
Project string `gorm:"primary_key;"` // part of pkey, no index needed as it is first column in the pkey
Name string `gorm:"primary_key;index:dataset_name_idx"` // part of pkey and has separate index for filtering
Domain string `gorm:"primary_key;index:dataset_domain_idx"` // part of pkey and has separate index for filtering
Version string `gorm:"primary_key;index:dataset_version_idx"` // part of pkey and has separate index for filtering
UUID string `gorm:"type:uuid;unique;default:uuid_generate_v4()"`
UUID string `gorm:"type:uuid;unique;"`
}

type Dataset struct {
Expand All @@ -20,3 +25,12 @@ type PartitionKey struct {
DatasetUUID string `gorm:"type:uuid;primary_key"`
Name string `gorm:"primary_key"`
}

// BeforeCreate so that we set the UUID in golang rather than from a DB function call
func (dataset *Dataset) BeforeCreate(tx *gorm.DB) error {
if dataset.UUID == "" {
generated := uuid.NewV4()
tx.Model(dataset).Update("UUID", generated)
}
return nil
}

0 comments on commit 3f61f67

Please sign in to comment.