@@ -12,6 +12,7 @@ import (
12
12
"regexp"
13
13
"strings"
14
14
"testing"
15
+ "time"
15
16
16
17
"github.com/stretchr/testify/require"
17
18
"golang.org/x/xerrors"
@@ -127,6 +128,16 @@ func dbNameFromConnectionURL(t testing.TB, connectionURL string) string {
127
128
return strings .TrimPrefix (u .Path , "/" )
128
129
}
129
130
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'
130
141
func DumpOnFailure (t testing.TB , connectionURL string ) {
131
142
if ! t .Failed () {
132
143
return
@@ -137,7 +148,9 @@ func DumpOnFailure(t testing.TB, connectionURL string) {
137
148
return
138
149
}
139
150
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" )
141
154
dump , err := pgDump (connectionURL )
142
155
if err != nil {
143
156
t .Errorf ("dump on failure: failed to run pg_dump" )
0 commit comments