Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 99e73d8

Browse files
committed
Improve PostgreSQL test speeds
1 parent 90cd045 commit 99e73d8

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

.github/workflows/coder.yaml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,8 @@ jobs:
135135
with:
136136
go-version: "^1.17"
137137

138-
- id: go-cache-paths
138+
- name: Echo Go Cache Paths
139+
id: go-cache-paths
139140
run: |
140141
echo "::set-output name=go-build::$(go env GOCACHE)"
141142
echo "::set-output name=go-mod::$(go env GOMODCACHE)"
@@ -184,6 +185,23 @@ jobs:
184185
GIT_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
185186
run: go run scripts/datadog-cireport/main.go gotests.xml
186187

188+
- name: Start PostgreSQL Database
189+
if: runner.os == 'Linux'
190+
env:
191+
POSTGRES_PASSWORD: postgres
192+
POSTGRES_USER: postgres
193+
POSTGRES_DB: postgres
194+
PGDATA: /tmp
195+
run: |
196+
docker run \
197+
-e POSTGRES_PASSWORD=postgres \
198+
-e POSTGRES_USER=postgres \
199+
-e POSTGRES_DB=postgres \
200+
-e PGDATA=/tmp \
201+
-p 5432:5432 \
202+
-d postgres:11
203+
pg_isready -h 127.0.0.1
204+
187205
- name: Test with PostgreSQL Database
188206
if: runner.os == 'Linux'
189207
run: DB=true gotestsum --junitfile="gotests.xml" --packages="./..." --
@@ -350,7 +368,8 @@ jobs:
350368
with:
351369
install-only: true
352370

353-
- id: go-cache-paths
371+
- name: Echo Go Cache Paths
372+
id: go-cache-paths
354373
run: |
355374
echo "::set-output name=go-build::$(go env GOCACHE)"
356375
echo "::set-output name=go-mod::$(go env GOMODCACHE)"

database/postgres/postgres.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"sync"
1010
"time"
1111

12+
"github.com/coder/coder/cryptorand"
1213
"github.com/ory/dockertest/v3"
1314
"github.com/ory/dockertest/v3/docker"
1415
"golang.org/x/xerrors"
@@ -20,6 +21,28 @@ var openPortMutex sync.Mutex
2021

2122
// Open creates a new PostgreSQL server using a Docker container.
2223
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:[email protected]: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:[email protected]:5432/" + dbName + "?sslmode=disable", func() {}, nil
44+
}
45+
2346
pool, err := dockertest.NewPool("")
2447
if err != nil {
2548
return "", nil, xerrors.Errorf("create pool: %w", err)

0 commit comments

Comments
 (0)