Skip to content

Commit 0bee395

Browse files
committed
db: clean up indexes and add composite for patch list
Add a (project_id, archived, date DESC) composite index on the patch table. The default patch list query filters by project and archived status then sorts by date, so this index covers it without scanning thousands of unrelated rows. Extend the schema index builder to support DESC/ASC suffixes in column specifications via ColumnExpr, since bun's Column() quotes them as identifiers. Drop five indexes that are never used: idx_patch_date (superseded by the new composite), idx_ci_check_context, idx_event_category, idx_series_metadata_key (all subsumed by existing composite or unique indexes), and idx_cover_date_project_id_submitter_id_name (wrong column order, zero scans). Signed-off-by: Robin Jarry <robin@jarry.cc>
1 parent 890d0af commit 0bee395

3 files changed

Lines changed: 114 additions & 8 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Patchwork - automated patch tracking system
2+
// Copyright (C) The Patchwork Contributors (see CONTRIBUTORS)
3+
//
4+
// SPDX-License-Identifier: GPL-2.0-or-later
5+
6+
package migrations
7+
8+
import (
9+
"context"
10+
11+
"github.com/uptrace/bun"
12+
)
13+
14+
func init() {
15+
Register(up0002, down0002)
16+
}
17+
18+
func up0002(ctx context.Context, tx bun.Tx) error {
19+
_, err := tx.NewCreateIndex().
20+
Table("patch").
21+
Index("idx_patch_project_id_archived_date_desc").
22+
ColumnExpr("project_id, archived, date DESC").
23+
IfNotExists().
24+
Exec(ctx)
25+
if err != nil {
26+
return err
27+
}
28+
29+
for _, idx := range []string{
30+
"idx_patch_date",
31+
"idx_ci_check_context",
32+
"idx_event_category",
33+
"idx_series_metadata_key",
34+
"idx_cover_date_project_id_submitter_id_name",
35+
} {
36+
_, err := tx.NewDropIndex().Index(idx).IfExists().Exec(ctx)
37+
if err != nil {
38+
return err
39+
}
40+
}
41+
42+
return nil
43+
}
44+
45+
func down0002(ctx context.Context, tx bun.Tx) error {
46+
_, err := tx.NewDropIndex().
47+
Index("idx_patch_project_id_archived_date_desc").
48+
IfExists().
49+
Exec(ctx)
50+
if err != nil {
51+
return err
52+
}
53+
54+
_, err = tx.NewCreateIndex().
55+
Table("patch").
56+
Index("idx_patch_date").
57+
Column("date").
58+
IfNotExists().
59+
Exec(ctx)
60+
if err != nil {
61+
return err
62+
}
63+
_, err = tx.NewCreateIndex().
64+
Table("ci_check").
65+
Index("idx_ci_check_context").
66+
Column("context").
67+
IfNotExists().
68+
Exec(ctx)
69+
if err != nil {
70+
return err
71+
}
72+
_, err = tx.NewCreateIndex().
73+
Table("event").
74+
Index("idx_event_category").
75+
Column("category").
76+
IfNotExists().
77+
Exec(ctx)
78+
if err != nil {
79+
return err
80+
}
81+
_, err = tx.NewCreateIndex().
82+
Table("series_metadata").
83+
Index("idx_series_metadata_key").
84+
Column("key").
85+
IfNotExists().
86+
Exec(ctx)
87+
if err != nil {
88+
return err
89+
}
90+
_, err = tx.NewCreateIndex().
91+
Table("cover").
92+
Index("idx_cover_date_project_id_submitter_id_name").
93+
Column("date", "project_id", "submitter_id", "name").
94+
IfNotExists().
95+
Exec(ctx)
96+
return err
97+
}

pkg/db/models.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ type SeriesMetadata struct {
173173

174174
ID int `bun:"id,pk,autoincrement"`
175175
SeriesID int `bun:"series_id,notnull" fk:"series.id,cascade"`
176-
Key string `bun:"key,notnull" index:""`
176+
Key string `bun:"key,notnull"`
177177
Value string `bun:"value,notnull"`
178178
}
179179

