Skip to content

Commit ea7fe2e

Browse files
committed
address PR comments
1 parent fad8705 commit ea7fe2e

File tree

1 file changed

+14
-1
lines changed
  • coderd/database/dbtestutil

1 file changed

+14
-1
lines changed

coderd/database/dbtestutil/db.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"regexp"
1313
"strings"
1414
"testing"
15+
"time"
1516

1617
"github.com/stretchr/testify/require"
1718
"golang.org/x/xerrors"
@@ -127,6 +128,16 @@ func dbNameFromConnectionURL(t testing.TB, connectionURL string) string {
127128
return strings.TrimPrefix(u.Path, "/")
128129
}
129130

131+
// DumpOnFailure exports the database referenced by connectionURL to a file
132+
// corresponding to the current test, with a suffix indicating the time the
133+
// test was run.
134+
// To import this into a new database (assuming you have already run make test-postgres-docker):
135+
// - Create a new test database:
136+
// go run ./scripts/migrate-ci/main.go and note the database name it outputs
137+
// - Import the file into the above database:
138+
// psql 'postgres://postgres:postgres@127.0.0.1:5432/<dbname>?sslmode=disable' -f <path to file.test.sql>
139+
// - Run a dev server against that database:
140+
// ./scripts/coder-dev.sh server --postgres-url='postgres://postgres:postgres@127.0.0.1:5432/<dbname>?sslmode=disable'
130141
func DumpOnFailure(t testing.TB, connectionURL string) {
131142
if !t.Failed() {
132143
return
@@ -137,7 +148,9 @@ func DumpOnFailure(t testing.TB, connectionURL string) {
137148
return
138149
}
139150
snakeCaseName := regexp.MustCompile("[^a-zA-Z0-9-_]+").ReplaceAllString(t.Name(), "_")
140-
outPath := filepath.Join(cwd, snakeCaseName+".test.sql")
151+
now := time.Now()
152+
timeSuffix := fmt.Sprintf("%d%d%d%d%d%d", now.Year(), now.Month(), now.Day(), now.Hour(), now.Minute(), now.Second())
153+
outPath := filepath.Join(cwd, snakeCaseName+"."+timeSuffix+".test.sql")
141154
dump, err := pgDump(connectionURL)
142155
if err != nil {
143156
t.Errorf("dump on failure: failed to run pg_dump")

0 commit comments

Comments
 (0)