internal/infrastructure/postgres opens a pgx pool, checks server_version_num >= 180000, and applies goose migrations with a Postgres session advisory lock so processes starting together apply each migration once. internal/infrastructure/postgres/pgtest starts postgres:18 through testcontainers per test binary and gives each test its own database. Adds jackc/pgx/v5 v5.11.0, pressly/goose/v3 v3.28.0 and testcontainers-go v0.44.0. The database URL config, sqlc, the Compose service and the ownership lock move to archie-core-7r0x.2, where they have consumers.
88 lines
2.3 KiB
Go
88 lines
2.3 KiB
Go
// Package pgtest runs a throwaway PostgreSQL 18 container for one test binary
|
|
// and gives each test its own database on it.
|
|
//
|
|
// A package using it calls Main from TestMain:
|
|
//
|
|
// func TestMain(m *testing.M) { os.Exit(pgtest.Main(m)) }
|
|
//
|
|
// It needs a running Docker daemon.
|
|
package pgtest
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/testcontainers/testcontainers-go"
|
|
tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres"
|
|
)
|
|
|
|
// image is the server tests run against: the same major version archie
|
|
// requires.
|
|
const image = "postgres:18"
|
|
|
|
var (
|
|
adminURL string
|
|
nextDB atomic.Int64
|
|
)
|
|
|
|
// Main starts the container, runs the tests and removes the container,
|
|
// returning the exit code. A container that cannot start is a test failure,
|
|
// not a skip.
|
|
func Main(m *testing.M) int {
|
|
ctx := context.Background()
|
|
container, err := tcpostgres.Run(ctx, image,
|
|
tcpostgres.WithDatabase("postgres"),
|
|
tcpostgres.WithUsername("archie"),
|
|
tcpostgres.WithPassword("archie"),
|
|
tcpostgres.BasicWaitStrategies(),
|
|
)
|
|
defer func() { _ = testcontainers.TerminateContainer(container) }()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "pgtest: start %s (is Docker running?): %v\n", image, err)
|
|
return 1
|
|
}
|
|
adminURL, err = container.ConnectionString(ctx, "sslmode=disable")
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "pgtest: connection string: %v\n", err)
|
|
return 1
|
|
}
|
|
return m.Run()
|
|
}
|
|
|
|
// URL creates an empty database for t, drops it when t ends, and returns a
|
|
// connection URL for it.
|
|
func URL(t testing.TB) string {
|
|
t.Helper()
|
|
if adminURL == "" {
|
|
t.Fatal("pgtest: no container running; call pgtest.Main from TestMain")
|
|
}
|
|
name := fmt.Sprintf("t%d", nextDB.Add(1))
|
|
exec(t, "CREATE DATABASE "+name)
|
|
t.Cleanup(func() { exec(t, "DROP DATABASE "+name+" WITH (FORCE)") })
|
|
|
|
u, err := url.Parse(adminURL)
|
|
if err != nil {
|
|
t.Fatalf("pgtest: parse connection string: %v", err)
|
|
}
|
|
u.Path = "/" + name
|
|
return u.String()
|
|
}
|
|
|
|
func exec(t testing.TB, sql string) {
|
|
t.Helper()
|
|
ctx := context.Background()
|
|
conn, err := pgx.Connect(ctx, adminURL)
|
|
if err != nil {
|
|
t.Fatalf("pgtest: connect: %v", err)
|
|
}
|
|
defer func() { _ = conn.Close(ctx) }()
|
|
if _, err := conn.Exec(ctx, sql); err != nil {
|
|
t.Fatalf("pgtest: %s: %v", sql, err)
|
|
}
|
|
}
|