From fdc449d660987023b04dc5f193f0b5f06b1c4a59 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Wed, 9 Nov 2022 18:27:12 -0500 Subject: [PATCH] change event type registration to upsert (#3) --- repository/repository_db.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/repository/repository_db.go b/repository/repository_db.go index 473dd2c..4b9b4c5 100644 --- a/repository/repository_db.go +++ b/repository/repository_db.go @@ -6,6 +6,7 @@ import ( "github.com/IceWhaleTech/CasaOS-MessageBus/model" "gorm.io/driver/sqlite" "gorm.io/gorm" + "gorm.io/gorm/clause" ) type DatabaseRepository struct { @@ -23,7 +24,8 @@ func (r *DatabaseRepository) GetEventTypes() ([]model.EventType, error) { } func (r *DatabaseRepository) RegisterEventType(eventType model.EventType) (*model.EventType, error) { - if err := r.db.Create(&eventType).Error; err != nil { + // upsert + if err := r.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&eventType).Error; err != nil { return nil, err }