Moved saving multiple user preferences into a database transaction
Этот коммит содержится в:
@@ -38,25 +38,98 @@ func (s SqlPreferenceStore) CreateIndexesIfNotExists() {
|
||||
func (s SqlPreferenceStore) Save(preference *model.Preference) StoreChannel {
|
||||
storeChannel := make(StoreChannel)
|
||||
|
||||
go func() {
|
||||
storeChannel <- s.save(s.GetMaster(), preference)
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) save(queryable Queryable, preference *model.Preference) StoreResult {
|
||||
result := StoreResult{}
|
||||
|
||||
if result.Err = preference.IsValid(); result.Err != nil {
|
||||
return result
|
||||
}
|
||||
|
||||
if err := queryable.Insert(preference); err != nil {
|
||||
if IsUniqueConstraintError(err.Error(), "UserId", "preferences_pkey") {
|
||||
result.Err = model.NewAppError("SqlPreferenceStore.Save", "A preference with that user id, category, name, and alt id already exists",
|
||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error())
|
||||
} else {
|
||||
result.Err = model.NewAppError("SqlPreferenceStore.Save", "We couldn't save the preference",
|
||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error())
|
||||
}
|
||||
} else {
|
||||
result.Data = preference
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) Update(preference *model.Preference) StoreChannel {
|
||||
storeChannel := make(StoreChannel)
|
||||
|
||||
go func() {
|
||||
storeChannel <- s.update(s.GetMaster(), preference)
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) update(queryable Queryable, preference *model.Preference) StoreResult {
|
||||
result := StoreResult{}
|
||||
|
||||
if result.Err = preference.IsValid(); result.Err != nil {
|
||||
return result
|
||||
}
|
||||
|
||||
if count, err := queryable.Update(preference); err != nil {
|
||||
result.Err = model.NewAppError("SqlPreferenceStore.Update", "We couldn't update the preference",
|
||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error())
|
||||
} else {
|
||||
result.Data = count
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) SaveOrUpdate(preferences ...*model.Preference) StoreChannel {
|
||||
storeChannel := make(StoreChannel)
|
||||
|
||||
go func() {
|
||||
result := StoreResult{}
|
||||
|
||||
if result.Err = preference.IsValid(); result.Err != nil {
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
return
|
||||
}
|
||||
db := s.GetReplica()
|
||||
|
||||
if err := s.GetMaster().Insert(preference); err != nil {
|
||||
if IsUniqueConstraintError(err.Error(), "UserId", "preferences_pkey") {
|
||||
result.Err = model.NewAppError("SqlPreferenceStore.Save", "A preference with that user id, category, name, and alt id already exists",
|
||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error())
|
||||
if len(preferences) > 1 {
|
||||
// wrap in a transaction so that if one fails, everything fails
|
||||
transaction, err := db.Begin()
|
||||
if err != nil {
|
||||
result.Err = model.NewAppError("SqlPreferenceStore.SaveOrUpdateMultiple", "Unable to open transaction to update preferences", err.Error())
|
||||
} else {
|
||||
result.Err = model.NewAppError("SqlPreferenceStore.Save", "We couldn't save the preference",
|
||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error())
|
||||
for _, preference := range preferences {
|
||||
if err := s.saveOrUpdate(transaction, preference); err != nil {
|
||||
result.Err = err
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if result.Err == nil {
|
||||
if err := transaction.Commit(); err != nil {
|
||||
// don't need to rollback here since the transaction is already closed
|
||||
result.Err = model.NewAppError("SqlPreferenceStore.SaveOrUpdateMultiple", "Unable to commit transaction to update preferences", err.Error())
|
||||
}
|
||||
} else {
|
||||
if err := transaction.Rollback(); err != nil {
|
||||
result.Err = model.NewAppError("SqlPreferenceStore.SaveOrUpdateMultiple", "Unable to rollback transaction to update preferences", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result.Data = preference
|
||||
result.Err = s.saveOrUpdate(db, preferences[0])
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
@@ -66,33 +139,14 @@ func (s SqlPreferenceStore) Save(preference *model.Preference) StoreChannel {
|
||||
return storeChannel
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) Update(preference *model.Preference) StoreChannel {
|
||||
storeChannel := make(StoreChannel)
|
||||
|
||||
go func() {
|
||||
result := StoreResult{}
|
||||
|
||||
if result.Err = preference.IsValid(); result.Err != nil {
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
return
|
||||
func (s SqlPreferenceStore) saveOrUpdate(queryable Queryable, preference *model.Preference) *model.AppError {
|
||||
if result := s.save(queryable, preference); result.Err != nil {
|
||||
if result := s.update(queryable, preference); result.Err != nil {
|
||||
return result.Err
|
||||
}
|
||||
}
|
||||
|
||||
if count, err := s.GetMaster().Update(preference); err != nil {
|
||||
result.Err = model.NewAppError("SqlPreferenceStore.Update", "We couldn't update the preference",
|
||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId+", "+err.Error())
|
||||
} else if count != 1 {
|
||||
result.Err = model.NewAppError("SqlPreferenceStore.Update", "We couldn't update the preference",
|
||||
"user_id="+preference.UserId+", category="+preference.Category+", name="+preference.Name+", alt_id="+preference.AltId)
|
||||
} else {
|
||||
result.Data = preference
|
||||
}
|
||||
|
||||
storeChannel <- result
|
||||
close(storeChannel)
|
||||
}()
|
||||
|
||||
return storeChannel
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) GetByName(userId string, category string, name string) StoreChannel {
|
||||
|
||||
Ссылка в новой задаче
Block a user