Files
coder/coderd/database/generate.sh
T
Mathias Fredriksson a6a8fd94d7 build(Makefile): enable parallel make -j gen with correct dependency graph (#22612)
`make gen` could not run with `-j` because inter-target dependency edges
were missing. Multiple recipes compile `coderd/rbac` (which includes
generated files like `object_gen.go`), and without explicit ordering,
parallel runs produced syntax errors from mid-write reads.

Three main changes:

**Dependency graph fixes** declare the compile-time chain through
`coderd/rbac` so that `object_gen.go` is written before anything that
imports it is compiled. The DB generation targets use a GNU Make 4.3+
grouped target (`&:`) so Make knows `generate.sh` co-produces
`querier.go`, `unique_constraint.go`, `dbmetrics`, and `dbauthz` in a
single invocation. `SKIP_DUMP_SQL=1` avoids re-entrant `make` inside
`generate.sh` when the Makefile already guarantees `dump.sql` is fresh.

**`scripts/atomicwrite` package** replaces `os.WriteFile` in all gen
scripts with a temp-file-in-same-dir + rename pattern, preventing
interrupted runs from leaving partial files.

**`.PRECIOUS` and shell atomic writes** protect git-tracked generated
files from Make's default delete-on-error behavior. Since these files
are committed, deletion is worse than staleness -- `git restore` is the
recovery path.

CI now runs `make -j --output-sync -B gen` (~32s, down from ~85s
serial).

| Scenario                          | Before             | After    |
|-----------------------------------|--------------------|----------|
| `make gen` (serial)               | 95s                | 95s      |
| `make -j gen` (parallel)          | race error         | **22s**  |
| CI `make -j --output-sync -B gen` | forced serial ~85s | **~32s** |
2026-03-05 11:58:10 +00:00

73 lines
2.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# This script turns many *.sql.go files into a single queries.sql.go file. This
# is due to sqlc's behavior when using multiple sql files to output them to
# multiple Go files. We decided it would be cleaner to move these to a single
# file for readability. We should probably contribute the option to do this
# upstream instead, because this is quite janky.
set -euo pipefail
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
(
cd "$SCRIPT_DIR"
echo generate 1>&2
# Dump the updated schema (use make to utilize caching).
if [[ "${SKIP_DUMP_SQL:-0}" != 1 ]]; then
make -C ../.. --no-print-directory coderd/database/dump.sql
fi
# The logic below depends on the exact version being correct :(
sqlc generate
tmpfile=$(mktemp "${TMPDIR:-/tmp}/queries.sql.go.XXXXXX")
trap 'rm -f "$tmpfile"' EXIT
first=true
files=$(find ./queries/ -type f -name "*.sql.go" | LC_ALL=C sort)
for fi in $files; do
# Find the last line from the imports section and add 1. We have to
# disable pipefail temporarily to avoid ERRPIPE errors when piping into
# `head -n1`.
set +o pipefail
cut=$(grep -n ')' "$fi" | head -n 1 | cut -d: -f1)
set -o pipefail
cut=$((cut + 1))
# Copy the header from the first file only, ignoring the source comment.
if $first; then
head -n 6 <"$fi" | grep -v "source" >"$tmpfile"
first=false
fi
# Append the file past the imports section into queries.sql.go.
tail -n "+$cut" <"$fi" >>"$tmpfile"
done
# Atomically replace the target file.
mv "$tmpfile" queries.sql.go
# Move the files we want.
mv queries/querier.go .
mv queries/models.go .
# Remove temporary go files.
rm -f queries/*.go
# Fix struct/interface names.
gofmt -w -r 'Querier -> sqlcQuerier' -- *.go
gofmt -w -r 'Queries -> sqlQuerier' -- *.go
# Ensure correct imports exist. Modules must all be downloaded so we get correct
# suggestions.
go mod download
go tool golang.org/x/tools/cmd/goimports -w queries.sql.go
go run ../../scripts/dbgen
# This will error if a view is broken. This is in it's own package to avoid
# stuff like dbmock breaking builds if it's out of date from the interface.
go test ./gentest
)