@@ -207,11 +207,11 @@ type SeriesRef struct {
207207
}
208208

209209
type Patch struct {
210-
bun.BaseModel `bun:"table:patch" unique:"msgid,project_id;series_id,number" index:"archived,state_id,delegate_id,date,project_id,submitter_id,name" json:"-"`
210+
bun.BaseModel `bun:"table:patch" unique:"msgid,project_id;series_id,number" index:"archived,state_id,delegate_id,date,project_id,submitter_id,name;project_id,archived,date DESC" json:"-"`
211211

212212
ID int `bun:"id,pk,autoincrement" json:"id"`
213213
Msgid string `bun:"msgid,notnull" json:"msgid"`
214-
Date time.Time `bun:"date,notnull" json:"date" index:""`
214+
Date time.Time `bun:"date,notnull" json:"date"`
215215
Headers string `bun:"headers,notnull" json:"headers,omitempty"`
216216
SubmitterID int `bun:"submitter_id,notnull" json:"-" fk:"person.id"`
217217
Content *string `bun:"content" json:"content,omitempty"`
@@ -280,7 +280,7 @@ type PatchRelation struct {
280280
}
281281

282282
type Cover struct {
283-
bun.BaseModel `bun:"table:cover" unique:"msgid,project_id" index:"date,project_id,submitter_id,name" json:"-"`
283+
bun.BaseModel `bun:"table:cover" unique:"msgid,project_id" json:"-"`
284284

285285
ID int `bun:"id,pk,autoincrement" json:"id"`
286286
Msgid string `bun:"msgid,notnull" json:"msgid"`
@@ -356,7 +356,7 @@ type Check struct {
356356
Date time.Time `bun:"date,notnull" json:"date"`
357357
State CheckState `bun:"state,notnull" json:"state"`
358358
TargetURL string `bun:"target_url,notnull" json:"target_url"`
359-
Context string `bun:"context,notnull" json:"context" index:""`
359+
Context string `bun:"context,notnull" json:"context"`
360360
Description string `bun:"description,notnull" json:"description"`
361361

362362
User *User `bun:"rel:belongs-to,join:user_id=id" json:"user,omitempty"`
@@ -406,7 +406,7 @@ type Event struct {
406406

407407
ID int `bun:"id,pk,autoincrement" json:"id"`
408408
ProjectID int `bun:"project_id,notnull" json:"-" fk:"project.id,cascade"`
409-
Category string `bun:"category,notnull" json:"category" index:""`
409+
Category string `bun:"category,notnull" json:"category"`
410410
Date time.Time `bun:"date,notnull" json:"date"`
411411
ActorID *int `bun:"actor_id" json:"-" fk:"auth_user.id,setnull"`
412412
PatchID *int `bun:"patch_id" json:"-" fk:"patch.id,cascade"`

pkg/db/schema.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type tableIndex struct {
4848
}
4949

5050
func (idx *tableIndex) create(model any, ctx context.Context, database bun.IDB) error {
51-
c := database.NewCreateIndex().Model(model).Column(idx.columns...)
51+
c := database.NewCreateIndex().Model(model)
5252
var tokens []string
5353
if idx.unique {
5454
c = c.Unique()
@@ -57,7 +57,16 @@ func (idx *tableIndex) create(model any, ctx context.Context, database bun.IDB)
5757
tokens = append(tokens, "idx")
5858
}
5959
tokens = append(tokens, c.GetTableName())
60-
tokens = append(tokens, idx.columns...)
60+
for _, col := range idx.columns {
61+
upper := strings.ToUpper(col)
62+
if strings.HasSuffix(upper, " DESC") || strings.HasSuffix(upper, " ASC") {
63+
c = c.ColumnExpr(col)
64+
} else {
65+
c = c.Column(col)
66+
}
67+
name := strings.ReplaceAll(strings.ToLower(col), " ", "_")
68+
tokens = append(tokens, name)
69+
}
6170
name := strings.Join(tokens, "_")
6271
_, err := c.Index(name).Exec(ctx)
6372
return err

0 commit comments

Comments
 (0)