From f9e594fbada4c5f6f4ead56ccdba93100a2705e7 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Sat, 29 Jan 2022 18:39:59 -0600 Subject: [PATCH] ci: Run PostgreSQL with a scratch directory to improve CI durability (#89) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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! --- .github/workflows/coder.yaml | 2 +- database/postgres/postgres.go | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/workflows/coder.yaml b/.github/workflows/coder.yaml index 7e0b890eda..16d265f93e 100644 --- a/.github/workflows/coder.yaml +++ b/.github/workflows/coder.yaml @@ -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: diff --git a/database/postgres/postgres.go b/database/postgres/postgres.go index 3b99297998..d022cd6669 100644 --- a/database/postgres/postgres.go +++ b/database/postgres/postgres.go @@ -3,6 +3,8 @@ package postgres import ( "database/sql" "fmt" + "io/ioutil" + "os" "time" "github.com/ory/dockertest/v3" @@ -16,6 +18,10 @@ 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", @@ -23,8 +29,18 @@ func Open() (string, func(), error) { "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 @@ -57,5 +73,6 @@ func Open() (string, func(), error) { } return dbURL, func() { _ = pool.Purge(resource) + _ = os.RemoveAll(tempDir) }, nil }