mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
71 lines
1.2 KiB
Go
71 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
|
|
"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 (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 main() {
|
|
t := &mockTB{}
|
|
defer func() {
|
|
for _, f := range t.cleanup {
|
|
f()
|
|
}
|
|
}()
|
|
|
|
connection, cleanup, err := dbtestutil.OpenContainerized(t, dbtestutil.DBContainerOptions{})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer cleanup()
|
|
|
|
db, err := sql.Open("postgres", connection)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer db.Close()
|
|
|
|
err = migrations.Up(db)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
dumpBytes, err := dbtestutil.PGDumpSchemaOnly(connection)
|
|
if err != nil {
|
|
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 {
|
|
panic(err)
|
|
}
|
|
}
|