Skip to content

Commit b958dba

Browse files
authored
Improve indices for action table (#23532)
Close #21611 Add the index mentioned in #21611 (comment) . Since we already have an index for `("created_unix", "user_id", "is_deleted")` columns on PostgreSQL, I removed the database type check to apply this index to all types of databases.
1 parent e52ea44 commit b958dba

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

models/activities/action.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,10 @@ func (a *Action) TableIndices() []*schemas.Index {
9898
actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType)
9999
actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted")
100100

101-
indices := []*schemas.Index{actUserIndex, repoIndex}
102-
if setting.Database.Type.IsPostgreSQL() {
103-
cudIndex := schemas.NewIndex("c_u_d", schemas.IndexType)
104-
cudIndex.AddColumn("created_unix", "user_id", "is_deleted")
105-
indices = append(indices, cudIndex)
106-
}
101+
cudIndex := schemas.NewIndex("c_u_d", schemas.IndexType)
102+
cudIndex.AddColumn("created_unix", "user_id", "is_deleted")
103+
104+
indices := []*schemas.Index{actUserIndex, repoIndex, cudIndex}
107105

108106
return indices
109107
}

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ var migrations = []Migration{
475475
NewMigration("Fix incorrect project type", v1_20.FixIncorrectProjectType),
476476
// v248 -> v249
477477
NewMigration("Add version column to action_runner table", v1_20.AddVersionToActionRunner),
478+
// v249 -> v250
479+
NewMigration("Improve Action table indices v3", v1_20.ImproveActionTableIndices),
478480
}
479481

480482
// GetCurrentDBVersion returns the current db version

models/migrations/v1_20/v249.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2023 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package v1_20 //nolint
5+
6+
import (
7+
"code.gitea.io/gitea/modules/timeutil"
8+
9+
"xorm.io/xorm"
10+
"xorm.io/xorm/schemas"
11+
)
12+
13+
type Action struct {
14+
UserID int64 // Receiver user id.
15+
ActUserID int64 // Action user id.
16+
RepoID int64
17+
IsDeleted bool `xorm:"NOT NULL DEFAULT false"`
18+
IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
19+
CreatedUnix timeutil.TimeStamp `xorm:"created"`
20+
}
21+
22+
// TableName sets the name of this table
23+
func (a *Action) TableName() string {
24+
return "action"
25+
}
26+
27+
// TableIndices implements xorm's TableIndices interface
28+
func (a *Action) TableIndices() []*schemas.Index {
29+
repoIndex := schemas.NewIndex("r_u_d", schemas.IndexType)
30+
repoIndex.AddColumn("repo_id", "user_id", "is_deleted")
31+
32+
actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType)
33+
actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted")
34+
35+
cudIndex := schemas.NewIndex("c_u_d", schemas.IndexType)
36+
cudIndex.AddColumn("created_unix", "user_id", "is_deleted")
37+
38+
indices := []*schemas.Index{actUserIndex, repoIndex, cudIndex}
39+
40+
return indices
41+
}
42+
43+
func ImproveActionTableIndices(x *xorm.Engine) error {
44+
return x.Sync(new(Action))
45+
}

0 commit comments

Comments
 (0)