mirror of
https://github.com/coder/coder.git
synced 2026-06-03 13:08:25 +00:00
e2076beb9f
relates to: https://github.com/coder/internal/issues/1026 On POSIX systems (macOS and Linux) we compile the dbcleaner binary into a temp directory. This allows us to explicitly separate the compilation step and report the time it takes. We suspect this might be a contributing factor in the above linked flakes we see on macOS. This doesn't work on Windows because Go tests clean up the temp directory at the end of the test and the dbcleaner binary will still be executing. On Windows you cannot delete a file being executed nor the directory. However, we are not seeing any flakes on Windows so the old behavior seems to be OK.
91 lines
1.8 KiB
Go
91 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"
|
|
)
|
|
|
|
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 = os.WriteFile(filepath.Join(mainPath, "..", "..", "..", "dump.sql"), append(preamble, dumpBytes...), 0o600)
|
|
if err != nil {
|
|
err = xerrors.Errorf("write dump failed: %w", err)
|
|
panic(err)
|
|
}
|
|
}
|