Skip to content

Commit

Permalink
fix: atomic first or create recore
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Oct 8, 2024
1 parent c006f8b commit 8e636fd
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
7 changes: 7 additions & 0 deletions internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/synctv-org/synctv/utils"
_ "github.com/synctv-org/synctv/utils/fastJSONSerializer"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

var (
Expand Down Expand Up @@ -366,3 +367,9 @@ func HandleUpdateResult(result *gorm.DB, entityName string) error {
}
return nil
}

func OnConflictDoNothing() *gorm.DB {
return db.Clauses(clause.OnConflict{
DoNothing: true,
})
}
2 changes: 1 addition & 1 deletion internal/db/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func FirstOrCreateRoomMemberRelation(roomID, userID string, conf ...CreateRoomMe
for _, c := range conf {
c(d)
}
err := db.Where("room_id = ? AND user_id = ?", roomID, userID).Attrs(d).FirstOrCreate(roomMemberRelation).Error
err := OnConflictDoNothing().Where("room_id = ? AND user_id = ?", roomID, userID).Attrs(d).FirstOrCreate(roomMemberRelation).Error
return roomMemberRelation, err
}

Expand Down
2 changes: 1 addition & 1 deletion internal/db/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func GetRoomByID(id string) (*model.Room, error) {

func CreateOrLoadRoomSettings(roomID string) (*model.RoomSettings, error) {
var rs model.RoomSettings
err := db.Where(model.RoomSettings{ID: roomID}).Attrs(model.DefaultRoomSettings()).FirstOrCreate(&rs).Error
err := OnConflictDoNothing().Where(model.RoomSettings{ID: roomID}).Attrs(model.DefaultRoomSettings()).FirstOrCreate(&rs).Error
return &rs, HandleNotFound(err, "room")
}

Expand Down
2 changes: 1 addition & 1 deletion internal/db/setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func GetSettingItemValue(name string) (string, error) {
}

func FirstOrCreateSettingItemValue(s *model.Setting) error {
return db.Where("name = ?", s.Name).FirstOrCreate(s, model.Setting{
return OnConflictDoNothing().Where("name = ?", s.Name).FirstOrCreate(s, model.Setting{
Value: s.Value,
Type: s.Type,
Group: s.Group,
Expand Down
2 changes: 1 addition & 1 deletion internal/db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func CreateOrLoadUserWithProvider(username, password string, p provider.OAuth2Pr
c(user)
}
user.EnableAutoAddUsernameSuffix()
err = db.Joins("JOIN user_providers ON users.id = user_providers.user_id").
err = OnConflictDoNothing().Joins("JOIN user_providers ON users.id = user_providers.user_id").
Where("user_providers.provider = ? AND user_providers.provider_user_id = ?", p, puid).
FirstOrCreate(user).Error
if err != nil {
Expand Down

0 comments on commit 8e636fd

Please sign in to comment.