Files
coder/coderd/database/gen/dump/main.go
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

92 lines
1.8 KiB
Go

package main
import (
"database/sql"
"fmt"
"os"
"path/filepath"
"runtime"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/coderd/database/dbtestutil"
"github.com/coder/coder/v2/coderd/database/migrations"
"github.com/coder/coder/v2/scripts/atomicwrite"
)
var preamble = []byte("-- Code generated by 'make coderd/database/generate'. DO NOT EDIT.")
type mockTB struct {
cleanup []func()
}
func (*mockTB) Name() string {
return "mockTB"
}
func (t *mockTB) Cleanup(f func()) {
t.cleanup = append(t.cleanup, f)
}
func (*mockTB) Helper() {
// noop
}
func (*mockTB) Logf(format string, args ...any) {
_, _ = fmt.Printf(format, args...)
}
func (*mockTB) TempDir() string {
panic("not implemented")
}
func main() {
t := &mockTB{}
defer func() {
for _, f := range t.cleanup {
f()
}
}()
connection := os.Getenv("DB_DUMP_CONNECTION_URL")
if connection == "" {
var cleanup func()
var err error
connection, cleanup, err = dbtestutil.OpenContainerized(t, dbtestutil.DBContainerOptions{})
if err != nil {
err = xerrors.Errorf("open containerized database failed: %w", err)
panic(err)
}
defer cleanup()
}
db, err := sql.Open("postgres", connection)
if err != nil {
err = xerrors.Errorf("open database failed: %w", err)
panic(err)
}
defer db.Close()
err = migrations.Up(db)
if err != nil {
err = xerrors.Errorf("run migrations failed: %w", err)
panic(err)
}
dumpBytes, err := dbtestutil.PGDumpSchemaOnly(connection)
if err != nil {
err = xerrors.Errorf("dump schema failed: %w", err)
panic(err)
}
_, mainPath, _, ok := runtime.Caller(0)
if !ok {
panic("couldn't get caller path")
}
err = atomicwrite.File(filepath.Join(mainPath, "..", "..", "..", "dump.sql"), append(preamble, dumpBytes...))
if err != nil {
err = xerrors.Errorf("write dump failed: %w", err)
panic(err)
}
}