Skip to content

Commit 5d653cc

Browse files
authored
Improve action table indices (#19472)
1 parent dd1ed35 commit 5d653cc

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

models/action.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"code.gitea.io/gitea/modules/util"
3131

3232
"xorm.io/builder"
33+
"xorm.io/xorm/schemas"
3334
)
3435

3536
// ActionType represents the type of an action.
@@ -70,25 +71,36 @@ const (
7071
// used in template render.
7172
type Action struct {
7273
ID int64 `xorm:"pk autoincr"`
73-
UserID int64 `xorm:"INDEX"` // Receiver user id.
74+
UserID int64 // Receiver user id.
7475
OpType ActionType
75-
ActUserID int64 `xorm:"INDEX"` // Action user id.
76-
ActUser *user_model.User `xorm:"-"`
77-
RepoID int64 `xorm:"INDEX"`
76+
ActUserID int64 // Action user id.
77+
ActUser *user_model.User `xorm:"-"`
78+
RepoID int64
7879
Repo *repo_model.Repository `xorm:"-"`
7980
CommentID int64 `xorm:"INDEX"`
8081
Comment *issues_model.Comment `xorm:"-"`
81-
IsDeleted bool `xorm:"INDEX NOT NULL DEFAULT false"`
82+
IsDeleted bool `xorm:"NOT NULL DEFAULT false"`
8283
RefName string
83-
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
84+
IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
8485
Content string `xorm:"TEXT"`
85-
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
86+
CreatedUnix timeutil.TimeStamp `xorm:"created"`
8687
}
8788

8889
func init() {
8990
db.RegisterModel(new(Action))
9091
}
9192

93+
// TableIndices implements xorm's TableIndices interface
94+
func (a *Action) TableIndices() []*schemas.Index {
95+
actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType)
96+
actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted")
97+
98+
repoIndex := schemas.NewIndex("r_c_u_d", schemas.IndexType)
99+
repoIndex.AddColumn("repo_id", "created_unix", "user_id", "is_deleted")
100+
101+
return []*schemas.Index{actUserIndex, repoIndex}
102+
}
103+
92104
// GetOpType gets the ActionType of this action.
93105
func (a *Action) GetOpType() ActionType {
94106
return a.OpType

models/migrations/migrations.go

+2
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,8 @@ var migrations = []Migration{
387387
NewMigration("Add auto merge table", addAutoMergeTable),
388388
// v215 -> v216
389389
NewMigration("allow to view files in PRs", addReviewViewedFiles),
390+
// v216 -> v217
391+
NewMigration("Improve Action table indices", improveActionTableIndices),
390392
}
391393

392394
// GetCurrentDBVersion returns the current db version

models/migrations/v216.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
"code.gitea.io/gitea/modules/timeutil"
9+
10+
"xorm.io/xorm"
11+
"xorm.io/xorm/schemas"
12+
)
13+
14+
type improveActionTableIndicesAction struct {
15+
ID int64 `xorm:"pk autoincr"`
16+
UserID int64 // Receiver user id.
17+
OpType int
18+
ActUserID int64 // Action user id.
19+
RepoID int64
20+
CommentID int64 `xorm:"INDEX"`
21+
IsDeleted bool `xorm:"NOT NULL DEFAULT false"`
22+
RefName string
23+
IsPrivate bool `xorm:"NOT NULL DEFAULT false"`
24+
Content string `xorm:"TEXT"`
25+
CreatedUnix timeutil.TimeStamp `xorm:"created"`
26+
}
27+
28+
// TableName sets the name of this table
29+
func (a *improveActionTableIndicesAction) TableName() string {
30+
return "action"
31+
}
32+
33+
// TableIndices implements xorm's TableIndices interface
34+
func (a *improveActionTableIndicesAction) TableIndices() []*schemas.Index {
35+
actUserIndex := schemas.NewIndex("au_r_c_u_d", schemas.IndexType)
36+
actUserIndex.AddColumn("act_user_id", "repo_id", "created_unix", "user_id", "is_deleted")
37+
38+
repoIndex := schemas.NewIndex("r_c_u_d", schemas.IndexType)
39+
repoIndex.AddColumn("repo_id", "created_unix", "user_id", "is_deleted")
40+
41+
return []*schemas.Index{actUserIndex, repoIndex}
42+
}
43+
44+
func improveActionTableIndices(x *xorm.Engine) error {
45+
{
46+
type Action struct {
47+
ID int64 `xorm:"pk autoincr"`
48+
UserID int64 `xorm:"INDEX"` // Receiver user id.
49+
OpType int
50+
ActUserID int64 `xorm:"INDEX"` // Action user id.
51+
RepoID int64 `xorm:"INDEX"`
52+
CommentID int64 `xorm:"INDEX"`
53+
IsDeleted bool `xorm:"INDEX NOT NULL DEFAULT false"`
54+
RefName string
55+
IsPrivate bool `xorm:"INDEX NOT NULL DEFAULT false"`
56+
Content string `xorm:"TEXT"`
57+
CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
58+
}
59+
if err := x.Sync2(&Action{}); err != nil {
60+
return err
61+
}
62+
if err := x.DropIndexes(&Action{}); err != nil {
63+
return err
64+
}
65+
}
66+
return x.Sync2(&improveActionTableIndicesAction{})
67+
}

0 commit comments

Comments
 (0)