Skip to content

Commit

Permalink
ci: Run PostgreSQL with a scratch directory to improve CI durability (#…
Browse files Browse the repository at this point in the history
…89)

When using parallel before, multiple PostgreSQL containers would
unintentionally interfere with the other's data. This ensures
both containers have separated data, and don't create a volume.

🌮 @bryphe-coder for the idea!
  • Loading branch information
kylecarbs committed Jan 30, 2022
1 parent 2b922b1 commit f9e594f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/coder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ jobs:
run:
DB=true gotestsum --jsonfile="gotests.json" --packages="./..." --
-covermode=atomic -coverprofile="gotests.coverage" -timeout=3m
-count=1 -race -parallel=1
-count=1 -race -parallel=2

- uses: codecov/codecov-action@v2
with:
Expand Down
17 changes: 17 additions & 0 deletions database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package postgres
import (
"database/sql"
"fmt"
"io/ioutil"
"os"
"time"

"github.com/ory/dockertest/v3"
Expand All @@ -16,15 +18,29 @@ func Open() (string, func(), error) {
if err != nil {
return "", nil, xerrors.Errorf("create pool: %w", err)
}
tempDir, err := ioutil.TempDir(os.TempDir(), "postgres")
if err != nil {
return "", nil, xerrors.Errorf("create tempdir: %w", err)
}
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "postgres",
Tag: "11",
Env: []string{
"POSTGRES_PASSWORD=postgres",
"POSTGRES_USER=postgres",
"POSTGRES_DB=postgres",
// The location for temporary database files!
"PGDATA=/tmp",
"listen_addresses = '*'",
},
Mounts: []string{
// The postgres image has a VOLUME parameter in it's image.
// If we don't mount at this point, Docker will allocate a
// volume for this directory.
//
// This isn't used anyways, since we override PGDATA.
fmt.Sprintf("%s:/var/lib/postgresql/data", tempDir),
},
}, func(config *docker.HostConfig) {
// set AutoRemove to true so that stopped container goes away by itself
config.AutoRemove = true
Expand Down Expand Up @@ -57,5 +73,6 @@ func Open() (string, func(), error) {
}
return dbURL, func() {
_ = pool.Purge(resource)
_ = os.RemoveAll(tempDir)
}, nil
}

0 comments on commit f9e594f

Please sign in to comment.