Skip to content

Commit 0a52be2

Browse files
lunnyGusted
authored andcommitted
Small refactor to reduce unnecessary database queries and remove duplicated functions (go-gitea#33779)
(cherry picked from commit 6c8fb8d)
1 parent 4969424 commit 0a52be2

File tree

6 files changed

+22
-7
lines changed

6 files changed

+22
-7
lines changed

models/actions/schedule.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,12 @@ func init() {
4545
// GetSchedulesMapByIDs returns the schedules by given id slice.
4646
func GetSchedulesMapByIDs(ctx context.Context, ids []int64) (map[int64]*ActionSchedule, error) {
4747
schedules := make(map[int64]*ActionSchedule, len(ids))
48+
if len(ids) == 0 {
49+
return schedules, nil
50+
}
4851
return schedules, db.GetEngine(ctx).In("id", ids).Find(&schedules)
4952
}
5053

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

models/actions/schedule_spec_list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (specs SpecList) LoadSchedules(ctx context.Context) error {
3636
}
3737

3838
repoIDs := specs.GetRepoIDs()
39-
repos, err := GetReposMapByIDs(ctx, repoIDs)
39+
repos, err := repo_model.GetRepositoriesMapByIDs(ctx, repoIDs)
4040
if err != nil {
4141
return err
4242
}

models/db/context.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ func FindIDs(ctx context.Context, tableName, idCol string, cond builder.Cond) ([
269269
// DecrByIDs decreases the given column for entities of the "bean" type with one of the given ids by one
270270
// Timestamps of the entities won't be updated
271271
func DecrByIDs(ctx context.Context, ids []int64, decrCol string, bean any) error {
272+
if len(ids) == 0 {
273+
return nil
274+
}
272275
_, err := GetEngine(ctx).Decr(decrCol).In("id", ids).NoAutoCondition().NoAutoTime().Update(bean)
273276
return err
274277
}

models/issues/issue.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,9 @@ func GetIssueByID(ctx context.Context, id int64) (*Issue, error) {
546546
// If keepOrder is true, the order of the returned issues will be the same as the given IDs.
547547
func GetIssuesByIDs(ctx context.Context, issueIDs []int64, keepOrder ...bool) (IssueList, error) {
548548
issues := make([]*Issue, 0, len(issueIDs))
549+
if len(issueIDs) == 0 {
550+
return issues, nil
551+
}
549552

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

models/issues/label.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ func GetLabelByID(ctx context.Context, labelID int64) (*Label, error) {
303303
// GetLabelsByIDs returns a list of labels by IDs
304304
func GetLabelsByIDs(ctx context.Context, labelIDs []int64, cols ...string) ([]*Label, error) {
305305
labels := make([]*Label, 0, len(labelIDs))
306+
if len(labelIDs) == 0 {
307+
return labels, nil
308+
}
306309
return labels, db.GetEngine(ctx).Table("label").
307310
In("id", labelIDs).
308311
Asc("name").
@@ -379,6 +382,9 @@ func BuildLabelNamesIssueIDsCondition(labelNames []string) *builder.Builder {
379382
// it silently ignores label IDs that do not belong to the repository.
380383
func GetLabelsInRepoByIDs(ctx context.Context, repoID int64, labelIDs []int64) ([]*Label, error) {
381384
labels := make([]*Label, 0, len(labelIDs))
385+
if len(labelIDs) == 0 {
386+
return labels, nil
387+
}
382388
return labels, db.GetEngine(ctx).
383389
Where("repo_id = ?", repoID).
384390
In("id", labelIDs).
@@ -451,6 +457,9 @@ func GetLabelInOrgByID(ctx context.Context, orgID, labelID int64) (*Label, error
451457
// it silently ignores label IDs that do not belong to the organization.
452458
func GetLabelsInOrgByIDs(ctx context.Context, orgID int64, labelIDs []int64) ([]*Label, error) {
453459
labels := make([]*Label, 0, len(labelIDs))
460+
if len(labelIDs) == 0 {
461+
return labels, nil
462+
}
454463
return labels, db.GetEngine(ctx).
455464
Where("org_id = ?", orgID).
456465
In("id", labelIDs).

models/project/column.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,9 @@ func SetDefaultColumn(ctx context.Context, projectID, columnID int64) error {
293293

294294
func GetColumnsByIDs(ctx context.Context, projectID int64, columnsIDs []int64) (ColumnList, error) {
295295
columns := make([]*Column, 0, 5)
296+
if len(columnsIDs) == 0 {
297+
return columns, nil
298+
}
296299
if err := db.GetEngine(ctx).
297300
Where("project_id =?", projectID).
298301
In("id", columnsIDs).

0 commit comments

Comments
 (0)