Register pgx driver using major version

Fixed: #1480
pull/1504/head
Vitalii Solodilov 2023-02-10 21:38:25 +04:00 committed by Jack Christensen
parent a47e836471
commit 255f16b00f
2 changed files with 24 additions and 4 deletions

View File

@ -66,6 +66,7 @@ import (
"math"
"math/rand"
"reflect"
"sort"
"strconv"
"strings"
"sync"
@ -85,7 +86,13 @@ func init() {
pgxDriver = &Driver{
configs: make(map[string]*pgx.ConnConfig),
}
sql.Register("pgx", pgxDriver)
drivers := sql.Drivers()
// if pgx driver was already registered by different pgx major version then we skip registration under the default name.
if i := sort.SearchStrings(sql.Drivers(), "pgx"); len(drivers) >= i || drivers[i] != "pgx" {
sql.Register("pgx", pgxDriver)
}
sql.Register("pgx/v5", pgxDriver)
databaseSQLResultFormats = pgx.QueryResultFormatsByOID{
pgtype.BoolOID: 1,

View File

@ -146,9 +146,22 @@ func closeStmt(t *testing.T, stmt *sql.Stmt) {
}
func TestSQLOpen(t *testing.T) {
db, err := sql.Open("pgx", os.Getenv("PGX_TEST_DATABASE"))
require.NoError(t, err)
closeDB(t, db)
tests := []struct {
driverName string
}{
{driverName: "pgx"},
{driverName: "pgx/v5"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.driverName, func(t *testing.T) {
db, err := sql.Open(tt.driverName, os.Getenv("PGX_TEST_DATABASE"))
require.NoError(t, err)
closeDB(t, db)
})
}
}
func TestNormalLifeCycle(t *testing.T) {