2022-01-05 09:20:56 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"database/sql"
|
2024-11-04 17:23:31 +01:00
|
|
|
"fmt"
|
2022-01-05 09:20:56 -06:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"runtime"
|
|
|
|
|
|
2025-01-23 18:53:24 +02:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
2024-03-21 14:53:16 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/database/dbtestutil"
|
2023-08-18 13:55:43 -05:00
|
|
|
"github.com/coder/coder/v2/coderd/database/migrations"
|
2026-03-05 13:58:10 +02:00
|
|
|
"github.com/coder/coder/v2/scripts/atomicwrite"
|
2022-01-05 09:20:56 -06:00
|
|
|
)
|
|
|
|
|
|
2024-04-23 12:43:14 +01:00
|
|
|
var preamble = []byte("-- Code generated by 'make coderd/database/generate'. DO NOT EDIT.")
|
2022-11-10 12:54:47 +02:00
|
|
|
|
2024-11-04 17:23:31 +01:00
|
|
|
type mockTB struct {
|
|
|
|
|
cleanup []func()
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-15 11:25:20 +02:00
|
|
|
func (*mockTB) Name() string {
|
|
|
|
|
return "mockTB"
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-04 17:23:31 +01:00
|
|
|
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...)
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-08 16:18:02 +04:00
|
|
|
func (*mockTB) TempDir() string {
|
|
|
|
|
panic("not implemented")
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 09:20:56 -06:00
|
|
|
func main() {
|
2024-11-04 17:23:31 +01:00
|
|
|
t := &mockTB{}
|
|
|
|
|
defer func() {
|
|
|
|
|
for _, f := range t.cleanup {
|
|
|
|
|
f()
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2025-01-23 18:53:24 +02:00
|
|
|
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()
|
2022-01-05 09:20:56 -06:00
|
|
|
}
|
2022-04-01 14:42:36 -05:00
|
|
|
|
2022-01-05 09:20:56 -06:00
|
|
|
db, err := sql.Open("postgres", connection)
|
|
|
|
|
if err != nil {
|
2025-01-23 18:53:24 +02:00
|
|
|
err = xerrors.Errorf("open database failed: %w", err)
|
2022-01-05 09:20:56 -06:00
|
|
|
panic(err)
|
|
|
|
|
}
|
2024-04-23 12:43:14 +01:00
|
|
|
defer db.Close()
|
2022-04-01 14:42:36 -05:00
|
|
|
|
2022-09-20 03:39:02 +10:00
|
|
|
err = migrations.Up(db)
|
2022-01-05 09:20:56 -06:00
|
|
|
if err != nil {
|
2025-01-23 18:53:24 +02:00
|
|
|
err = xerrors.Errorf("run migrations failed: %w", err)
|
2022-01-05 09:20:56 -06:00
|
|
|
panic(err)
|
|
|
|
|
}
|
2022-04-01 14:42:36 -05:00
|
|
|
|
2024-04-23 12:43:14 +01:00
|
|
|
dumpBytes, err := dbtestutil.PGDumpSchemaOnly(connection)
|
2022-01-05 09:20:56 -06:00
|
|
|
if err != nil {
|
2025-01-23 18:53:24 +02:00
|
|
|
err = xerrors.Errorf("dump schema failed: %w", err)
|
2022-01-05 09:20:56 -06:00
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, mainPath, _, ok := runtime.Caller(0)
|
|
|
|
|
if !ok {
|
|
|
|
|
panic("couldn't get caller path")
|
|
|
|
|
}
|
2026-03-05 13:58:10 +02:00
|
|
|
err = atomicwrite.File(filepath.Join(mainPath, "..", "..", "..", "dump.sql"), append(preamble, dumpBytes...))
|
2022-01-05 09:20:56 -06:00
|
|
|
if err != nil {
|
2025-01-23 18:53:24 +02:00
|
|
|
err = xerrors.Errorf("write dump failed: %w", err)
|
2022-01-05 09:20:56 -06:00
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
}
|