Skip to content

Commit

Permalink
Fix Postgres Docker flake and update 14.4 -> 14.6 (#4)
Browse files Browse the repository at this point in the history
This PR fixes an issue with our Docker Postgres connection where we sometimes connect before Postgres is fully up, see [1] for more details.

[1] docker-library/postgres#146
  • Loading branch information
bcspragu authored Dec 5, 2022
1 parent 335519c commit d198f78
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions example/golden/raw_dump.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
-- PostgreSQL database dump
--

-- Dumped from database version 14.4 (Debian 14.4-1.pgdg110+1)
-- Dumped by pg_dump version 14.4 (Debian 14.4-1.pgdg110+1)
-- Dumped from database version 14.6 (Debian 14.6-1.pgdg110+1)
-- Dumped by pg_dump version 14.6 (Debian 14.6-1.pgdg110+1)

SET statement_timeout = 0;
SET lock_timeout = 0;
Expand Down
34 changes: 32 additions & 2 deletions testpgx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package testpgx

import (
"bufio"
"context"
"database/sql"
"errors"
Expand All @@ -19,8 +20,8 @@ import (
"github.com/google/uuid"
"github.com/hashicorp/go-multierror"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"

"github.com/jackc/pgx/v4/pgxpool"
pgxstdlib "github.com/jackc/pgx/v4/stdlib"
)

Expand Down Expand Up @@ -78,7 +79,7 @@ const (
testDBUser = "postgres"
testDBPass = "anypassword"

defaultPostgresImage = "postgres:14.4"
defaultPostgresImage = "postgres:14.6"
defaultMaxDBs = 10
)

Expand Down Expand Up @@ -180,6 +181,31 @@ func New(ctx context.Context, opts ...Option) (*Env, error) {
return nil, fmt.Errorf("when getting postgres cid: %w", err)
}

// Postgres starts up twice, first as an initialization phase, and then for
// real. See this thread [1] for more info.
// [1] https://github.com/docker-library/postgres/issues/146
pgLogCtx, pgLogDone := context.WithCancel(ctx)
defer pgLogDone()
pgLogCmd := exec.CommandContext(pgLogCtx, o.dockerBinaryPath, "logs", "-f", cID)
pgLogs, err := pgLogCmd.StdoutPipe()
if err != nil {
return nil, fmt.Errorf("failed to get stdout pipe for docker logs: %w", err)
}
if err := pgLogCmd.Start(); err != nil {
return nil, fmt.Errorf("failed to start docker logs: %w", err)
}
initDone := make(chan struct{})
go func() {
sc := bufio.NewScanner(pgLogs)
for sc.Scan() {
if strings.Contains(sc.Text(), "PostgreSQL init process complete; ready for start up.") {
break
}
}
close(initDone)
pgLogDone()
}()

env := &Env{
postgresCid: cID,
canCreateDB: make(chan struct{}, o.maxDBs),
Expand All @@ -195,6 +221,10 @@ func New(ctx context.Context, opts ...Option) (*Env, error) {
}

err = waitForPostgresToBeReady(ctx, func(ctx context.Context) error {
// Don't try to connect until initialization is done. Postgres starts up twice
// in Docker, the first is just an initialization.
<-initDone

pool, err := pgxpool.Connect(ctx, env.dsn("" /* dbName */))
if err != nil {
return fmt.Errorf("failed to connect to database instance: %w", err)
Expand Down

0 comments on commit d198f78

Please sign in to comment.