Skip to content
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

Fix Postgres Docker flake and update 14.4 -> 14.6 #4

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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