[MM-15296] Migrate "Preference.Save" to Sync by default (#10866)
* response format changes * Generated mocks Fixed all references of Preferences.Save * Remove old code from store.go (incorrect merge) * Review change - Add validations on count and error * Review change - return from root level * Review change - return 0 as nil value for int * fix initialisation of err in preference_store
Этот коммит содержится в:
коммит произвёл
Jesús Espino
родитель
c05cf5b033
Коммит
fb6c1debf0
@@ -54,31 +54,25 @@ func (s SqlPreferenceStore) DeleteUnusedFeatures() {
|
||||
s.GetMaster().Exec(sql, queryParams)
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) Save(preferences *model.Preferences) store.StoreChannel {
|
||||
return store.Do(func(result *store.StoreResult) {
|
||||
// wrap in a transaction so that if one fails, everything fails
|
||||
transaction, err := s.GetMaster().Begin()
|
||||
if err != nil {
|
||||
result.Err = model.NewAppError("SqlPreferenceStore.Save", "store.sql_preference.save.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
} else {
|
||||
defer finalizeTransaction(transaction)
|
||||
for _, preference := range *preferences {
|
||||
if upsertResult := s.save(transaction, &preference); upsertResult.Err != nil {
|
||||
*result = upsertResult
|
||||
break
|
||||
}
|
||||
}
|
||||
func (s SqlPreferenceStore) Save(preferences *model.Preferences) (int, *model.AppError) {
|
||||
// wrap in a transaction so that if one fails, everything fails
|
||||
transaction, err := s.GetMaster().Begin()
|
||||
if err != nil {
|
||||
return 0, model.NewAppError("SqlPreferenceStore.Save", "store.sql_preference.save.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
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.Save", "store.sql_preference.save.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
} else {
|
||||
result.Data = len(*preferences)
|
||||
}
|
||||
}
|
||||
defer finalizeTransaction(transaction)
|
||||
for _, preference := range *preferences {
|
||||
if upsertResult := s.save(transaction, &preference); upsertResult.Err != nil {
|
||||
return 0, upsertResult.Err
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if err := transaction.Commit(); err != nil {
|
||||
// don't need to rollback here since the transaction is already closed
|
||||
return 0, model.NewAppError("SqlPreferenceStore.Save", "store.sql_preference.save.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
return len(*preferences), nil
|
||||
}
|
||||
|
||||
func (s SqlPreferenceStore) save(transaction *gorp.Transaction, preference *model.Preference) store.StoreResult {
|
||||
|
||||
@@ -9,6 +9,8 @@ import (
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/store"
|
||||
"github.com/mattermost/mattermost-server/store/storetest"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestPreferenceStore(t *testing.T) {
|
||||
@@ -50,7 +52,9 @@ func TestDeleteUnusedFeatures(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
store.Must(ss.Preference().Save(&features))
|
||||
count, err := ss.Preference().Save(&features)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 4, count)
|
||||
|
||||
ss.Preference().(*SqlPreferenceStore).DeleteUnusedFeatures()
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user