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

Skip to content

Commit d57cbd1

Browse files
committed
test: Use a template to prevent migrations from running for every test
1 parent 7cce7a9 commit d57cbd1

File tree

5 files changed

+71
-18
lines changed

5 files changed

+71
-18
lines changed

.github/workflows/coder.yaml

+17-3
Original file line numberDiff line numberDiff line change
@@ -284,17 +284,31 @@ jobs:
284284
-e POSTGRES_DB=postgres \
285285
-e PGDATA=/tmp \
286286
-p 5432:5432 \
287-
-d postgres:11 \
287+
-d postgres:13 \
288288
-c shared_buffers=1GB \
289-
-c max_connections=1000
289+
-c max_connections=1000 \
290+
-c fsync=false
290291
while ! pg_isready -h 127.0.0.1
291292
do
292293
echo "$(date) - waiting for database to start"
293294
sleep 0.5
294295
done
295296
297+
- name: Create Migration Template
298+
id: migrated
299+
run: |
300+
DB_FROM=$(go run scripts/migrate-ci/main.go)
301+
echo "::set-output name=db-from::$DB_FROM"
302+
echo $DB_FROM
303+
296304
- name: Test with PostgreSQL Database
297-
run: "make test-postgres"
305+
run: |
306+
gotestsum --junitfile="gotests.xml" --packages="./..." -- \
307+
-covermode=atomic -coverprofile="gotests.coverage" -timeout=30m \
308+
-coverpkg=./...,github.com/coder/coder/codersdk \
309+
-count=2 -race -failfast
310+
env:
311+
DB_FROM: ${{ steps.migrated.outputs.db-from }}
298312

299313
- name: Upload DataDog Trace
300314
if: always() && github.actor != 'dependabot[bot]' && !github.event.pull_request.head.repo.fork

Makefile

+1-9
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,6 @@ site/src/api/typesGenerated.ts: scripts/apitypings/main.go $(shell find codersdk
112112
test: test-clean
113113
gotestsum -- -v -short ./...
114114

115-
.PHONY: test-postgres
116-
test-postgres: test-clean
117-
DB=ci gotestsum --junitfile="gotests.xml" --packages="./..." -- \
118-
-covermode=atomic -coverprofile="gotests.coverage" -timeout=30m \
119-
-coverpkg=./...,github.com/coder/coder/codersdk \
120-
-count=1 -race -failfast
121-
122-
123115
.PHONY: test-postgres-docker
124116
test-postgres-docker:
125117
docker run \
@@ -131,7 +123,7 @@ test-postgres-docker:
131123
--name test-postgres-docker \
132124
--restart unless-stopped \
133125
--detach \
134-
postgres:11 \
126+
postgres:13 \
135127
-c shared_buffers=1GB \
136128
-c max_connections=1000
137129

coderd/coderdtest/coderdtest.go

-2
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ func NewWithAPI(t *testing.T, options *Options) (*codersdk.Client, *coderd.API)
115115
t.Cleanup(func() {
116116
_ = sqlDB.Close()
117117
})
118-
err = database.MigrateUp(sqlDB)
119-
require.NoError(t, err)
120118
db = database.New(sqlDB)
121119

122120
pubsub, err = database.NewPubsub(context.Background(), sqlDB, connectionURL)

coderd/database/postgres/postgres.go

+17-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/ory/dockertest/v3/docker"
1414
"golang.org/x/xerrors"
1515

16+
"github.com/coder/coder/coderd/database"
1617
"github.com/coder/coder/cryptorand"
1718
)
1819

@@ -39,9 +40,21 @@ func Open() (string, func(), error) {
3940
}
4041

4142
dbName = "ci" + dbName
42-
_, err = db.Exec("CREATE DATABASE " + dbName)
43-
if err != nil {
44-
return "", nil, xerrors.Errorf("create db: %w", err)
43+
if os.Getenv("DB_FROM") == "" {
44+
_, err = db.Exec("CREATE DATABASE " + dbName)
45+
if err != nil {
46+
return "", nil, xerrors.Errorf("create db: %w", err)
47+
}
48+
49+
err = database.MigrateUp(db)
50+
if err != nil {
51+
return "", nil, xerrors.Errorf("migrate db: %w", err)
52+
}
53+
} else {
54+
_, err = db.Exec("CREATE DATABASE " + dbName + " WITH TEMPLATE " + os.Getenv("DB_FROM"))
55+
if err != nil {
56+
return "", nil, xerrors.Errorf("create db with template: %w", err)
57+
}
4558
}
4659

4760
deleteDB := func() {
@@ -74,7 +87,7 @@ func Open() (string, func(), error) {
7487

7588
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
7689
Repository: "postgres",
77-
Tag: "11",
90+
Tag: "13",
7891
Env: []string{
7992
"POSTGRES_PASSWORD=postgres",
8093
"POSTGRES_USER=postgres",

scripts/migrate-ci/main.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"fmt"
6+
7+
"github.com/coder/coder/coderd/database"
8+
"github.com/coder/coder/cryptorand"
9+
)
10+
11+
func main() {
12+
dbURL := "postgres://postgres:[email protected]:5432/postgres?sslmode=disable"
13+
db, err := sql.Open("postgres", dbURL)
14+
if err != nil {
15+
panic(err)
16+
}
17+
defer db.Close()
18+
19+
dbName, err := cryptorand.StringCharset(cryptorand.Lower, 10)
20+
if err != nil {
21+
panic(err)
22+
}
23+
24+
dbName = "ci" + dbName
25+
_, err = db.Exec("CREATE DATABASE " + dbName)
26+
if err != nil {
27+
panic(err)
28+
}
29+
30+
err = database.MigrateUp(db)
31+
if err != nil {
32+
panic(err)
33+
}
34+
35+
_, _ = fmt.Println(dbName)
36+
}

0 commit comments

Comments
 (0)