Skip to content

test: Use a template to prevent migrations from running for every test #2462

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create a single makefile target
  • Loading branch information
kylecarbs committed Jun 26, 2022
commit 6f05b7d1edc1f16f989c75246aa741d860448f53
38 changes: 1 addition & 37 deletions .github/workflows/coder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -267,44 +267,8 @@ jobs:
terraform_version: 1.1.9
terraform_wrapper: false

- name: Start PostgreSQL Database
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: postgres
PGDATA: /tmp
run: |
docker run \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_USER=postgres \
-e POSTGRES_DB=postgres \
-e PGDATA=/tmp \
-p 5432:5432 \
-d postgres:13 \
-c shared_buffers=1GB \
-c max_connections=1000 \
-c fsync=false
while ! pg_isready -h 127.0.0.1
do
echo "$(date) - waiting for database to start"
sleep 0.5
done

- name: Create Migration Template
id: migrated
run: |
DB_FROM=$(go run scripts/migrate-ci/main.go)
echo "::set-output name=db-from::$DB_FROM"
echo $DB_FROM

- name: Test with PostgreSQL Database
run: |
gotestsum --junitfile="gotests.xml" --packages="./..." -- \
-covermode=atomic -coverprofile="gotests.coverage" -timeout=30m \
-coverpkg=./...,github.com/coder/coder/codersdk \
-count=2 -race -failfast
env:
DB_FROM: ${{ steps.migrated.outputs.db-from }}
run: make test-postgres

- name: Upload DataDog Trace
if: always() && github.actor != 'dependabot[bot]' && !github.event.pull_request.head.repo.fork
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ node_modules
vendor
.eslintcache
yarn-error.log
gotests.xml
gotests.coverage
.idea
.DS_Store

Expand Down
16 changes: 11 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,15 @@ test: test-clean
gotestsum -- -v -short ./...
.PHONY: test

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

test-postgres-docker:
docker rm -f test-postgres-docker || true
docker run \
--env POSTGRES_PASSWORD=postgres \
--env POSTGRES_USER=postgres \
Expand All @@ -195,6 +196,11 @@ test-postgres-docker:
-c fsync=off \
-c synchronous_commit=off \
-c full_page_writes=off
while ! pg_isready -h 127.0.0.1
do
echo "$(date) - waiting for database to start"
sleep 0.5
done
.PHONY: test-postgres-docker

test-clean:
Expand Down
24 changes: 8 additions & 16 deletions coderd/database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var openPortMutex sync.Mutex

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

dbName = "ci" + dbName
if os.Getenv("DB_FROM") == "" {
_, err = db.Exec("CREATE DATABASE " + dbName)
if err != nil {
return "", nil, xerrors.Errorf("create db: %w", err)
}

err = database.MigrateUp(db)
if err != nil {
return "", nil, xerrors.Errorf("migrate db: %w", err)
}
} else {
_, err = db.Exec("CREATE DATABASE " + dbName + " WITH TEMPLATE " + os.Getenv("DB_FROM"))
if err != nil {
return "", nil, xerrors.Errorf("create db with template: %w", err)
}
_, err = db.Exec("CREATE DATABASE " + dbName + " WITH TEMPLATE " + os.Getenv("DB_FROM"))
if err != nil {
return "", nil, xerrors.Errorf("create db with template: %w", err)
}

deleteDB := func() {
Expand Down Expand Up @@ -146,6 +134,10 @@ func Open() (string, func(), error) {
if err != nil {
return xerrors.Errorf("ping postgres: %w", err)
}
err = database.MigrateUp(db)
if err != nil {
return xerrors.Errorf("migrate db: %w", err)
}

return nil
})
Expand Down