Skip to content

Commit 2131fa2

Browse files
feat: add missing fk indexes for owner_id, edges (#2669)
Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com>
1 parent 5a589bb commit 2131fa2

20 files changed

Lines changed: 6595 additions & 2394 deletions

.task/checksum/generate-ent-smart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8f951e1a0020299ee9450a72fdfa0456
1+
89fc2bb10974a78331af25af3e6356b2
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f0f7821ffee1b805d010e873c4106a75
1+
8f91ac57f215f32a3ca106419a6acb64
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
673005be2e9496ae3902b1e0f281124f
1+
750124ab9bc37d5c92fe13f7f73811cc

db/migrations-goose-postgres/20260807044755_indexes.sql

Lines changed: 2091 additions & 0 deletions
Large diffs are not rendered by default.

db/migrations-goose-postgres/atlas.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
h1:ZcsB4l/WXn0+/QcEGCFrW6G/1mcFCEGId4QUixLvCtg=
1+
h1:jLsTB4giCB9Aim1vYqnT0i+A+VHa+4j68szQMtBkyKQ=
22
20251229183203_init.sql h1:g/05irCXoqKFObJV2PPLuvDRqma+da41VPsZ5fXQgfE=
33
20251229183205_init_history.sql h1:P/q2kAHb8zfNMOWzYkKtPbOwoh9GdJUQljSNAiPEjYI=
44
20251231045229_workflow.sql h1:uUPRLO4yw2y//fPGeqIXRrL+fiEN1TRGkF4wHiqPaPk=
@@ -175,3 +175,4 @@ h1:ZcsB4l/WXn0+/QcEGCFrW6G/1mcFCEGId4QUixLvCtg=
175175
20260806055955_drop_integration_history_tables.sql h1:iF8wKuPtK4tAqxhVpBdC6407ptkgOUnc46azCkr9z/Y=
176176
20260806102628_tfa_enforcement.sql h1:OfHMmkxbWl9RonXcnx3MunsruZ6vwp8872QAhERMWL8=
177177
20260806102636_tfa_enforcement_history.sql h1:0NNHJpOCxJ5qlMtaItDEYlLgQAmtpDueL+noHLc+et0=
178+
20260807044755_indexes.sql h1:OBcWxQjR4rC5RqjFJuKaWsat7KaZrRCRN0sRzryJdwQ=

db/migrations/20260807044745_indexes.sql

Lines changed: 1044 additions & 0 deletions
Large diffs are not rendered by default.

db/migrations/atlas.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
h1:qDkwYcCwccihiwBlHCESVxoH6O59yShr/zPNLhR06c4=
1+
h1:ZRtx8ExSIwMmpoMwjuY/XrS/zeB24Dtv4xneXGSfxvg=
22
20251229183159_init.sql h1:3uf/ftr28zW8YUD9qAaY2GESQHC7pYmkhlI6oDWUCKU=
33
20251229183201_init_history.sql h1:1tFSeCDWvZgb2Ctw80C/s3tqVPNLADQw9fSDACzL8WA=
44
20251231045221_workflow.sql h1:5bLq4cHh2kxUV7xajK5bq5McKKpr88HmufYauTdsgUw=
@@ -175,3 +175,4 @@ h1:qDkwYcCwccihiwBlHCESVxoH6O59yShr/zPNLhR06c4=
175175
20260806055946_drop_integration_history_tables.sql h1:WpsMuHDxGn3t4hAepDLBUePTYZBgh0P/cx/DgRQCj1Q=
176176
20260806102606_tfa_enforcement.sql h1:iKnXbvc7CxEr0FU7GxJ6Lq7Ah92sIaytO+FP053Lybo=
177177
20260806102616_tfa_enforcement_history.sql h1:lYstxxJz5bNKGS3M5+WaULQnGWLpYSDWhLK2YgbeF10=
178+
20260807044745_indexes.sql h1:V4MfAk/UWsmop4oCtc2k+N8PO0X2Wnhzxp8FA/MuFkA=
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
001a87c385f673386bb8cd60ec25c1b4a6bc53b30912f6ecb2eef46289b08d62
1+
4e205f0c4e424a0b8da12cb559d7d727a0bc35b65d68075e53a57f3d23feb3ee
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ac6af5b3c4a1d30cefe12c7f70dc5f2ec7cce30d4e4d3cf78c3c6ba6b9461e3d
1+
d80531a7ed0cf28417e6ff4aa9944530d61071832344883fcc848617fa5a1a7c
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{{/* Generate indexes for the trailing column of every m2m join table */}}
2+
{{/* gotype: entgo.io/ent/entc/gen.Graph */}}
3+
4+
{{ define "join_table_indexes" }}
5+
6+
{{/* Add the base header for the generated file */}}
7+
{{ $pkg := base $.Config.Package }}
8+
{{ template "header" $ }}
9+
10+
import (
11+
"entgo.io/ent/dialect/sql/schema"
12+
13+
"{{ $.Config.Package }}/migrate"
14+
)
15+
16+
// maxJoinIndexNameLen is the maximum length of an identifier in postgres, longer names are
17+
// silently truncated by the server which would make the generated migration diff unstable
18+
const maxJoinIndexNameLen = 63
19+
20+
// joinTables are the m2m join tables ent generates from the edges in the schema
21+
// ent has no schema level api for indexing them, so they are amended below instead
22+
var joinTables = map[string]bool{
23+
{{- range $t := $.Tables }}
24+
{{- if and (eq (len $t.Columns) 2) (eq (len $t.PrimaryKey) 2) }}
25+
"{{ $t.Name }}": true,
26+
{{- end }}
27+
{{- end }}
28+
}
29+
30+
// init adds an index on the trailing column of every m2m join table
31+
// the leading column is already covered by the composite primary key, but the trailing one is
32+
// not, so enforcing its foreign key requires a sequential scan of the join table whenever a
33+
// referenced row is deleted, and traversing the edge in reverse scans it as well
34+
//
35+
// this runs for every consumer of the tables, both the auto migration and the versioned
36+
// migration diff, because they share this package
37+
func init() {
38+
for _, t := range migrate.Tables {
39+
if !joinTables[t.Name] {
40+
continue
41+
}
42+
43+
col := t.PrimaryKey[1]
44+
45+
name := t.Name + "_" + col.Name + "_idx"
46+
if len(name) > maxJoinIndexNameLen {
47+
name = name[:maxJoinIndexNameLen]
48+
}
49+
50+
t.Indexes = append(t.Indexes, &schema.Index{
51+
Name: name,
52+
Columns: []*schema.Column{col},
53+
})
54+
}
55+
}
56+
{{ end }}

0 commit comments

Comments
 (0)