From cb89c53dccb91f6e2bdba4f9b43c53b50b28ad29 Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Thu, 16 Jul 2020 12:52:09 -0400 Subject: [PATCH] MM-26826 Populate orphaned channels in a transaction (#15016) --- store/sqlstore/channel_store.go | 38 +++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/store/sqlstore/channel_store.go b/store/sqlstore/channel_store.go index ea3400d892..fef6789f07 100644 --- a/store/sqlstore/channel_store.go +++ b/store/sqlstore/channel_store.go @@ -3562,6 +3562,25 @@ func (s SqlChannelStore) CreateSidebarCategory(userId, teamId string, newCategor } func (s SqlChannelStore) completePopulatingCategoryChannels(category *model.SidebarCategoryWithChannels) (*model.SidebarCategoryWithChannels, *model.AppError) { + transaction, err := s.GetMaster().Begin() + if err != nil { + return nil, model.NewAppError("SqlChannelStore.completePopulatingCategoryChannels", "store.sql_channel.sidebar_categories.open_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) + } + defer finalizeTransaction(transaction) + + result, appErr := s.completePopulatingCategoryChannelsT(transaction, category) + if appErr != nil { + return nil, appErr + } + + if err = transaction.Commit(); err != nil { + return nil, model.NewAppError("SqlChannelStore.completePopulatingCategoryChannels", "store.sql_channel.sidebar_categories.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) + } + + return result, nil +} + +func (s SqlChannelStore) completePopulatingCategoryChannelsT(transation *gorp.Transaction, category *model.SidebarCategoryWithChannels) (*model.SidebarCategoryWithChannels, *model.AppError) { if category.Type == model.SidebarCategoryCustom || category.Type == model.SidebarCategoryFavorites { return category, nil } @@ -3603,9 +3622,10 @@ func (s SqlChannelStore) completePopulatingCategoryChannels(category *model.Side }). OrderBy("DisplayName ASC").ToSql() - if _, err := s.GetReplica().Select(&channels, sql, args...); err != nil { - return nil, model.NewAppError("SqlPostStore.completePopulatingCategoryChannels", "store.sql_channel.sidebar_categories.app_error", nil, err.Error(), http.StatusNotFound) + if _, err := transation.Select(&channels, sql, args...); err != nil { + return nil, model.NewAppError("SqlPostStore.completePopulatingCategoryChannelsT", "store.sql_channel.sidebar_categories.app_error", nil, err.Error(), http.StatusNotFound) } + category.Channels = append(channels, category.Channels...) return category, nil } @@ -3650,7 +3670,7 @@ func (s SqlChannelStore) getSidebarCategoriesT(transaction *gorp.Transaction, us }). OrderBy("SidebarCategories.SortOrder ASC, SidebarChannels.SortOrder ASC").ToSql() - if _, err := s.GetReplica().Select(&categories, sql, args...); err != nil { + if _, err := transaction.Select(&categories, sql, args...); err != nil { return nil, model.NewAppError("SqlPostStore.GetSidebarCategories", "store.sql_channel.sidebar_categories.app_error", nil, err.Error(), http.StatusNotFound) } for _, category := range categories { @@ -3674,7 +3694,7 @@ func (s SqlChannelStore) getSidebarCategoriesT(transaction *gorp.Transaction, us } } for _, category := range oc.Categories { - if _, err := s.completePopulatingCategoryChannels(category); err != nil { + if _, err := s.completePopulatingCategoryChannelsT(transaction, category); err != nil { return nil, err } } @@ -3913,13 +3933,9 @@ func (s SqlChannelStore) UpdateSidebarCategories(userId, teamId string, categori updatedCategories = append(updatedCategories, updatedCategory) } - if err = transaction.Commit(); err != nil { - return nil, model.NewAppError("SqlChannelStore.UpdateSidebarCategory", "store.sql_channel.sidebar_categories.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) - } - // Ensure Channels are populated for Channels/Direct Messages category if they change for i, updatedCategory := range updatedCategories { - populated, err := s.completePopulatingCategoryChannels(updatedCategory) + populated, err := s.completePopulatingCategoryChannelsT(transaction, updatedCategory) if err != nil { return nil, model.NewAppError("SqlPostStore.UpdateSidebarCategory", "store.sql_channel.sidebar_categories.app_error", nil, err.Error(), http.StatusInternalServerError) } @@ -3927,6 +3943,10 @@ func (s SqlChannelStore) UpdateSidebarCategories(userId, teamId string, categori updatedCategories[i] = populated } + if err = transaction.Commit(); err != nil { + return nil, model.NewAppError("SqlChannelStore.UpdateSidebarCategory", "store.sql_channel.sidebar_categories.commit_transaction.app_error", nil, err.Error(), http.StatusInternalServerError) + } + return updatedCategories, nil }