From d1a594e504bc8d847b036a8cae1fd02d1739c748 Mon Sep 17 00:00:00 2001 From: Taufiq Rahman Date: Wed, 3 Jul 2019 01:33:27 +0600 Subject: [PATCH] Migrate Channel.PermanentDeleteByTeam to Sync by default #11175 (#11473) --- store/sqlstore/channel_store.go | 61 ++++++++++++--------------- store/store.go | 2 +- store/storetest/channel_store.go | 4 +- store/storetest/mocks/ChannelStore.go | 8 ++-- 4 files changed, 34 insertions(+), 41 deletions(-) diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index f9a21433c3..48d638ca9c 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -825,49 +825,42 @@ func (s SqlChannelStore) setDeleteAtT(transaction *gorp.Transaction, channelId s } // PermanentDeleteByTeam removes all channels for the given team from the database. -func (s SqlChannelStore) PermanentDeleteByTeam(teamId string) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - transaction, err := s.GetMaster().Begin() - if err != nil { - result.Err = model.NewAppError("SqlChannelStore.PermanentDeleteByTeam", "store.sql_channel.permanent_delete_by_team.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) - return - } - defer finalizeTransaction(transaction) +func (s SqlChannelStore) PermanentDeleteByTeam(teamId string) *model.AppError { + transaction, err := s.GetMaster().Begin() + if err != nil { + return model.NewAppError("SqlChannelStore.PermanentDeleteByTeam", "store.sql_channel.permanent_delete_by_team.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) + } + defer finalizeTransaction(transaction) - *result = s.permanentDeleteByTeamtT(transaction, teamId) - if result.Err != nil { - return - } + if err := s.permanentDeleteByTeamtT(transaction, teamId); err != nil { + return err + } - // Additionally propagate the deletions to the PublicChannels table. - if _, err := transaction.Exec(` + // Additionally propagate the deletions to the PublicChannels table. + if _, err := transaction.Exec(` DELETE FROM PublicChannels WHERE TeamId = :TeamId `, map[string]interface{}{ - "TeamId": teamId, - }); err != nil { - result.Err = model.NewAppError("SqlChannelStore.PermanentDeleteByTeamt", "store.sql_channel.permanent_delete_by_team.delete_public_channels.app_error", nil, "team_id="+teamId+", "+err.Error(), http.StatusInternalServerError) - return - } - - if err := transaction.Commit(); err != nil { - result.Err = model.NewAppError("SqlChannelStore.PermanentDeleteByTeam", "store.sql_channel.permanent_delete_by_team.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) - return - } - }) -} - -func (s SqlChannelStore) permanentDeleteByTeamtT(transaction *gorp.Transaction, teamId string) store.StoreResult { - result := store.StoreResult{} - - if _, err := transaction.Exec("DELETE FROM Channels WHERE TeamId = :TeamId", map[string]interface{}{"TeamId": teamId}); err != nil { - result.Err = model.NewAppError("SqlChannelStore.PermanentDeleteByTeam", "store.sql_channel.permanent_delete_by_team.app_error", nil, "teamId="+teamId+", "+err.Error(), http.StatusInternalServerError) - return result + "TeamId": teamId, + }); err != nil { + return model.NewAppError("SqlChannelStore.PermanentDeleteByTeamt", "store.sql_channel.permanent_delete_by_team.delete_public_channels.app_error", nil, "team_id="+teamId+", "+err.Error(), http.StatusInternalServerError) } - return result + if err := transaction.Commit(); err != nil { + return model.NewAppError("SqlChannelStore.PermanentDeleteByTeam", "store.sql_channel.permanent_delete_by_team.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) + } + + return nil +} + +func (s SqlChannelStore) permanentDeleteByTeamtT(transaction *gorp.Transaction, teamId string) *model.AppError { + if _, err := transaction.Exec("DELETE FROM Channels WHERE TeamId = :TeamId", map[string]interface{}{"TeamId": teamId}); err != nil { + return model.NewAppError("SqlChannelStore.PermanentDeleteByTeam", "store.sql_channel.permanent_delete_by_team.app_error", nil, "teamId="+teamId+", "+err.Error(), http.StatusInternalServerError) + } + + return nil } // PermanentDelete removes the given channel from the database. diff --git a/store/store.go b/store/store.go index d90e380946..eedfbfc9e9 100644 --- a/store/store.go +++ b/store/store.go @@ -140,7 +140,7 @@ type ChannelStore interface { Delete(channelId string, time int64) *model.AppError Restore(channelId string, time int64) *model.AppError SetDeleteAt(channelId string, deleteAt int64, updateAt int64) *model.AppError - PermanentDeleteByTeam(teamId string) StoreChannel + PermanentDeleteByTeam(teamId string) *model.AppError PermanentDelete(channelId string) StoreChannel GetByName(team_id string, name string, allowFromCache bool) (*model.Channel, *model.AppError) GetByNames(team_id string, names []string, allowFromCache bool) ([]*model.Channel, *model.AppError) diff --git a/store/storetest/channel_store.go b/store/storetest/channel_store.go index 062216dbe5..e9a79f7021 100644 --- a/store/storetest/channel_store.go +++ b/store/storetest/channel_store.go @@ -645,8 +645,8 @@ func testChannelStoreDelete(t *testing.T, ss store.Store) { require.Equal(t, &model.ChannelList{}, list) } - if r := <-ss.Channel().PermanentDeleteByTeam(o1.TeamId); r.Err != nil { - t.Fatal(r.Err) + if err = ss.Channel().PermanentDeleteByTeam(o1.TeamId); err != nil { + t.Fatal(err) } } diff --git a/store/storetest/mocks/ChannelStore.go b/store/storetest/mocks/ChannelStore.go index f31bdf72f5..763c04eb2e 100644 --- a/store/storetest/mocks/ChannelStore.go +++ b/store/storetest/mocks/ChannelStore.go @@ -1158,15 +1158,15 @@ func (_m *ChannelStore) PermanentDelete(channelId string) store.StoreChannel { } // PermanentDeleteByTeam provides a mock function with given fields: teamId -func (_m *ChannelStore) PermanentDeleteByTeam(teamId string) store.StoreChannel { +func (_m *ChannelStore) PermanentDeleteByTeam(teamId string) *model.AppError { ret := _m.Called(teamId) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(string) store.StoreChannel); ok { + var r0 *model.AppError + if rf, ok := ret.Get(0).(func(string) *model.AppError); ok { r0 = rf(teamId) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).(*model.AppError) } }