2
0

Add BeforeConnect callback to pgxpool.Config.

This allows for connection settings to be updated without having to create
a new pool. The callback is passed a copy of the pgx.ConnConfig and will
not impact existing live connections.
This commit is contained in:
Robert Froehlich
2021-01-02 15:08:59 -08:00
parent b664891853
commit 210a217818
2 changed files with 37 additions and 1 deletions
+21
View File
@@ -112,6 +112,27 @@ func TestPoolAcquireAndConnRelease(t *testing.T) {
c.Release()
}
func TestPoolBeforeConnect(t *testing.T) {
t.Parallel()
config, err := pgxpool.ParseConfig(os.Getenv("PGX_TEST_DATABASE"))
require.NoError(t, err)
config.BeforeConnect = func(ctx context.Context, cfg *pgx.ConnConfig) error {
cfg.Config.RuntimeParams["application_name"] = "pgx"
return nil
}
db, err := pgxpool.ConnectConfig(context.Background(), config)
require.NoError(t, err)
defer db.Close()
var str string
err = db.QueryRow(context.Background(), "SHOW application_name").Scan(&str)
require.NoError(t, err)
assert.EqualValues(t, "pgx", str)
}
func TestPoolAfterConnect(t *testing.T) {
t.Parallel()