Skip to content

Commit

Permalink
Make UUID field a pointer (#459)
Browse files Browse the repository at this point in the history
Fixes an issue where new objects were being created with a UUID of the
empty string instead of a null, which was interfering with the
uniqueness test.

Signed-off-by: Sam Lucidi <[email protected]>
  • Loading branch information
mansam authored Jul 25, 2023
1 parent d1683e4 commit 7766a0c
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 27 deletions.
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/konveyor/tackle2-hub/metrics"
"github.com/konveyor/tackle2-hub/migration"
"github.com/konveyor/tackle2-hub/reaper"
"github.com/konveyor/tackle2-hub/seeding"
"github.com/konveyor/tackle2-hub/seed"
"github.com/konveyor/tackle2-hub/settings"
"github.com/konveyor/tackle2-hub/task"
"github.com/konveyor/tackle2-hub/tracker"
Expand All @@ -42,7 +42,7 @@ func Setup() (db *gorm.DB, err error) {
if err != nil {
return
}
err = seeding.Seed()
err = seed.Seed()
if err != nil {
return
}
Expand Down
8 changes: 4 additions & 4 deletions migration/v7/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@ func (r Migration) Apply(db *gorm.DB) (err error) {
// and then mark the column unique and create the index via auto-migrate.
type TagCategory struct {
model.TagCategory
UUID string
UUID *string
}

type Tag struct {
model.Tag
UUID string
UUID *string
}

type JobFunction struct {
model.JobFunction
UUID string
UUID *string
}

type RuleSet struct {
model.RuleSet
UUID string
UUID *string
}

err = db.AutoMigrate(&TagCategory{}, &Tag{}, &JobFunction{}, &RuleSet{})
Expand Down
2 changes: 1 addition & 1 deletion migration/v7/model/jobfunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package model

type JobFunction struct {
Model
UUID string `gorm:"uniqueIndex"`
UUID *string `gorm:"uniqueIndex"`
Username string
Name string `gorm:"index;unique;not null"`
Stakeholders []Stakeholder `gorm:"constraint:OnDelete:SET NULL"`
Expand Down
2 changes: 1 addition & 1 deletion migration/v7/model/ruleset.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "gorm.io/gorm"
// RuleSet - Analysis ruleset.
type RuleSet struct {
Model
UUID string `gorm:"uniqueIndex"`
UUID *string `gorm:"uniqueIndex"`
Kind string
Name string `gorm:"uniqueIndex;not null"`
Description string
Expand Down
4 changes: 2 additions & 2 deletions migration/v7/model/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package model

type Tag struct {
Model
UUID string `gorm:"uniqueIndex"`
Name string `gorm:"uniqueIndex:tagA;not null"`
UUID *string `gorm:"uniqueIndex"`
Name string `gorm:"uniqueIndex:tagA;not null"`
Username string
CategoryID uint `gorm:"uniqueIndex:tagA;index;not null"`
Category TagCategory
Expand Down
4 changes: 2 additions & 2 deletions migration/v7/model/tagcategory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package model

type TagCategory struct {
Model
UUID string `gorm:"uniqueIndex"`
Name string `gorm:"index;unique;not null"`
UUID *string `gorm:"uniqueIndex"`
Name string `gorm:"index;unique;not null"`
Username string
Rank uint
Color string
Expand Down
7 changes: 4 additions & 3 deletions seeding/jobfunction.go → seed/jobfunction.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seeding
package seed

import (
"errors"
Expand Down Expand Up @@ -32,7 +32,8 @@ func (r *JobFunction) With(seed libseed.Seed) (err error) {
// Apply seeds the database with JobFunctions.
func (r *JobFunction) Apply(db *gorm.DB) (err error) {
log.Info("Applying JobFunctions", "count", len(r.jobFunctions))
for _, jf := range r.jobFunctions {
for i := range r.jobFunctions {
jf := r.jobFunctions[i]
jobFunction, found, fErr := r.find(db, "uuid = ?", jf.UUID)
if fErr != nil {
err = fErr
Expand Down Expand Up @@ -71,7 +72,7 @@ func (r *JobFunction) Apply(db *gorm.DB) (err error) {
}

jobFunction.Name = jf.Name
jobFunction.UUID = jf.UUID
jobFunction.UUID = &jf.UUID
result := db.Save(&jobFunction)
if result.Error != nil {
err = liberr.Wrap(result.Error)
Expand Down
2 changes: 1 addition & 1 deletion seeding/pkg.go → seed/pkg.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seeding
package seed

import (
"errors"
Expand Down
9 changes: 5 additions & 4 deletions seeding/ruleset.go → seed/ruleset.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seeding
package seed

import (
"encoding/json"
Expand Down Expand Up @@ -41,7 +41,8 @@ func (r *RuleSet) Apply(db *gorm.DB) (err error) {
ruleSetsByUUID := make(map[string]*model.RuleSet)
ids := []uint{}

for _, rs := range r.ruleSets {
for i := range r.ruleSets {
rs := r.ruleSets[i]
ruleSet, found, fErr := r.find(db, "uuid = ?", rs.UUID)
if fErr != nil {
err = fErr
Expand Down Expand Up @@ -86,7 +87,7 @@ func (r *RuleSet) Apply(db *gorm.DB) (err error) {
}
ruleSet.Name = rs.Name
ruleSet.Description = rs.Description
ruleSet.UUID = rs.UUID
ruleSet.UUID = &rs.UUID
ruleSet.ImageID = file.ID
result := db.Save(ruleSet)
if result.Error != nil {
Expand All @@ -98,7 +99,7 @@ func (r *RuleSet) Apply(db *gorm.DB) (err error) {
if err != nil {
return
}
ruleSetsByUUID[ruleSet.UUID] = ruleSet
ruleSetsByUUID[rs.UUID] = ruleSet
ids = append(ids, ruleSet.ID)
}

Expand Down
2 changes: 1 addition & 1 deletion seeding/seed.go → seed/seed.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seeding
package seed

import (
"encoding/json"
Expand Down
14 changes: 8 additions & 6 deletions seeding/tag.go → seed/tag.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package seeding
package seed

import (
"errors"
Expand Down Expand Up @@ -32,7 +32,8 @@ func (r *TagCategory) With(seed libseed.Seed) (err error) {
// Apply seeds the database with TagCategories and Tags.
func (r *TagCategory) Apply(db *gorm.DB) (err error) {
log.Info("Applying TagCategories", "count", len(r.categories))
for _, tc := range r.categories {
for i := range r.categories {
tc := r.categories[i]
category, found, fErr := r.find(db, "uuid = ?", tc.UUID)
if fErr != nil {
err = fErr
Expand Down Expand Up @@ -72,7 +73,7 @@ func (r *TagCategory) Apply(db *gorm.DB) (err error) {
}

category.Name = tc.Name
category.UUID = tc.UUID
category.UUID = &tc.UUID
category.Color = tc.Color
result := db.Save(&category)
if result.Error != nil {
Expand All @@ -91,9 +92,10 @@ func (r *TagCategory) Apply(db *gorm.DB) (err error) {
//
// Seed a TagCategory's tags.
func (r *TagCategory) applyTags(db *gorm.DB, category *model.TagCategory, tc libseed.TagCategory) (err error) {
for _, t := range tc.Tags {
for i := range tc.Tags {
t := tc.Tags[i]
tag := model.Tag{}
result := db.First(&tag, model.Tag{UUID: t.UUID})
result := db.First(&tag, model.Tag{UUID: &t.UUID})
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
err = liberr.Wrap(result.Error)
return
Expand All @@ -107,7 +109,7 @@ func (r *TagCategory) applyTags(db *gorm.DB, category *model.TagCategory, tc lib
}

tag.Name = t.Name
tag.UUID = t.UUID
tag.UUID = &t.UUID
tag.CategoryID = category.ID
result = db.Save(&tag)
if result.Error != nil {
Expand Down

0 comments on commit 7766a0c

Please sign in to comment.