MM-62156: Avoid SELECT * in retention_policy_store.go (#30458)

* MM-62156: Avoid SELECT * in retention_policy_store.go

- Modified subQueryIN function to use specific column name instead of SELECT *
- Improved code comments to explain the change
- Maintained same functionality while avoiding SELECT *

Fixes: https://mattermost.atlassian.net/browse/MM-62156

🤖 Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>

* simplify subQueryIN comments

* inline part of subQueryIN for greater clarity

---------

Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Mattermost Build <build@mattermost.com>
This commit is contained in:
Jesse Hallam 2025-04-14 09:21:13 -03:00 committed by GitHub
parent 0f860f0512
commit 949a19efc1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -642,7 +642,8 @@ func (s *SqlRetentionPolicyStore) RemoveTeams(policyId string, teamIds []string)
func subQueryIN(property string, query sq.SelectBuilder) sq.Sqlizer {
queryString, args := query.MustSql()
subQuery := fmt.Sprintf("%s IN (SELECT * FROM (%s) AS A)", property, queryString)
subQuery := fmt.Sprintf("%s IN (%s)", property, queryString)
return sq.Expr(subQuery, args...)
}
@ -650,11 +651,14 @@ func subQueryIN(property string, query sq.SelectBuilder) sq.Sqlizer {
// where a channel or team no longer exists.
func (s *SqlRetentionPolicyStore) DeleteOrphanedRows(limit int) (deleted int64, err error) {
// We need the extra level of nesting to deal with MySQL's locking
rpcSubQuery := sq.Select("ChannelId").
From("RetentionPoliciesChannels").
LeftJoin("Channels ON RetentionPoliciesChannels.ChannelId = Channels.Id").
Where("Channels.Id IS NULL").
Limit(uint64(limit))
rpcSubQuery := sq.Select("ChannelId").FromSelect(
sq.Select("ChannelId").
From("RetentionPoliciesChannels").
LeftJoin("Channels ON RetentionPoliciesChannels.ChannelId = Channels.Id").
Where("Channels.Id IS NULL").
Limit(uint64(limit)),
"A",
)
rpcDeleteQuery, rpcArgs, err := s.getQueryBuilder().
Delete("RetentionPoliciesChannels").
@ -664,11 +668,15 @@ func (s *SqlRetentionPolicyStore) DeleteOrphanedRows(limit int) (deleted int64,
return int64(0), errors.Wrap(err, "retention_policies_channels_tosql")
}
rptSubQuery := sq.Select("TeamId").
From("RetentionPoliciesTeams").
LeftJoin("Teams ON RetentionPoliciesTeams.TeamId = Teams.Id").
Where("Teams.Id IS NULL").
Limit(uint64(limit))
// We need the extra level of nesting to deal with MySQL's locking
rptSubQuery := sq.Select("TeamId").FromSelect(
sq.Select("TeamId").
From("RetentionPoliciesTeams").
LeftJoin("Teams ON RetentionPoliciesTeams.TeamId = Teams.Id").
Where("Teams.Id IS NULL").
Limit(uint64(limit)),
"A",
)
rptDeleteQuery, rptArgs, err := s.getQueryBuilder().
Delete("RetentionPoliciesTeams").