Skip to content

Commit 6c8fb8d

Browse files
authored
Small refactor to reduce unnecessary database queries and remove duplicated functions (#33779)
1 parent 75e85c2 commit 6c8fb8d

File tree

10 files changed

+32
-12
lines changed

10 files changed

+32
-12
lines changed

models/actions/schedule.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,12 @@ func init() {
4343
// GetSchedulesMapByIDs returns the schedules by given id slice.
4444
func GetSchedulesMapByIDs(ctx context.Context, ids []int64) (map[int64]*ActionSchedule, error) {
4545
schedules := make(map[int64]*ActionSchedule, len(ids))
46+
if len(ids) == 0 {
47+
return schedules, nil
48+
}
4649
return schedules, db.GetEngine(ctx).In("id", ids).Find(&schedules)
4750
}
4851

49-
// GetReposMapByIDs returns the repos by given id slice.
50-
func GetReposMapByIDs(ctx context.Context, ids []int64) (map[int64]*repo_model.Repository, error) {
51-
repos := make(map[int64]*repo_model.Repository, len(ids))
52-
return repos, db.GetEngine(ctx).In("id", ids).Find(&repos)
53-
}
54-
5552
// CreateScheduleTask creates new schedule task.
5653
func CreateScheduleTask(ctx context.Context, rows []*ActionSchedule) error {
5754
// Return early if there are no rows to insert

models/actions/schedule_spec_list.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (specs SpecList) LoadSchedules(ctx context.Context) error {
3232
}
3333

3434
repoIDs := specs.GetRepoIDs()
35-
repos, err := GetReposMapByIDs(ctx, repoIDs)
35+
repos, err := repo_model.GetRepositoriesMapByIDs(ctx, repoIDs)
3636
if err != nil {
3737
return err
3838
}

models/db/context.go

+3
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ func FindIDs(ctx context.Context, tableName, idCol string, cond builder.Cond) ([
289289
// DecrByIDs decreases the given column for entities of the "bean" type with one of the given ids by one
290290
// Timestamps of the entities won't be updated
291291
func DecrByIDs(ctx context.Context, ids []int64, decrCol string, bean any) error {
292+
if len(ids) == 0 {
293+
return nil
294+
}
292295
_, err := GetEngine(ctx).Decr(decrCol).In("id", ids).NoAutoCondition().NoAutoTime().Update(bean)
293296
return err
294297
}

models/issues/issue.go

+3
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,9 @@ func GetIssueByID(ctx context.Context, id int64) (*Issue, error) {
595595
// If keepOrder is true, the order of the returned issues will be the same as the given IDs.
596596
func GetIssuesByIDs(ctx context.Context, issueIDs []int64, keepOrder ...bool) (IssueList, error) {
597597
issues := make([]*Issue, 0, len(issueIDs))
598+
if len(issueIDs) == 0 {
599+
return issues, nil
600+
}
598601

599602
if err := db.GetEngine(ctx).In("id", issueIDs).Find(&issues); err != nil {
600603
return nil, err

models/issues/label.go

+9
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ func GetLabelByID(ctx context.Context, labelID int64) (*Label, error) {
299299
// GetLabelsByIDs returns a list of labels by IDs
300300
func GetLabelsByIDs(ctx context.Context, labelIDs []int64, cols ...string) ([]*Label, error) {
301301
labels := make([]*Label, 0, len(labelIDs))
302+
if len(labelIDs) == 0 {
303+
return labels, nil
304+
}
302305
return labels, db.GetEngine(ctx).Table("label").
303306
In("id", labelIDs).
304307
Asc("name").
@@ -375,6 +378,9 @@ func BuildLabelNamesIssueIDsCondition(labelNames []string) *builder.Builder {
375378
// it silently ignores label IDs that do not belong to the repository.
376379
func GetLabelsInRepoByIDs(ctx context.Context, repoID int64, labelIDs []int64) ([]*Label, error) {
377380
labels := make([]*Label, 0, len(labelIDs))
381+
if len(labelIDs) == 0 {
382+
return labels, nil
383+
}
378384
return labels, db.GetEngine(ctx).
379385
Where("repo_id = ?", repoID).
380386
In("id", labelIDs).
@@ -447,6 +453,9 @@ func GetLabelInOrgByID(ctx context.Context, orgID, labelID int64) (*Label, error
447453
// it silently ignores label IDs that do not belong to the organization.
448454
func GetLabelsInOrgByIDs(ctx context.Context, orgID int64, labelIDs []int64) ([]*Label, error) {
449455
labels := make([]*Label, 0, len(labelIDs))
456+
if len(labelIDs) == 0 {
457+
return labels, nil
458+
}
450459
return labels, db.GetEngine(ctx).
451460
Where("org_id = ?", orgID).
452461
In("id", labelIDs).

models/organization/team_list.go

+3
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,8 @@ func GetTeamsByOrgIDs(ctx context.Context, orgIDs []int64) (TeamList, error) {
133133

134134
func GetTeamsByIDs(ctx context.Context, teamIDs []int64) (map[int64]*Team, error) {
135135
teams := make(map[int64]*Team, len(teamIDs))
136+
if len(teamIDs) == 0 {
137+
return teams, nil
138+
}
136139
return teams, db.GetEngine(ctx).Where(builder.In("`id`", teamIDs)).Find(&teams)
137140
}

models/project/column.go

+3
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ func UpdateColumnSorting(ctx context.Context, cl ColumnList) error {
336336

337337
func GetColumnsByIDs(ctx context.Context, projectID int64, columnsIDs []int64) (ColumnList, error) {
338338
columns := make([]*Column, 0, 5)
339+
if len(columnsIDs) == 0 {
340+
return columns, nil
341+
}
339342
if err := db.GetEngine(ctx).
340343
Where("project_id =?", projectID).
341344
In("id", columnsIDs).

models/repo/repo.go

+3
Original file line numberDiff line numberDiff line change
@@ -839,6 +839,9 @@ func GetRepositoryByID(ctx context.Context, id int64) (*Repository, error) {
839839
// GetRepositoriesMapByIDs returns the repositories by given id slice.
840840
func GetRepositoriesMapByIDs(ctx context.Context, ids []int64) (map[int64]*Repository, error) {
841841
repos := make(map[int64]*Repository, len(ids))
842+
if len(ids) == 0 {
843+
return repos, nil
844+
}
842845
return repos, db.GetEngine(ctx).In("id", ids).Find(&repos)
843846
}
844847

models/repo/repo_list.go

-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ import (
2121
"xorm.io/builder"
2222
)
2323

24-
// FindReposMapByIDs find repos as map
25-
func FindReposMapByIDs(ctx context.Context, repoIDs []int64, res map[int64]*Repository) error {
26-
return db.GetEngine(ctx).In("id", repoIDs).Find(&res)
27-
}
28-
2924
// RepositoryListDefaultPageSize is the default number of repositories
3025
// to load in memory when running administrative tasks on all (or almost
3126
// all) of them.

models/user/user_list.go

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111

1212
func GetUsersMapByIDs(ctx context.Context, userIDs []int64) (map[int64]*User, error) {
1313
userMaps := make(map[int64]*User, len(userIDs))
14+
if len(userIDs) == 0 {
15+
return userMaps, nil
16+
}
17+
1418
left := len(userIDs)
1519
for left > 0 {
1620
limit := db.DefaultMaxInSize

0 commit comments

Comments
 (0)