Skip to content
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
Next Next commit
test: Use a template to prevent migrations from running for every test
  • Loading branch information
kylecarbs committed Jun 21, 2022
commit d57cbd1aba60a4dfb2f78b3de43d1e2167e2c945
20 changes: 17 additions & 3 deletions .github/workflows/coder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -284,17 +284,31 @@ jobs:
-e POSTGRES_DB=postgres \
-e PGDATA=/tmp \
-p 5432:5432 \
-d postgres:11 \
-d postgres:13 \
-c shared_buffers=1GB \
-c max_connections=1000
-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: "make test-postgres"
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 }}

- name: Upload DataDog Trace
if: always() && github.actor != 'dependabot[bot]' && !github.event.pull_request.head.repo.fork
Expand Down
10 changes: 1 addition & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,6 @@ site/src/api/typesGenerated.ts: scripts/apitypings/main.go $(shell find codersdk
test: test-clean
gotestsum -- -v -short ./...

.PHONY: test-postgres
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why drop this make target?

It's important for developers to be able to more-or-less exactly (closer the better) reproduce CI build/test locally so that we can reproduce errors that CI discovers. To that end, you want to keep all the logic in the Makefile or build scripts and then the CI just executes those targets/scripts.

This PR takes us in the opposite direction, removing stuff from make and baking it into the CI.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assumes an external PostgreSQL instance is running, which breaks a pretty fundamental rule of make (which declares explicit chaining dependencies).

I'm fine to move this in a script, but it's not idiomatic to have a make target with external deps.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair. Would be nice to have a .PHONY target that runs a script to ensure the postgres docker container is up, and list it in the dependencies of this target.

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


.PHONY: test-postgres-docker
test-postgres-docker:
docker run \
Expand All @@ -131,7 +123,7 @@ test-postgres-docker:
--name test-postgres-docker \
--restart unless-stopped \
--detach \
postgres:11 \
postgres:13 \
-c shared_buffers=1GB \
-c max_connections=1000

Expand Down
2 changes: 0 additions & 2 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ func NewWithAPI(t *testing.T, options *Options) (*codersdk.Client, *coderd.API)
t.Cleanup(func() {
_ = sqlDB.Close()
})
err = database.MigrateUp(sqlDB)
require.NoError(t, err)
db = database.New(sqlDB)

pubsub, err = database.NewPubsub(context.Background(), sqlDB, connectionURL)
Expand Down
21 changes: 17 additions & 4 deletions coderd/database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/ory/dockertest/v3/docker"
"golang.org/x/xerrors"

"github.com/coder/coder/coderd/database"
"github.com/coder/coder/cryptorand"
)

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

dbName = "ci" + dbName
_, err = db.Exec("CREATE DATABASE " + dbName)
if err != nil {
return "", nil, xerrors.Errorf("create db: %w", err)
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)
}
}

deleteDB := func() {
Expand Down Expand Up @@ -74,7 +87,7 @@ func Open() (string, func(), error) {

resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "postgres",
Tag: "11",
Tag: "13",
Env: []string{
"POSTGRES_PASSWORD=postgres",
"POSTGRES_USER=postgres",
Expand Down
36 changes: 36 additions & 0 deletions scripts/migrate-ci/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"database/sql"
"fmt"

"github.com/coder/coder/coderd/database"
"github.com/coder/coder/cryptorand"
)

func main() {
dbURL := "postgres://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable"
db, err := sql.Open("postgres", dbURL)
if err != nil {
panic(err)
}
defer db.Close()

dbName, err := cryptorand.StringCharset(cryptorand.Lower, 10)
if err != nil {
panic(err)
}

dbName = "ci" + dbName
_, err = db.Exec("CREATE DATABASE " + dbName)
if err != nil {
panic(err)
}

err = database.MigrateUp(db)
if err != nil {
panic(err)
}

_, _ = fmt.Println(dbName)
}