Skip to content

Commit f337c32

Browse files
authored
Add index for hook_task table (#21545)
Since `hook_id` and `uuid` will become a search condition column. It's better to add some index for them.
1 parent e09025f commit f337c32

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

models/migrations/migrations.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ var migrations = []Migration{
423423
NewMigration("Update counts of all open milestones", updateOpenMilestoneCounts),
424424
// v230 -> v231
425425
NewMigration("Add ConfidentialClient column (default true) to OAuth2Application table", addConfidentialClientColumnToOAuth2ApplicationTable),
426+
// v231 -> v232
427+
NewMigration("Add index for hook_task", addIndexForHookTask),
426428
}
427429

428430
// GetCurrentDBVersion returns the current db version

models/migrations/v231.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2022 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package migrations
6+
7+
import (
8+
"xorm.io/xorm"
9+
)
10+
11+
func addIndexForHookTask(x *xorm.Engine) error {
12+
type HookTask struct {
13+
ID int64 `xorm:"pk autoincr"`
14+
HookID int64 `xorm:"index"`
15+
UUID string `xorm:"unique"`
16+
}
17+
18+
return x.Sync(new(HookTask))
19+
}

models/webhook/hooktask.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ type HookResponse struct {
103103

104104
// HookTask represents a hook task.
105105
type HookTask struct {
106-
ID int64 `xorm:"pk autoincr"`
107-
HookID int64
108-
UUID string
106+
ID int64 `xorm:"pk autoincr"`
107+
HookID int64 `xorm:"index"`
108+
UUID string `xorm:"unique"`
109109
api.Payloader `xorm:"-"`
110110
PayloadContent string `xorm:"LONGTEXT"`
111111
EventType HookEventType
@@ -270,7 +270,7 @@ func CleanupHookTaskTable(ctx context.Context, cleanupType HookTaskCleanupType,
270270
return db.ErrCancelledf("Before deleting hook_task records for hook id %d", hookID)
271271
default:
272272
}
273-
if err = deleteDeliveredHookTasksByWebhook(hookID, numberToKeep); err != nil {
273+
if err = deleteDeliveredHookTasksByWebhook(ctx, hookID, numberToKeep); err != nil {
274274
return err
275275
}
276276
}
@@ -279,10 +279,10 @@ func CleanupHookTaskTable(ctx context.Context, cleanupType HookTaskCleanupType,
279279
return nil
280280
}
281281

282-
func deleteDeliveredHookTasksByWebhook(hookID int64, numberDeliveriesToKeep int) error {
282+
func deleteDeliveredHookTasksByWebhook(ctx context.Context, hookID int64, numberDeliveriesToKeep int) error {
283283
log.Trace("Deleting hook_task rows for webhook %d, keeping the most recent %d deliveries", hookID, numberDeliveriesToKeep)
284284
deliveryDates := make([]int64, 0, 10)
285-
err := db.GetEngine(db.DefaultContext).Table("hook_task").
285+
err := db.GetEngine(ctx).Table("hook_task").
286286
Where("hook_task.hook_id = ? AND hook_task.is_delivered = ? AND hook_task.delivered is not null", hookID, true).
287287
Cols("hook_task.delivered").
288288
Join("INNER", "webhook", "hook_task.hook_id = webhook.id").
@@ -294,7 +294,7 @@ func deleteDeliveredHookTasksByWebhook(hookID int64, numberDeliveriesToKeep int)
294294
}
295295

296296
if len(deliveryDates) > 0 {
297-
deletes, err := db.GetEngine(db.DefaultContext).
297+
deletes, err := db.GetEngine(ctx).
298298
Where("hook_id = ? and is_delivered = ? and delivered <= ?", hookID, true, deliveryDates[0]).
299299
Delete(new(HookTask))
300300
if err != nil {

0 commit comments

Comments
 (0)