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.
This commit is contained in:
Lunny Xiao 2022-10-28 19:05:39 +08:00 committed by GitHub
parent e09025fdce
commit f337c32e86
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 7 deletions

View file

@ -423,6 +423,8 @@ var migrations = []Migration{
NewMigration("Update counts of all open milestones", updateOpenMilestoneCounts), NewMigration("Update counts of all open milestones", updateOpenMilestoneCounts),
// v230 -> v231 // v230 -> v231
NewMigration("Add ConfidentialClient column (default true) to OAuth2Application table", addConfidentialClientColumnToOAuth2ApplicationTable), NewMigration("Add ConfidentialClient column (default true) to OAuth2Application table", addConfidentialClientColumnToOAuth2ApplicationTable),
// v231 -> v232
NewMigration("Add index for hook_task", addIndexForHookTask),
} }
// GetCurrentDBVersion returns the current db version // GetCurrentDBVersion returns the current db version

19
models/migrations/v231.go Normal file
View file

@ -0,0 +1,19 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package migrations
import (
"xorm.io/xorm"
)
func addIndexForHookTask(x *xorm.Engine) error {
type HookTask struct {
ID int64 `xorm:"pk autoincr"`
HookID int64 `xorm:"index"`
UUID string `xorm:"unique"`
}
return x.Sync(new(HookTask))
}

View file

@ -103,9 +103,9 @@ type HookResponse struct {
// HookTask represents a hook task. // HookTask represents a hook task.
type HookTask struct { type HookTask struct {
ID int64 `xorm:"pk autoincr"` ID int64 `xorm:"pk autoincr"`
HookID int64 HookID int64 `xorm:"index"`
UUID string UUID string `xorm:"unique"`
api.Payloader `xorm:"-"` api.Payloader `xorm:"-"`
PayloadContent string `xorm:"LONGTEXT"` PayloadContent string `xorm:"LONGTEXT"`
EventType HookEventType 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) return db.ErrCancelledf("Before deleting hook_task records for hook id %d", hookID)
default: default:
} }
if err = deleteDeliveredHookTasksByWebhook(hookID, numberToKeep); err != nil { if err = deleteDeliveredHookTasksByWebhook(ctx, hookID, numberToKeep); err != nil {
return err return err
} }
} }
@ -279,10 +279,10 @@ func CleanupHookTaskTable(ctx context.Context, cleanupType HookTaskCleanupType,
return nil 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) log.Trace("Deleting hook_task rows for webhook %d, keeping the most recent %d deliveries", hookID, numberDeliveriesToKeep)
deliveryDates := make([]int64, 0, 10) 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). Where("hook_task.hook_id = ? AND hook_task.is_delivered = ? AND hook_task.delivered is not null", hookID, true).
Cols("hook_task.delivered"). Cols("hook_task.delivered").
Join("INNER", "webhook", "hook_task.hook_id = webhook.id"). Join("INNER", "webhook", "hook_task.hook_id = webhook.id").
@ -294,7 +294,7 @@ func deleteDeliveredHookTasksByWebhook(hookID int64, numberDeliveriesToKeep int)
} }
if len(deliveryDates) > 0 { 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]). Where("hook_id = ? and is_delivered = ? and delivered <= ?", hookID, true, deliveryDates[0]).
Delete(new(HookTask)) Delete(new(HookTask))
if err != nil { if err != nil {