Skip to content

Commit fd52310

Browse files
authored
chore: split queries.sql into files by table (#762)
1 parent 2b1a0ee commit fd52310

27 files changed

+2560
-2514
lines changed

.github/workflows/coder.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ jobs:
8989

9090
- run: go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.26
9191
- run: go install storj.io/drpc/cmd/protoc-gen-go-drpc@v0.0.26
92+
- run: go install golang.org/x/tools/cmd/goimports@latest
9293
- run: "make --output-sync -j gen"
9394
- run: ./scripts/check_unstaged.sh
9495

Makefile

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ coderd/database/dump.sql: $(wildcard coderd/database/migrations/*.sql)
1515
.PHONY: coderd/database/dump.sql
1616

1717
# Generates Go code for querying the database.
18-
coderd/database/generate: fmt/sql coderd/database/dump.sql coderd/database/query.sql
19-
cd coderd/database && sqlc generate && rm db_tmp.go
20-
cd coderd/database && gofmt -w -r 'Querier -> querier' *.go
21-
cd coderd/database && gofmt -w -r 'Queries -> sqlQuerier' *.go
18+
coderd/database/generate: fmt/sql coderd/database/dump.sql $(wildcard coderd/database/queries/*.sql)
19+
coderd/database/generate.sh
2220
.PHONY: coderd/database/generate
2321

2422
fmt/prettier:
@@ -31,13 +29,18 @@ else
3129
endif
3230
.PHONY: fmt/prettier
3331

34-
fmt/sql: ./coderd/database/query.sql
35-
npx sql-formatter \
36-
--language postgresql \
37-
--lines-between-queries 2 \
38-
./coderd/database/query.sql \
39-
--output ./coderd/database/query.sql
40-
sed -i 's/@ /@/g' ./coderd/database/query.sql
32+
fmt/sql: $(wildcard coderd/database/queries/*.sql)
33+
# TODO: this is slightly slow
34+
for fi in coderd/database/queries/*.sql; do \
35+
npx sql-formatter \
36+
--language postgresql \
37+
--lines-between-queries 2 \
38+
--tab-indent \
39+
$$fi \
40+
--output $$fi; \
41+
done
42+
43+
sed -i 's/@ /@/g' ./coderd/database/queries/*.sql
4144

4245
fmt: fmt/prettier fmt/sql
4346
.PHONY: fmt

coderd/database/dump.sql

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

coderd/database/generate.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
3+
# This script turns many *.sql.go files into a single queries.sql.go file. This
4+
# is due to sqlc's behavior when using multiple sql files to output them to
5+
# multiple Go files. We decided it would be cleaner to move these to a single
6+
# file for readability. We should probably contribute the option to do this
7+
# upstream instead, because this is quite janky.
8+
9+
set -euo pipefail
10+
11+
cd "$(dirname "$0")"
12+
13+
sqlc generate
14+
15+
first=true
16+
for fi in queries/*.sql.go; do
17+
# Find the last line from the imports section and add 1.
18+
cut=$(grep -n ')' "$fi" | head -n 1 | cut -d: -f1)
19+
cut=$((cut + 1))
20+
21+
# Copy the header from the first file only, ignoring the source comment.
22+
if $first; then
23+
head -n 4 < "$fi" | grep -v "source" > queries.sql.go
24+
first=false
25+
fi
26+
27+
# Append the file past the imports section into queries.sql.go.
28+
tail -n "+$cut" < "$fi" >> queries.sql.go
29+
done
30+
31+
# Remove temporary go files.
32+
rm -f queries/*.go
33+
34+
# Fix struct/interface names.
35+
gofmt -w -r 'Querier -> querier' -- *.go
36+
gofmt -w -r 'Queries -> sqlQuerier' -- *.go
37+
38+
# Ensure correct imports exist. Modules must all be downloaded so we get correct
39+
# suggestions.
40+
go mod download
41+
goimports -w queries.sql.go

coderd/database/migrations/000002_projects.up.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ CREATE TABLE projects (
3333

3434
-- Enforces no active projects have the same name.
3535
CREATE UNIQUE INDEX ON projects (organization_id, name) WHERE deleted = FALSE;
36+
CREATE UNIQUE INDEX idx_projects_name_lower ON projects USING btree (lower(name));
3637

3738
-- Project Versions store historical project data. When a Project Version is imported,
3839
-- an "import" job is queued to parse parameters. A Project Version

coderd/database/migrations/000003_workspaces.up.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ CREATE TABLE workspaces (
1212
);
1313

1414
-- Enforces no active workspaces have the same name.
15-
CREATE UNIQUE INDEX ON workspaces (owner_id, name) WHERE deleted = FALSE;
15+
CREATE UNIQUE INDEX ON workspaces USING btree (owner_id, name) WHERE deleted = FALSE;
16+
CREATE UNIQUE INDEX idx_workspaces_name_lower ON workspaces USING btree (lower(name));
1617

1718
CREATE TYPE workspace_transition AS ENUM (
1819
'start',

0 commit comments

Comments
 (0)