Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refix indices on actions table #20158

Merged
merged 9 commits into from
Jul 1, 2022
6 changes: 3 additions & 3 deletions models/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ func init() {

// TableIndices implements xorm's TableIndices interface
func (a *Action) TableIndices() []*schemas.Index {
repoIndex := schemas.NewIndex("r_u_d", schemas.IndexType)
repoIndex.AddColumn("repo_id", "user_id", "is_deleted")

actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType)
actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted")

repoIndex := schemas.NewIndex("r_c_u_d", schemas.IndexType)
repoIndex.AddColumn("repo_id", "created_unix", "user_id", "is_deleted")

return []*schemas.Index{actUserIndex, repoIndex}
}

Expand Down
9 changes: 7 additions & 2 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type Version struct {
Version int64
}

// Use noopMigration when there is a migration that has been no-oped
var noopMigration = func(_ *xorm.Engine) error { return nil }

// This is a sequence of migrations. Add new migrations to the bottom of the list.
// If you want to "retire" a migration, remove it from the top of the list and
// update minDBVersion accordingly
Expand Down Expand Up @@ -351,7 +354,7 @@ var migrations = []Migration{
// v198 -> v199
NewMigration("Add issue content history table", addTableIssueContentHistory),
// v199 -> v200
NewMigration("No-op (remote version is using AppState now)", addRemoteVersionTableNoop),
NewMigration("No-op (remote version is using AppState now)", noopMigration),
// v200 -> v201
NewMigration("Add table app_state", addTableAppState),
// v201 -> v202
Expand Down Expand Up @@ -388,9 +391,11 @@ var migrations = []Migration{
// v215 -> v216
NewMigration("allow to view files in PRs", addReviewViewedFiles),
// v216 -> v217
NewMigration("Improve Action table indices", improveActionTableIndices),
NewMigration("No-op (Improve Action table indices v1)", noopMigration),
// v217 -> v218
NewMigration("Alter hook_task table TEXT fields to LONGTEXT", alterHookTaskTextFieldsToLongText),
// v218 -> v219
NewMigration("Improve Action table indices v2", improveActionTableIndices),
}

// GetCurrentDBVersion returns the current db version
Expand Down
9 changes: 1 addition & 8 deletions models/migrations/v199.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,4 @@

package migrations

import (
"xorm.io/xorm"
)

func addRemoteVersionTableNoop(x *xorm.Engine) error {
// we used to use a table `remote_version` to store information for updater, now we use `AppState`, so this migration task is a no-op now.
return nil
}
// We used to use a table `remote_version` to store information for updater, now we use `AppState`, so this migration task is a no-op now.
42 changes: 2 additions & 40 deletions models/migrations/v216.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,5 @@

package migrations

import (
"code.gitea.io/gitea/modules/timeutil"

"xorm.io/xorm"
"xorm.io/xorm/schemas"
)

type improveActionTableIndicesAction struct {
ID int64 `xorm:"pk autoincr"`
UserID int64 // Receiver user id.
OpType int
ActUserID int64 // Action user id.
RepoID int64
CommentID int64 `xorm:"INDEX"`
IsDeleted bool `xorm:"NOT NULL DEFAULT false"`
RefName string
IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
Content string `xorm:"TEXT"`
CreatedUnix timeutil.TimeStamp `xorm:"created"`
}

// TableName sets the name of this table
func (a *improveActionTableIndicesAction) TableName() string {
return "action"
}

// TableIndices implements xorm's TableIndices interface
func (a *improveActionTableIndicesAction) TableIndices() []*schemas.Index {
actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType)
actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted")

repoIndex := schemas.NewIndex("r_c_u_d", schemas.IndexType)
repoIndex.AddColumn("repo_id", "created_unix", "user_id", "is_deleted")

return []*schemas.Index{actUserIndex, repoIndex}
}

func improveActionTableIndices(x *xorm.Engine) error {
return x.Sync2(&improveActionTableIndicesAction{})
}
// This migration added non-ideal indices to the action table which on larger datasets slowed things down
// it has been superceded by v218.go
46 changes: 46 additions & 0 deletions models/migrations/v218.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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 (
"code.gitea.io/gitea/modules/timeutil"

"xorm.io/xorm"
"xorm.io/xorm/schemas"
)

type improveActionTableIndicesAction struct {
ID int64 `xorm:"pk autoincr"`
UserID int64 // Receiver user id.
OpType int
ActUserID int64 // Action user id.
RepoID int64
CommentID int64 `xorm:"INDEX"`
IsDeleted bool `xorm:"NOT NULL DEFAULT false"`
RefName string
IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
Content string `xorm:"TEXT"`
CreatedUnix timeutil.TimeStamp `xorm:"created"`
}

// TableName sets the name of this table
func (*improveActionTableIndicesAction) TableName() string {
return "action"
}

// TableIndices implements xorm's TableIndices interface
func (*improveActionTableIndicesAction) TableIndices() []*schemas.Index {
repoIndex := schemas.NewIndex("r_u_d", schemas.IndexType)
zeripath marked this conversation as resolved.
Show resolved Hide resolved
repoIndex.AddColumn("repo_id", "user_id", "is_deleted")

actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType)
actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted")

return []*schemas.Index{actUserIndex, repoIndex}
}

func improveActionTableIndices(x *xorm.Engine) error {
return x.Sync2(&improveActionTableIndicesAction{})
}