MM-62159: Avoid SELECT * in channel_store_categories.go (#30424)

* 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>

* 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 <noreply@anthropic.com>
Этот коммит содержится в:
Jesse Hallam
2025-03-19 10:49:19 -03:00
коммит произвёл GitHub
родитель fd717cfa64
Коммит 594e8d3171
2 изменённых файлов: 20 добавлений и 15 удалений

Просмотреть файл

@@ -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

Просмотреть файл

@@ -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)