From 5b70962f71bd463e8fd7c54d39c0be7e5e27fd12 Mon Sep 17 00:00:00 2001 From: Puneeth Reddy <45575072+therealpuneeth20@users.noreply.github.com> Date: Mon, 29 Apr 2019 07:22:44 -0700 Subject: [PATCH] MM-15294: migrate commandstore.Update to sync by default (#10745) * MM-15294: migrate commandstore.Update to sync by default * minor change for code consistency --- app/command.go | 19 +++++-------------- store/sqlstore/command_store.go | 22 ++++++++++------------ store/store.go | 2 +- store/storetest/command_store.go | 6 +++--- store/storetest/mocks/CommandStore.go | 19 ++++++++++++++----- 5 files changed, 33 insertions(+), 35 deletions(-) diff --git a/app/command.go b/app/command.go index d49aadd8c4..7981a09538 100644 --- a/app/command.go +++ b/app/command.go @@ -502,19 +502,15 @@ func (a *App) UpdateCommand(oldCmd, updatedCmd *model.Command) (*model.Command, updatedCmd.CreatorId = oldCmd.CreatorId updatedCmd.TeamId = oldCmd.TeamId - result := <-a.Srv.Store.Command().Update(updatedCmd) - if result.Err != nil { - return nil, result.Err - } - return result.Data.(*model.Command), nil + return a.Srv.Store.Command().Update(updatedCmd) } func (a *App) MoveCommand(team *model.Team, command *model.Command) *model.AppError { command.TeamId = team.Id - result := <-a.Srv.Store.Command().Update(command) - if result.Err != nil { - return result.Err + _, err := a.Srv.Store.Command().Update(command) + if err != nil { + return err } return nil @@ -527,12 +523,7 @@ func (a *App) RegenCommandToken(cmd *model.Command) (*model.Command, *model.AppE cmd.Token = model.NewId() - result := <-a.Srv.Store.Command().Update(cmd) - if result.Err != nil { - return nil, result.Err - } - - return result.Data.(*model.Command), nil + return a.Srv.Store.Command().Update(cmd) } func (a *App) DeleteCommand(commandId string) *model.AppError { diff --git a/store/sqlstore/command_store.go b/store/sqlstore/command_store.go index 22485bc2f6..8eaac1108d 100644 --- a/store/sqlstore/command_store.go +++ b/store/sqlstore/command_store.go @@ -132,20 +132,18 @@ func (s SqlCommandStore) PermanentDeleteByUser(userId string) *model.AppError { return nil } -func (s SqlCommandStore) Update(cmd *model.Command) store.StoreChannel { - return store.Do(func(result *store.StoreResult) { - cmd.UpdateAt = model.GetMillis() +func (s SqlCommandStore) Update(cmd *model.Command) (*model.Command, *model.AppError) { + cmd.UpdateAt = model.GetMillis() - if result.Err = cmd.IsValid(); result.Err != nil { - return - } + if err := cmd.IsValid(); err != nil { + return nil, err + } - if _, err := s.GetMaster().Update(cmd); err != nil { - result.Err = model.NewAppError("SqlCommandStore.Update", "store.sql_command.save.update.app_error", nil, "id="+cmd.Id+", "+err.Error(), http.StatusInternalServerError) - } else { - result.Data = cmd - } - }) + if _, err := s.GetMaster().Update(cmd); err != nil { + return nil, model.NewAppError("SqlCommandStore.Update", "store.sql_command.save.update.app_error", nil, "id="+cmd.Id+", "+err.Error(), http.StatusInternalServerError) + } + + return cmd, nil } func (s SqlCommandStore) AnalyticsCommandCount(teamId string) store.StoreChannel { diff --git a/store/store.go b/store/store.go index f1b4c5675f..9af90e32e3 100644 --- a/store/store.go +++ b/store/store.go @@ -411,7 +411,7 @@ type CommandStore interface { Delete(commandId string, time int64) StoreChannel PermanentDeleteByTeam(teamId string) StoreChannel PermanentDeleteByUser(userId string) *model.AppError - Update(hook *model.Command) StoreChannel + Update(hook *model.Command) (*model.Command, *model.AppError) AnalyticsCommandCount(teamId string) StoreChannel } diff --git a/store/storetest/command_store.go b/store/storetest/command_store.go index d74422fe87..852444c66a 100644 --- a/store/storetest/command_store.go +++ b/store/storetest/command_store.go @@ -218,13 +218,13 @@ func testCommandStoreUpdate(t *testing.T, ss store.Store) { o1.Token = model.NewId() - if r2 := <-ss.Command().Update(o1); r2.Err != nil { - t.Fatal(r2.Err) + if _, err := ss.Command().Update(o1); err != nil { + t.Fatal(err) } o1.URL = "junk" - if r2 := <-ss.Command().Update(o1); r2.Err == nil { + if _, err := ss.Command().Update(o1); err == nil { t.Fatal("should have failed - bad URL") } } diff --git a/store/storetest/mocks/CommandStore.go b/store/storetest/mocks/CommandStore.go index 87bf55a317..42c3fa68c9 100644 --- a/store/storetest/mocks/CommandStore.go +++ b/store/storetest/mocks/CommandStore.go @@ -151,17 +151,26 @@ func (_m *CommandStore) Save(webhook *model.Command) store.StoreChannel { } // Update provides a mock function with given fields: hook -func (_m *CommandStore) Update(hook *model.Command) store.StoreChannel { +func (_m *CommandStore) Update(hook *model.Command) (*model.Command, *model.AppError) { ret := _m.Called(hook) - var r0 store.StoreChannel - if rf, ok := ret.Get(0).(func(*model.Command) store.StoreChannel); ok { + var r0 *model.Command + if rf, ok := ret.Get(0).(func(*model.Command) *model.Command); ok { r0 = rf(hook) } else { if ret.Get(0) != nil { - r0 = ret.Get(0).(store.StoreChannel) + r0 = ret.Get(0).(*model.Command) } } - return r0 + var r1 *model.AppError + if rf, ok := ret.Get(1).(func(*model.Command) *model.AppError); ok { + r1 = rf(hook) + } else { + if ret.Get(1) != nil { + r1 = ret.Get(1).(*model.AppError) + } + } + + return r0, r1 }