Skip to content

Commit

Permalink
Don't hold a connection to the postgres instance after creating the DB
Browse files Browse the repository at this point in the history
  • Loading branch information
josephschorr committed Jan 10, 2025
1 parent 324ae5a commit 971cfd8
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions internal/testserver/datastore/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ type container struct {

type postgresTester struct {
container
hostConn *pgx.Conn
creds string
targetMigration string
pgbouncerProxy *container
pool *dockertest.Pool
useContainerHostname bool
}

Expand Down Expand Up @@ -98,13 +98,10 @@ func RunPostgresForTestingWithCommitTimestamps(t testing.TB, bridgeNetworkName s
creds: POSTGRES_TEST_USER + ":" + POSTGRES_TEST_PASSWORD,
targetMigration: targetMigration,
useContainerHostname: bridgeSupplied,
pool: pool,
}

t.Cleanup(func() {
if builder.hostConn != nil {
require.NoError(t, builder.hostConn.Close(context.Background()))
}

require.NoError(t, pool.Purge(postgres))
})

Expand All @@ -113,8 +110,6 @@ func RunPostgresForTestingWithCommitTimestamps(t testing.TB, bridgeNetworkName s
builder.runPgbouncerForTesting(t, pool, bridgeNetworkName)
}

builder.hostConn = builder.initializeHostConnection(t, pool)

return builder
}

Expand All @@ -124,10 +119,14 @@ func (b *postgresTester) NewDatabase(t testing.TB) string {

newDBName := "db" + uniquePortion

_, err = b.hostConn.Exec(context.Background(), "CREATE DATABASE "+newDBName)
ctx := context.Background()
conn := b.initializeHostConnection(t)
defer conn.Close(ctx)

_, err = conn.Exec(ctx, "CREATE DATABASE "+newDBName)
require.NoError(t, err)

row := b.hostConn.QueryRow(context.Background(), "SELECT datname FROM pg_catalog.pg_database WHERE datname = $1", newDBName)
row := conn.QueryRow(ctx, "SELECT datname FROM pg_catalog.pg_database WHERE datname = $1", newDBName)
var dbName string
err = row.Scan(&dbName)
require.NoError(t, err)
Expand Down Expand Up @@ -218,10 +217,10 @@ func (b *postgresTester) runPgbouncerForTesting(t testing.TB, pool *dockertest.P
}
}

func (b *postgresTester) initializeHostConnection(t testing.TB, pool *dockertest.Pool) (conn *pgx.Conn) {
func (b *postgresTester) initializeHostConnection(t testing.TB) (conn *pgx.Conn) {
hostname, port := b.getHostHostnameAndPort()
uri := fmt.Sprintf("postgresql://%s@%s:%s/?sslmode=disable", b.creds, hostname, port)
err := pool.Retry(func() error {
err := b.pool.Retry(func() error {
var err error
ctx, cancelConnect := context.WithTimeout(context.Background(), dockerBootTimeout)
defer cancelConnect()
Expand Down

0 comments on commit 971cfd8

Please sign in to comment.