9
9
"sync"
10
10
"time"
11
11
12
+ "github.com/coder/coder/cryptorand"
12
13
"github.com/ory/dockertest/v3"
13
14
"github.com/ory/dockertest/v3/docker"
14
15
"golang.org/x/xerrors"
@@ -20,6 +21,28 @@ var openPortMutex sync.Mutex
20
21
21
22
// Open creates a new PostgreSQL server using a Docker container.
22
23
func Open () (string , func (), error ) {
24
+ if os .Getenv ("CI" ) != "" {
25
+ // In CI, creating a Docker container for each test is slow.
26
+ // This expects a PostgreSQL instance with the hardcoded credentials
27
+ // available.
28
+ dbURL := "postgres://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable"
29
+ db , err := sql .Open ("postgres" , dbURL )
30
+ if err != nil {
31
+ return "" , nil , xerrors .Errorf ("connect to ci postgres: %w" , err )
32
+ }
33
+ defer db .Close ()
34
+ dbName , err := cryptorand .StringCharset (cryptorand .Lower , 10 )
35
+ if err != nil {
36
+ return "" , nil , xerrors .Errorf ("generate db name: %w" , err )
37
+ }
38
+ dbName = "ci" + dbName
39
+ _ , err = db .Exec ("CREATE DATABASE " + dbName )
40
+ if err != nil {
41
+ return "" , nil , xerrors .Errorf ("create db: %w" , err )
42
+ }
43
+ return "postgres://postgres:postgres@127.0.0.1:5432/" + dbName + "?sslmode=disable" , func () {}, nil
44
+ }
45
+
23
46
pool , err := dockertest .NewPool ("" )
24
47
if err != nil {
25
48
return "" , nil , xerrors .Errorf ("create pool: %w" , err )
0 commit comments