From f337c32e868381c6d2d948221aca0c59f8420c13 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Fri, 28 Oct 2022 19:05:39 +0800 Subject: 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. --- models/webhook/hooktask.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'models/webhook/hooktask.go') diff --git a/models/webhook/hooktask.go b/models/webhook/hooktask.go index 2b9b63c09b..246484aea9 100644 --- a/models/webhook/hooktask.go +++ b/models/webhook/hooktask.go @@ -103,9 +103,9 @@ type HookResponse struct { // HookTask represents a hook task. type HookTask struct { - ID int64 `xorm:"pk autoincr"` - HookID int64 - UUID string + ID int64 `xorm:"pk autoincr"` + HookID int64 `xorm:"index"` + UUID string `xorm:"unique"` api.Payloader `xorm:"-"` PayloadContent string `xorm:"LONGTEXT"` EventType HookEventType @@ -270,7 +270,7 @@ func CleanupHookTaskTable(ctx context.Context, cleanupType HookTaskCleanupType, return db.ErrCancelledf("Before deleting hook_task records for hook id %d", hookID) default: } - if err = deleteDeliveredHookTasksByWebhook(hookID, numberToKeep); err != nil { + if err = deleteDeliveredHookTasksByWebhook(ctx, hookID, numberToKeep); err != nil { return err } } @@ -279,10 +279,10 @@ func CleanupHookTaskTable(ctx context.Context, cleanupType HookTaskCleanupType, return nil } -func deleteDeliveredHookTasksByWebhook(hookID int64, numberDeliveriesToKeep int) error { +func deleteDeliveredHookTasksByWebhook(ctx context.Context, hookID int64, numberDeliveriesToKeep int) error { log.Trace("Deleting hook_task rows for webhook %d, keeping the most recent %d deliveries", hookID, numberDeliveriesToKeep) deliveryDates := make([]int64, 0, 10) - err := db.GetEngine(db.DefaultContext).Table("hook_task"). + err := db.GetEngine(ctx).Table("hook_task"). Where("hook_task.hook_id = ? AND hook_task.is_delivered = ? AND hook_task.delivered is not null", hookID, true). Cols("hook_task.delivered"). Join("INNER", "webhook", "hook_task.hook_id = webhook.id"). @@ -294,7 +294,7 @@ func deleteDeliveredHookTasksByWebhook(hookID int64, numberDeliveriesToKeep int) } if len(deliveryDates) > 0 { - deletes, err := db.GetEngine(db.DefaultContext). + deletes, err := db.GetEngine(ctx). Where("hook_id = ? and is_delivered = ? and delivered <= ?", hookID, true, deliveryDates[0]). Delete(new(HookTask)) if err != nil { -- cgit v1.2.3