Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ignore the check for duplicate entry for entities #199

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion models/v1alpha3/relationship/relationship_helper.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package relationship

import (
"errors"
"fmt"
"path/filepath"
"strings"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions models/v1beta1/category/category_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"sync"

Expand Down Expand Up @@ -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
Expand Down
11 changes: 10 additions & 1 deletion models/v1beta1/component/component_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package component

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions models/v1beta1/connection/connection_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"sync"

"github.com/gofrs/uuid"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions models/v1beta1/model/model_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/md5"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"path/filepath"
"sync"
Expand Down Expand Up @@ -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
Expand Down