diff --git a/models/v1alpha3/relationship/relationship_helper.go b/models/v1alpha3/relationship/relationship_helper.go index e6f8ecac92..55b3a7051b 100644 --- a/models/v1alpha3/relationship/relationship_helper.go +++ b/models/v1alpha3/relationship/relationship_helper.go @@ -1,6 +1,7 @@ package relationship import ( + "errors" "fmt" "path/filepath" "strings" @@ -9,6 +10,7 @@ import ( "github.com/layer5io/meshkit/database" "github.com/layer5io/meshkit/models/meshmodel/entity" "github.com/layer5io/meshkit/utils" + "gorm.io/gorm" "gorm.io/gorm/clause" ) @@ -41,9 +43,13 @@ func (r *RelationshipDefinition) Create(db *database.Handler, hostID uuid.UUID) r.ModelId = mid err = db.Omit(clause.Associations).Create(&r).Error if err != nil { + if errors.Is(err, gorm.ErrDuplicatedKey) { + fmt.Printf("Duplicate entry detected for Relationship ID: %s\n", r.Id) + return r.Id, nil + } return uuid.UUID{}, err } - return r.Id, err + return r.Id, nil } func (r *RelationshipDefinition) UpdateStatus(db *database.Handler, status entity.EntityStatus) error { diff --git a/models/v1beta1/category/category_helper.go b/models/v1beta1/category/category_helper.go index 77f60ebb15..d9ca6dfdb5 100644 --- a/models/v1beta1/category/category_helper.go +++ b/models/v1beta1/category/category_helper.go @@ -5,6 +5,7 @@ import ( "crypto/md5" "encoding/hex" "encoding/json" + "errors" "fmt" "sync" @@ -68,6 +69,10 @@ func (cat *CategoryDefinition) Create(db *database.Handler, _ uuid.UUID) (uuid.U cat.Id = catID err = db.Create(&cat).Error if err != nil { + if errors.Is(err, gorm.ErrDuplicatedKey) { + fmt.Printf("Duplicate entry detected for Category ID: %s\n", catID) + return catID, nil + } return uuid.UUID{}, err } return cat.Id, nil diff --git a/models/v1beta1/component/component_helper.go b/models/v1beta1/component/component_helper.go index ce2fb4b061..cd460d8fea 100644 --- a/models/v1beta1/component/component_helper.go +++ b/models/v1beta1/component/component_helper.go @@ -3,6 +3,7 @@ package component import ( + "errors" "fmt" "os" "path/filepath" @@ -11,6 +12,7 @@ import ( "github.com/layer5io/meshkit/database" "github.com/layer5io/meshkit/models/meshmodel/entity" "github.com/layer5io/meshkit/utils" + "gorm.io/gorm" "gorm.io/gorm/clause" ) @@ -58,7 +60,14 @@ func (c *ComponentDefinition) Create(db *database.Handler, hostID uuid.UUID) (uu c.ModelId = mid err = db.Omit(clause.Associations).Create(&c).Error - return c.Id, err + if err != nil { + if errors.Is(err, gorm.ErrDuplicatedKey) { + fmt.Printf("Duplicate entry detected for Component ID: %s\n", c.Id) + return c.Id, nil + } + return uuid.UUID{}, err + } + return c.Id, nil } func (m *ComponentDefinition) UpdateStatus(db *database.Handler, status entity.EntityStatus) error { diff --git a/models/v1beta1/connection/connection_helper.go b/models/v1beta1/connection/connection_helper.go index 0f413c827b..a437752e0c 100644 --- a/models/v1beta1/connection/connection_helper.go +++ b/models/v1beta1/connection/connection_helper.go @@ -6,6 +6,8 @@ import ( "crypto/md5" "encoding/hex" "encoding/json" + "errors" + "fmt" "sync" "github.com/gofrs/uuid" @@ -44,6 +46,10 @@ func (h *Connection) Create(db *database.Handler) (uuid.UUID, error) { h.Id = hID err = db.Create(&h).Error if err != nil { + if errors.Is(err, gorm.ErrDuplicatedKey) { + fmt.Printf("Duplicate entry detected for Connection ID: %s\n", hID) + return hID, nil + } return uuid.UUID{}, err } return h.Id, nil diff --git a/models/v1beta1/model/model_helper.go b/models/v1beta1/model/model_helper.go index 1e1fc703e8..37d53a94e9 100644 --- a/models/v1beta1/model/model_helper.go +++ b/models/v1beta1/model/model_helper.go @@ -6,6 +6,7 @@ import ( "crypto/md5" "encoding/hex" "encoding/json" + "errors" "fmt" "path/filepath" "sync" @@ -81,6 +82,11 @@ func (m *ModelDefinition) Create(db *database.Handler, hostID uuid.UUID) (uuid.U m.RegistrantId = hostID err = db.Omit(clause.Associations).Create(&m).Error if err != nil { + // Use gorm.ErrDuplicatedKey for checking duplicate errors + if errors.Is(err, gorm.ErrDuplicatedKey) { + fmt.Printf("Duplicate entry detected for model ID: %s\n", modelID) + return modelID, nil + } return uuid.UUID{}, err } // register model inside registries table