From cc86ec9f506e4c02d1a2218d98100dc8bde0153b Mon Sep 17 00:00:00 2001 From: Claudio Costa Date: Sat, 27 Feb 2021 13:54:13 +0100 Subject: [PATCH] [MM-33333] Fix GetSidebarCategories() failing on db replicas (#17031) * Fix GetSidebarCategories failing on replicas * Simplify further * Add comment --- store/sqlstore/channel_store_categories.go | 34 ++++++++-------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/store/sqlstore/channel_store_categories.go b/store/sqlstore/channel_store_categories.go index 8a8e5ffc34..8a978fd55f 100644 --- a/store/sqlstore/channel_store_categories.go +++ b/store/sqlstore/channel_store_categories.go @@ -14,6 +14,12 @@ import ( "github.com/mattermost/mattermost-server/v5/store" ) +// dbSelecter is an interface used to enable some internal store methods to +// accept both transactions (*gorp.Transaction) and common db handlers (*gorp.DbMap). +type dbSelecter interface { + Select(i interface{}, query string, args ...interface{}) ([]interface{}, error) +} + func (s SqlChannelStore) CreateInitialSidebarCategories(userId, teamId string) (*model.OrderedSidebarCategories, error) { transaction, err := s.GetMaster().Begin() if err != nil { @@ -366,7 +372,7 @@ func (s SqlChannelStore) completePopulatingCategoryChannels(category *model.Side return result, nil } -func (s SqlChannelStore) completePopulatingCategoryChannelsT(transaction *gorp.Transaction, category *model.SidebarCategoryWithChannels) (*model.SidebarCategoryWithChannels, error) { +func (s SqlChannelStore) completePopulatingCategoryChannelsT(db dbSelecter, category *model.SidebarCategoryWithChannels) (*model.SidebarCategoryWithChannels, error) { if category.Type == model.SidebarCategoryCustom || category.Type == model.SidebarCategoryFavorites { return category, nil } @@ -411,7 +417,7 @@ func (s SqlChannelStore) completePopulatingCategoryChannelsT(transaction *gorp.T return nil, errors.Wrap(err, "channel_tosql") } - if _, err = transaction.Select(&channels, sql, args...); err != nil { + if _, err = db.Select(&channels, sql, args...); err != nil { return nil, store.NewErrNotFound("ChannelMembers", "") } @@ -451,7 +457,7 @@ func (s SqlChannelStore) GetSidebarCategory(categoryId string) (*model.SidebarCa return s.completePopulatingCategoryChannels(result) } -func (s SqlChannelStore) getSidebarCategoriesT(transaction *gorp.Transaction, userId, teamId string) (*model.OrderedSidebarCategories, error) { +func (s SqlChannelStore) getSidebarCategoriesT(db dbSelecter, userId, teamId string) (*model.OrderedSidebarCategories, error) { oc := model.OrderedSidebarCategories{ Categories: make(model.SidebarCategoriesWithChannels, 0), Order: make([]string, 0), @@ -471,7 +477,7 @@ func (s SqlChannelStore) getSidebarCategoriesT(transaction *gorp.Transaction, us return nil, errors.Wrap(err, "sidebar_categories_tosql") } - if _, err = transaction.Select(&categories, query, args...); err != nil { + if _, err = db.Select(&categories, query, args...); err != nil { return nil, store.NewErrNotFound("SidebarCategories", fmt.Sprintf("userId=%s,teamId=%s", userId, teamId)) } for _, category := range categories { @@ -495,7 +501,7 @@ func (s SqlChannelStore) getSidebarCategoriesT(transaction *gorp.Transaction, us } } for _, category := range oc.Categories { - if _, err := s.completePopulatingCategoryChannelsT(transaction, category); err != nil { + if _, err := s.completePopulatingCategoryChannelsT(db, category); err != nil { return nil, err } } @@ -504,23 +510,7 @@ func (s SqlChannelStore) getSidebarCategoriesT(transaction *gorp.Transaction, us } func (s SqlChannelStore) GetSidebarCategories(userId, teamId string) (*model.OrderedSidebarCategories, error) { - transaction, err := s.GetReplica().Begin() - if err != nil { - return nil, errors.Wrap(err, "begin_transaction") - } - - defer finalizeTransaction(transaction) - - oc, err := s.getSidebarCategoriesT(transaction, userId, teamId) - if err != nil { - return nil, err - } - - if err = transaction.Commit(); err != nil { - return nil, errors.Wrap(err, "commit_transaction") - } - - return oc, nil + return s.getSidebarCategoriesT(s.GetReplica(), userId, teamId) } func (s SqlChannelStore) GetSidebarCategoryOrder(userId, teamId string) ([]string, error) {