Skip to content

Commit d32e9e8

Browse files
committed
feat: Generate DB unique constraints as enums
This fixes a TODO from #3409.
1 parent c8f8c95 commit d32e9e8

File tree

4 files changed

+149
-9
lines changed

4 files changed

+149
-9
lines changed

coderd/database/errors.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ import (
66
"github.com/lib/pq"
77
)
88

9-
// UniqueConstraint represents a named unique constraint on a table.
10-
type UniqueConstraint string
11-
12-
// UniqueConstraint enums.
13-
// TODO(mafredri): Generate these from the database schema.
14-
const (
15-
UniqueWorkspacesOwnerIDLowerIdx UniqueConstraint = "workspaces_owner_id_lower_idx"
16-
)
17-
189
// IsUniqueViolation checks if the error is due to a unique violation.
1910
// If one or more specific unique constraints are given as arguments,
2011
// the error must be caused by one of them. If no constraints are given,

coderd/database/gen/enum/main.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"fmt"
7+
"os"
8+
"os/exec"
9+
"strings"
10+
11+
"golang.org/x/xerrors"
12+
)
13+
14+
const header = `// Code generated by gen/enum. DO NOT EDIT.
15+
package database
16+
`
17+
18+
func main() {
19+
if err := run(); err != nil {
20+
panic(err)
21+
}
22+
}
23+
24+
func run() error {
25+
dump, err := os.Open("dump.sql")
26+
if err != nil {
27+
fmt.Fprintf(os.Stderr, "error: %s must be run in the database directory with dump.sql present\n", os.Args[0])
28+
return err
29+
}
30+
defer dump.Close()
31+
32+
var uniqueConstraints []string
33+
34+
s := bufio.NewScanner(dump)
35+
query := ""
36+
for s.Scan() {
37+
line := s.Text()
38+
switch {
39+
case strings.HasPrefix(line, "--"):
40+
case line == "":
41+
case strings.HasSuffix(line, ";"):
42+
query += strings.TrimSpace(line)
43+
if isUniqueConstraint(query) {
44+
uniqueConstraints = append(uniqueConstraints, query)
45+
}
46+
query = ""
47+
default:
48+
query += strings.TrimSpace(line) + " "
49+
}
50+
}
51+
if err = s.Err(); err != nil {
52+
return err
53+
}
54+
55+
return writeContents("unique_constraint.go", uniqueConstraints, generateUniqueConstraints)
56+
}
57+
58+
func isUniqueConstraint(query string) bool {
59+
return strings.Contains(query, "UNIQUE")
60+
}
61+
62+
func generateUniqueConstraints(queries []string) ([]byte, error) {
63+
s := &bytes.Buffer{}
64+
65+
_, _ = fmt.Fprint(s, header)
66+
_, _ = fmt.Fprint(s, `
67+
// UniqueConstraint represents a named unique constraint on a table.
68+
type UniqueConstraint string
69+
70+
// UniqueConstraint enums.
71+
const (
72+
`)
73+
for _, query := range queries {
74+
name := ""
75+
switch {
76+
case strings.Contains(query, "ALTER TABLE") && strings.Contains(query, "ADD CONSTRAINT"):
77+
name = strings.Split(query, " ")[6]
78+
case strings.Contains(query, "CREATE UNIQUE INDEX"):
79+
name = strings.Split(query, " ")[3]
80+
default:
81+
return nil, xerrors.Errorf("unknown unique constraint format: %s", query)
82+
}
83+
_, _ = fmt.Fprintf(s, "\tUnique%s UniqueConstraint = %q // %s\n", nameFromSnakeCase(name), name, query)
84+
}
85+
_, _ = fmt.Fprint(s, ")\n")
86+
87+
return s.Bytes(), nil
88+
}
89+
90+
func writeContents[T any](dest string, arg T, fn func(T) ([]byte, error)) error {
91+
b, err := fn(arg)
92+
if err != nil {
93+
return err
94+
}
95+
err = os.WriteFile(dest, b, 0o600)
96+
if err != nil {
97+
return err
98+
}
99+
cmd := exec.Command("goimports", "-w", dest)
100+
return cmd.Run()
101+
}
102+
103+
func nameFromSnakeCase(s string) string {
104+
var ret string
105+
for _, ss := range strings.Split(s, "_") {
106+
switch ss {
107+
case "id":
108+
ret += "ID"
109+
case "ids":
110+
ret += "IDs"
111+
case "jwt":
112+
ret += "JWT"
113+
case "idx":
114+
ret += "Index"
115+
default:
116+
ret += strings.Title(ss)
117+
}
118+
}
119+
return ret
120+
}

coderd/database/generate.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,7 @@ SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
4949
# suggestions.
5050
go mod download
5151
goimports -w queries.sql.go
52+
53+
# Generate enums (e.g. unique constraints).
54+
go run gen/enum/main.go
5255
)

coderd/database/unique_constraint.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy