From 6158adb5b4d9734f41f6ef97d693b2716144a936 Mon Sep 17 00:00:00 2001 From: farhadab <34206237+farhadab@users.noreply.github.com> Date: Fri, 24 May 2019 03:46:07 -0400 Subject: [PATCH] [MM-15305] Migrate Preference.CleanupFlagsBatch to Sync by default (#10858) * [MM-15305] Migrate Preference.CleanupFlagsBatch to Sync by default * clean up code in CleanupFlagsBatch --- store/sqlstore/preference_store.go | 75 ++++++++++++------------ store/store.go | 2 +- store/storetest/mocks/PreferenceStore.go | 19 ++++-- store/storetest/preference_store.go | 6 +- 4 files changed, 53 insertions(+), 49 deletions(-) diff --git a/store/sqlstore/preference_store.go b/store/sqlstore/preference_store.go index 93b6786ee2..ad42502529 100644 --- a/store/sqlstore/preference_store.go +++ b/store/sqlstore/preference_store.go @@ -287,45 +287,42 @@ func (s SqlPreferenceStore) DeleteCategoryAndName(category string, name string) }) } -func (s SqlPreferenceStore) CleanupFlagsBatch(limit int64) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - query := - `DELETE FROM - Preferences - WHERE - Category = :Category - AND Name IN ( +func (s SqlPreferenceStore) CleanupFlagsBatch(limit int64) (int64, *model.AppError) { + query := + `DELETE FROM + Preferences + WHERE + Category = :Category + AND Name IN ( + SELECT + * + FROM ( SELECT - * - FROM ( - SELECT - Preferences.Name - FROM - Preferences - LEFT JOIN - Posts - ON - Preferences.Name = Posts.Id - WHERE - Preferences.Category = :Category - AND Posts.Id IS null - LIMIT - :Limit - ) - AS t - )` + Preferences.Name + FROM + Preferences + LEFT JOIN + Posts + ON + Preferences.Name = Posts.Id + WHERE + Preferences.Category = :Category + AND Posts.Id IS null + LIMIT + :Limit + ) + AS t + )` - sqlResult, err := s.GetMaster().Exec(query, map[string]interface{}{"Category": model.PREFERENCE_CATEGORY_FLAGGED_POST, "Limit": limit}) - if err != nil { - result.Err = model.NewAppError("SqlPostStore.CleanupFlagsBatch", "store.sql_preference.cleanup_flags_batch.app_error", nil, ""+err.Error(), http.StatusInternalServerError) - } else { - rowsAffected, err1 := sqlResult.RowsAffected() - if err1 != nil { - result.Err = model.NewAppError("SqlPostStore.CleanupFlagsBatch", "store.sql_preference.cleanup_flags_batch.app_error", nil, ""+err.Error(), http.StatusInternalServerError) - result.Data = int64(0) - } else { - result.Data = rowsAffected - } - } - }) + sqlResult, err := s.GetMaster().Exec(query, map[string]interface{}{"Category": model.PREFERENCE_CATEGORY_FLAGGED_POST, "Limit": limit}) + if err != nil { + return int64(0), model.NewAppError("SqlPostStore.CleanupFlagsBatch", "store.sql_preference.cleanup_flags_batch.app_error", nil, ""+err.Error(), http.StatusInternalServerError) + } + + rowsAffected, err := sqlResult.RowsAffected() + if err != nil { + return int64(0), model.NewAppError("SqlPostStore.CleanupFlagsBatch", "store.sql_preference.cleanup_flags_batch.app_error", nil, ""+err.Error(), http.StatusInternalServerError) + } + + return rowsAffected, nil } diff --git a/store/store.go b/store/store.go index 5479ec4875..2a2fbab75f 100644 --- a/store/store.go +++ b/store/store.go @@ -437,7 +437,7 @@ type PreferenceStore interface { DeleteCategoryAndName(category string, name string) StoreChannel PermanentDeleteByUser(userId string) *model.AppError IsFeatureEnabled(feature, userId string) StoreChannel - CleanupFlagsBatch(limit int64) StoreChannel + CleanupFlagsBatch(limit int64) (int64, *model.AppError) } type LicenseStore interface { diff --git a/store/storetest/mocks/PreferenceStore.go b/store/storetest/mocks/PreferenceStore.go index 05f5e009f9..60a6e05666 100644 --- a/store/storetest/mocks/PreferenceStore.go +++ b/store/storetest/mocks/PreferenceStore.go @@ -14,19 +14,26 @@ type PreferenceStore struct { } // CleanupFlagsBatch provides a mock function with given fields: limit -func (_m *PreferenceStore) CleanupFlagsBatch(limit int64) store.StoreChannel { +func (_m *PreferenceStore) CleanupFlagsBatch(limit int64) (int64, *model.AppError) { ret := _m.Called(limit) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(int64) store.StoreChannel); ok { + var r0 int64 + if rf, ok := ret.Get(0).(func(int64) int64); ok { r0 = rf(limit) } else { - if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).(int64) + } + + var r1 *model.AppError + if rf, ok := ret.Get(1).(func(int64) *model.AppError); ok { + r1 = rf(limit) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) } } - return r0 + return r0, r1 } // Delete provides a mock function with given fields: userId, category, name diff --git a/store/storetest/preference_store.go b/store/storetest/preference_store.go index aebc93e75c..d1606fbd4d 100644 --- a/store/storetest/preference_store.go +++ b/store/storetest/preference_store.go @@ -436,10 +436,10 @@ func testPreferenceCleanupFlagsBatch(t *testing.T, ss store.Store) { store.Must(ss.Preference().Save(&model.Preferences{preference1, preference2})) - result := <-ss.Preference().CleanupFlagsBatch(10000) - assert.Nil(t, result.Err) + _, err := ss.Preference().CleanupFlagsBatch(10000) + assert.Nil(t, err) - _, err := ss.Preference().Get(userId, category, preference1.Name) + _, err = ss.Preference().Get(userId, category, preference1.Name) assert.Nil(t, err) _, err = ss.Preference().Get(userId, category, preference2.Name)