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

Skip to content

Commit 6f05b7d

Browse files
committed
Create a single makefile target
1 parent 5ca6beb commit 6f05b7d

File tree

4 files changed

+22
-58
lines changed

4 files changed

+22
-58
lines changed

.github/workflows/coder.yaml

+1-37
Original file line numberDiff line numberDiff line change
@@ -267,44 +267,8 @@ jobs:
267267
terraform_version: 1.1.9
268268
terraform_wrapper: false
269269

270-
- name: Start PostgreSQL Database
271-
env:
272-
POSTGRES_PASSWORD: postgres
273-
POSTGRES_USER: postgres
274-
POSTGRES_DB: postgres
275-
PGDATA: /tmp
276-
run: |
277-
docker run \
278-
-e POSTGRES_PASSWORD=postgres \
279-
-e POSTGRES_USER=postgres \
280-
-e POSTGRES_DB=postgres \
281-
-e PGDATA=/tmp \
282-
-p 5432:5432 \
283-
-d postgres:13 \
284-
-c shared_buffers=1GB \
285-
-c max_connections=1000 \
286-
-c fsync=false
287-
while ! pg_isready -h 127.0.0.1
288-
do
289-
echo "$(date) - waiting for database to start"
290-
sleep 0.5
291-
done
292-
293-
- name: Create Migration Template
294-
id: migrated
295-
run: |
296-
DB_FROM=$(go run scripts/migrate-ci/main.go)
297-
echo "::set-output name=db-from::$DB_FROM"
298-
echo $DB_FROM
299-
300270
- name: Test with PostgreSQL Database
301-
run: |
302-
gotestsum --junitfile="gotests.xml" --packages="./..." -- \
303-
-covermode=atomic -coverprofile="gotests.coverage" -timeout=30m \
304-
-coverpkg=./...,github.com/coder/coder/codersdk \
305-
-count=2 -race -failfast
306-
env:
307-
DB_FROM: ${{ steps.migrated.outputs.db-from }}
271+
run: make test-postgres
308272

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

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ node_modules
1313
vendor
1414
.eslintcache
1515
yarn-error.log
16+
gotests.xml
17+
gotests.coverage
1618
.idea
1719
.DS_Store
1820

Makefile

+11-5
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,15 @@ test: test-clean
171171
gotestsum -- -v -short ./...
172172
.PHONY: test
173173

174-
test-postgres: test-clean
175-
DB=ci gotestsum --junitfile="gotests.xml" --packages="./..." -- \
176-
-covermode=atomic -coverprofile="gotests.coverage" -timeout=30m \
177-
-coverpkg=./...,github.com/coder/coder/codersdk \
178-
-count=1 -race -failfast
174+
test-postgres: test-clean test-postgres-docker
175+
DB_FROM=$(shell go run scripts/migrate-ci/main.go) gotestsum --junitfile="gotests.xml" --packages="./..." -- \
176+
-covermode=atomic -coverprofile="gotests.coverage" -timeout=30m \
177+
-coverpkg=./...,github.com/coder/coder/codersdk \
178+
-count=2 -race -failfast
179179
.PHONY: test-postgres
180180

181181
test-postgres-docker:
182+
docker rm -f test-postgres-docker || true
182183
docker run \
183184
--env POSTGRES_PASSWORD=postgres \
184185
--env POSTGRES_USER=postgres \
@@ -195,6 +196,11 @@ test-postgres-docker:
195196
-c fsync=off \
196197
-c synchronous_commit=off \
197198
-c full_page_writes=off
199+
while ! pg_isready -h 127.0.0.1
200+
do
201+
echo "$(date) - waiting for database to start"
202+
sleep 0.5
203+
done
198204
.PHONY: test-postgres-docker
199205

200206
test-clean:

coderd/database/postgres/postgres.go

+8-16
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var openPortMutex sync.Mutex
2323

2424
// Open creates a new PostgreSQL server using a Docker container.
2525
func Open() (string, func(), error) {
26-
if os.Getenv("DB") == "ci" {
26+
if os.Getenv("DB_FROM") != "" {
2727
// In CI, creating a Docker container for each test is slow.
2828
// This expects a PostgreSQL instance with the hardcoded credentials
2929
// available.
@@ -40,21 +40,9 @@ func Open() (string, func(), error) {
4040
}
4141

4242
dbName = "ci" + dbName
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-
}
43+
_, err = db.Exec("CREATE DATABASE " + dbName + " WITH TEMPLATE " + os.Getenv("DB_FROM"))
44+
if err != nil {
45+
return "", nil, xerrors.Errorf("create db with template: %w", err)
5846
}
5947

6048
deleteDB := func() {
@@ -146,6 +134,10 @@ func Open() (string, func(), error) {
146134
if err != nil {
147135
return xerrors.Errorf("ping postgres: %w", err)
148136
}
137+
err = database.MigrateUp(db)
138+
if err != nil {
139+
return xerrors.Errorf("migrate db: %w", err)
140+
}
149141

150142
return nil
151143
})

0 commit comments

Comments
 (0)