1- // Copyright (c) 2023 Vasiliy Vasilyuk. All rights reserved.
1+ // Copyright (c) 2023-2024 Vasiliy Vasilyuk. All rights reserved.
22// Use of this source code is governed by a BSD-style
33// license that can be found in the LICENSE file.
44
@@ -7,6 +7,7 @@ package testingpg
77import (
88 "context"
99 "crypto/rand"
10+ "database/sql"
1011 "encoding/base64"
1112 "fmt"
1213 "net/url"
@@ -16,7 +17,7 @@ import (
1617 "time"
1718 "unicode"
1819
19- "github.com/jackc/pgx/v5/pgxpool "
20+ _ "github.com/jackc/pgx/v5/stdlib "
2021 "github.com/stretchr/testify/require"
2122)
2223
@@ -40,8 +41,8 @@ type Postgres struct {
4041 url string
4142 ref string
4243
43- pgxpool * pgxpool. Pool
44- pgxpoolOnce sync.Once
44+ sqlDB * sql. DB
45+ sqlDBOnce sync.Once
4546}
4647
4748func newPostgres (t TestingT ) * Postgres {
@@ -71,12 +72,12 @@ func (p *Postgres) URL() string {
7172 return p .url
7273}
7374
74- func (p * Postgres ) PgxPool () * pgxpool. Pool {
75- p .pgxpoolOnce .Do (func () {
76- p .pgxpool = newPGxPool (p .t , p .URL ())
75+ func (p * Postgres ) DB () * sql. DB {
76+ p .sqlDBOnce .Do (func () {
77+ p .sqlDB = open (p .t , p .URL ())
7778 })
7879
79- return p .pgxpool
80+ return p .sqlDB
8081}
8182
8283func (p * Postgres ) cloneFromReference () * Postgres {
@@ -90,7 +91,7 @@ func (p *Postgres) cloneFromReference() *Postgres {
9091 p .ref ,
9192 )
9293
93- _ , err := p .PgxPool ().Exec (context .Background (), sql )
94+ _ , err := p .DB ().ExecContext (context .Background (), sql )
9495 require .NoError (p .t , err )
9596
9697 // Automatically drop database copy after the test is completed.
@@ -100,7 +101,7 @@ func (p *Postgres) cloneFromReference() *Postgres {
100101 ctx , done := context .WithTimeout (context .Background (), time .Minute )
101102 defer done ()
102103
103- _ , err := p .PgxPool ().Exec (ctx , sql )
104+ _ , err := p .DB ().ExecContext (ctx , sql )
104105 require .NoError (p .t , err )
105106 })
106107
@@ -169,17 +170,14 @@ func replaceDBName(t TestingT, dataSourceURL, dbname string) string {
169170 return r .String ()
170171}
171172
172- func newPGxPool (t TestingT , dataSourceURL string ) * pgxpool.Pool {
173- ctx , done := context .WithTimeout (context .Background (), 1 * time .Second )
174- defer done ()
175-
176- pool , err := pgxpool .New (ctx , dataSourceURL )
173+ func open (t TestingT , dataSourceURL string ) * sql.DB {
174+ db , err := sql .Open ("pgx/v5" , dataSourceURL )
177175 require .NoError (t , err )
178176
179177 // Automatically close connection after the test is completed.
180178 t .Cleanup (func () {
181- pool .Close ()
179+ db .Close ()
182180 })
183181
184- return pool
182+ return db
185183}
0 commit comments