Skip to content

Commit

Permalink
Incorporate review comment and few optimisation
Browse files Browse the repository at this point in the history
Signed-off-by: parauliya <[email protected]>
  • Loading branch information
parauliya committed Dec 4, 2020
1 parent 060c42b commit 0837b7f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 34 deletions.
2 changes: 1 addition & 1 deletion db/migration/202009171251-init-database.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CREATE INDEX IF NOT EXISTS idxgin_type ON hardware USING GIN (data JSONB_PATH_OP
CREATE TABLE IF NOT EXISTS template (
id UUID UNIQUE NOT NULL
, name VARCHAR(200) NOT NULL
, name VARCHAR(200) UNIQUE NOT NULL
, created_at TIMESTAMPTZ
, updated_at TIMESTAMPTZ
, deleted_at TIMESTAMPTZ
Expand Down
12 changes: 12 additions & 0 deletions db/migration/202012041103-remove-unique-constraints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package migration

import migrate "github.com/rubenv/sql-migrate"

func Get202012041103() *migrate.Migration {
return &migrate.Migration{
Id: "202010221010-remove-unique-index",
Up: []string{`
ALTER TABLE template DROP CONSTRAINT template_name_key;
`},
}
}
1 change: 1 addition & 0 deletions db/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ func GetMigrations() *migrate.MemoryMigrationSource {
Migrations: []*migrate.Migration{
Get202009171251(),
Get202010221010(),
Get202012041103(),
},
}
}
35 changes: 3 additions & 32 deletions db/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ func (d TinkDB) CreateTemplate(ctx context.Context, name string, data string, id
fields := map[string]string{
"name": name,
}
if d.getTemplateByName(ctx, fields) {
_, _, _, err := d.GetTemplate(ctx, fields)
if err != sql.ErrNoRows {
return errors.New("Template with name '" + name + "' already exist")
}
_, err := wflow.Parse([]byte(data))
_, err = wflow.Parse([]byte(data))
if err != nil {
return err
}
Expand Down Expand Up @@ -79,36 +80,6 @@ func (d TinkDB) GetTemplate(ctx context.Context, fields map[string]string) (stri
return "", "", "", err
}

// check for existing template
func (d TinkDB) getTemplateByName(ctx context.Context, fields map[string]string) bool {
getCondition, err := buildGetCondition(fields)
if err != nil {
return true
}

query := `
SELECT id, name, data
FROM template
WHERE
` + getCondition + `
deleted_at IS NULL
`
row := d.instance.QueryRowContext(ctx, query)
id := []byte{}
name := []byte{}
data := []byte{}
err = row.Scan(&id, &name, &data)
if err == nil {
return true
}
if err != sql.ErrNoRows {
err = errors.Wrap(err, "SELECT")
logger.Error(err)
return false
}
return false
}

// GetTemplateForWorkflow returns a template for workflow
func (d TinkDB) GetTemplateForWorkflow(ctx context.Context, fields map[string]string) (string, string, string, error) {
getCondition, err := buildGetCondition(fields)
Expand Down
2 changes: 1 addition & 1 deletion deploy/db/tinkerbell-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ CREATE INDEX IF NOT EXISTS idxgin_type ON hardware USING GIN (data JSONB_PATH_OP

CREATE TABLE IF NOT EXISTS template (
id UUID UNIQUE NOT NULL
, name VARCHAR(200) NOT NULL
, name VARCHAR(200) UNIQUE NOT NULL
, created_at TIMESTAMPTZ
, updated_at TIMESTAMPTZ
, deleted_at TIMESTAMPTZ
Expand Down

0 comments on commit 0837b7f

Please sign in to comment.