Skip to content

Commit 6cfc2ca

Browse files
committed
Do not throw exception on duplicate key constraint
1 parent 3e3e5bd commit 6cfc2ca

2 files changed

Lines changed: 4 additions & 7 deletions

File tree

api/pkg/di/container.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,9 @@ func (container *Container) DB() (db *gorm.DB) {
250250

251251
container.logger.Debug(fmt.Sprintf("creating %T", db))
252252

253-
config := &gorm.Config{}
253+
config := &gorm.Config{TranslateError: true}
254254
if isLocal() {
255-
config = &gorm.Config{Logger: container.GormLogger()}
255+
config.Logger = container.GormLogger()
256256
}
257257

258258
db, err := gorm.Open(postgres.Open(os.Getenv("DATABASE_URL")), config)

api/pkg/repositories/gorm_phone_repository.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ import (
55
"errors"
66
"fmt"
77

8-
"github.com/google/uuid"
9-
"gorm.io/gorm/clause"
10-
118
"github.com/NdoleStudio/httpsms/pkg/entities"
129
"github.com/NdoleStudio/httpsms/pkg/telemetry"
10+
"github.com/google/uuid"
1311
"github.com/palantir/stacktrace"
1412
"gorm.io/gorm"
1513
)
@@ -80,11 +78,10 @@ func (repository *gormPhoneRepository) Save(ctx context.Context, phone *entities
8078
defer span.End()
8179

8280
err := repository.db.
83-
Clauses(clause.OnConflict{DoNothing: true, OnConstraint: "idx__phones__user_id__phone_number"}).
8481
WithContext(ctx).
8582
Save(phone).
8683
Error
87-
if err != nil {
84+
if err != nil && !errors.Is(err, gorm.ErrDuplicatedKey) {
8885
msg := fmt.Sprintf("cannot save phone with ID [%s]", phone.ID)
8986
return repository.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
9087
}

0 commit comments

Comments
 (0)