From 594e8d31714703c6df036f0bfa1cdde19ebdb5f1 Mon Sep 17 00:00:00 2001 From: Jesse Hallam Date: Wed, 19 Mar 2025 10:49:19 -0300 Subject: [PATCH] MM-62159: Avoid SELECT * in channel_store_categories.go (#30424) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * MM-62159: Avoid SELECT * in channel_store_categories.go - Added sidebarCategorySelectQuery field to SqlChannelStore struct - Replaced SELECT * with explicit column selection in GetSidebarCategory and getSidebarCategoriesT functions - Updated raw SQL query in addChannelToFavoritesCategoryT to use explicit column selection - Made the implementation more resilient to schema changes 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude * use sc alias, simplify * MM-62159: Fix ambiguous ID column in sidebar category queries - Modified sidebarCategorySelectQuery initialization to explicitly use "sc" table alias for all columns - Prevents "Column 'Id' in field list is ambiguous" error when joining with other tables 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude * MM-62159: Consistently use table aliases in sidebar categories queries - Added 'sc' table alias to all sidebar category queries - Ensures consistency and avoids ambiguous column errors in future joins 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude * MM-62159: Replace 'sc' alias with full 'SidebarCategories' table name - Replaced all instances of the 'sc' alias with the full table name 'SidebarCategories' - Updated the SidebarCategories query builder to use the full table name - Removed commented-out debug printf statement 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .../channels/store/sqlstore/channel_store.go | 7 ++++- .../sqlstore/channel_store_categories.go | 28 +++++++++---------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/server/channels/store/sqlstore/channel_store.go b/server/channels/store/sqlstore/channel_store.go index 420f18de02..2c6edd1296 100644 --- a/server/channels/store/sqlstore/channel_store.go +++ b/server/channels/store/sqlstore/channel_store.go @@ -27,7 +27,8 @@ type SqlChannelStore struct { *SqlStore metrics einterfaces.MetricsInterface - tableSelectQuery sq.SelectBuilder + tableSelectQuery sq.SelectBuilder + sidebarCategorySelectQuery sq.SelectBuilder // prepared query builders for use in multiple methods channelMembersForTeamWithSchemeSelectQuery sq.SelectBuilder @@ -507,6 +508,10 @@ func newSqlChannelStore(sqlStore *SqlStore, metrics einterfaces.MetricsInterface s.tableSelectQuery = s.getQueryBuilder().Select(channelSliceColumns()...).From("Channels") + s.sidebarCategorySelectQuery = s.getQueryBuilder(). + Select("SidebarCategories.Id", "SidebarCategories.UserId", "SidebarCategories.TeamId", "SidebarCategories.SortOrder", "SidebarCategories.Sorting", "SidebarCategories.Type", "SidebarCategories.DisplayName", "SidebarCategories.Muted", "SidebarCategories.Collapsed"). + From("SidebarCategories") + s.initializeQueries() return &s diff --git a/server/channels/store/sqlstore/channel_store_categories.go b/server/channels/store/sqlstore/channel_store_categories.go index 51cecaabb2..62ffe3f14e 100644 --- a/server/channels/store/sqlstore/channel_store_categories.go +++ b/server/channels/store/sqlstore/channel_store_categories.go @@ -54,11 +54,11 @@ func (s SqlChannelStore) CreateInitialSidebarCategories(c request.CTX, userId st func (s SqlChannelStore) createInitialSidebarCategoriesT(transaction *sqlxTxWrapper, userId string, excludedTeamIDs []string, opts *store.SidebarCategorySearchOpts) error { query := s.getQueryBuilder(). - Select("Type, TeamId"). + Select("SidebarCategories.Type, SidebarCategories.TeamId"). From("SidebarCategories"). Where(sq.Eq{ - "UserId": userId, - "Type": []model.SidebarCategoryType{ + "SidebarCategories.UserId": userId, + "SidebarCategories.Type": []model.SidebarCategoryType{ model.SidebarCategoryFavorites, model.SidebarCategoryChannels, model.SidebarCategoryDirectMessages, @@ -66,9 +66,9 @@ func (s SqlChannelStore) createInitialSidebarCategoriesT(transaction *sqlxTxWrap }) if !opts.ExcludeTeam { - query = query.Where(sq.Eq{"TeamId": opts.TeamID}) + query = query.Where(sq.Eq{"SidebarCategories.TeamId": opts.TeamID}) } else { - query = query.Where(sq.NotEq{"TeamId": opts.TeamID}) + query = query.Where(sq.NotEq{"SidebarCategories.TeamId": opts.TeamID}) } selectQuery, selectParams, err := query.ToSql() @@ -493,12 +493,13 @@ func (s SqlChannelStore) completePopulatingCategoryChannelsT(db dbSelecter, cate } func (s SqlChannelStore) GetSidebarCategory(categoryId string) (*model.SidebarCategoryWithChannels, error) { - sql, args, err := s.getQueryBuilder(). - Select("SidebarCategories.*", "SidebarChannels.ChannelId"). - From("SidebarCategories"). + query := s.sidebarCategorySelectQuery. + Columns("SidebarChannels.ChannelId"). LeftJoin("SidebarChannels ON SidebarChannels.CategoryId=SidebarCategories.Id"). Where(sq.Eq{"SidebarCategories.Id": categoryId}). - OrderBy("SidebarChannels.SortOrder ASC").ToSql() + OrderBy("SidebarChannels.SortOrder ASC") + + sql, args, err := query.ToSql() if err != nil { return nil, errors.Wrap(err, "sidebar_category_tosql") } @@ -531,10 +532,9 @@ func (s SqlChannelStore) getSidebarCategoriesT(db dbSelecter, userId string, opt } categories := []*sidebarCategoryForJoin{} - query := s.getQueryBuilder(). - Select("SidebarCategories.*", "SidebarChannels.ChannelId"). - From("SidebarCategories"). - LeftJoin("SidebarChannels ON SidebarChannels.CategoryId=Id"). + query := s.sidebarCategorySelectQuery. + Columns("SidebarChannels.ChannelId"). + LeftJoin("SidebarChannels ON SidebarChannels.CategoryId=SidebarCategories.Id"). InnerJoin("Teams ON Teams.Id=SidebarCategories.TeamId"). InnerJoin("TeamMembers ON TeamMembers.TeamId=SidebarCategories.TeamId"). Where(sq.And{ @@ -942,7 +942,7 @@ func (s SqlChannelStore) addChannelToFavoritesCategoryT(transaction *sqlxTxWrapp } var channel model.Channel - if err := transaction.Get(&channel, `SELECT * FROM Channels WHERE Id=?`, preference.Name); err != nil { + if err := transaction.Get(&channel, `SELECT Id, TeamId FROM Channels WHERE Id=?`, preference.Name); err != nil { return errors.Wrapf(err, "Failed to get favorited channel with id=%s", preference.Name) } else if channel.Id == "" { return store.NewErrNotFound("Channel", preference.Name)