Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to change (or disable) the default driver name for registration #1160

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,19 @@ func (d Driver) Open(name string) (driver.Conn, error) {
return Open(name)
}

// This variable can be replaced with -ldflags like below:
// go build "-ldflags=-X github.com/lib/pq.driverName=custom"
var driverName = "postgres"

func init() {
sql.Register("postgres", &Driver{})
if driverName != "" {
sql.Register(driverName, &Driver{})
}
}

// DriverName return default driver name
func DriverName() string {
return driverName
}

type parameterStatus struct {
Expand Down Expand Up @@ -1251,7 +1262,7 @@ func (cn *conn) auth(r *readBuf, o values) {
token, err = cli.GetInitTokenFromSpn(spn)
} else {
// Allow the kerberos service name to be overridden
service := "postgres"
service := driverName
if val, ok := o["krbsrvname"]; ok {
service = val
}
Expand Down
19 changes: 18 additions & 1 deletion conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ import (
"time"
)

// This variable can be replaced with -ldflags like below:
// go test "-ldflags=-X github.com/lib/pq.driverNameTest=custom"
var driverNameTest string

func init() {
if driverNameTest == "" {
driverNameTest = driverName
}
}

type Fatalistic interface {
Fatal(args ...interface{})
}
Expand Down Expand Up @@ -49,7 +59,7 @@ func testConninfo(conninfo string) string {
}

func openTestConnConninfo(conninfo string) (*sql.DB, error) {
return sql.Open("postgres", testConninfo(conninfo))
return sql.Open(driverNameTest, testConninfo(conninfo))
}

func openTestConn(t Fatalistic) *sql.DB {
Expand Down Expand Up @@ -1971,3 +1981,10 @@ func TestStmtExecContext(t *testing.T) {
})
}
}

func TestDriverName(t *testing.T) {
dn := DriverName()
if dn != driverName {
t.Fatalf("DriverName() returned driverName: %v, want: %v", dn, driverName)
}
}
4 changes: 2 additions & 2 deletions hstore/hstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"testing"

_ "github.com/lib/pq"
"github.com/lib/pq"
)

type Fatalistic interface {
Expand All @@ -24,7 +24,7 @@ func openTestConn(t Fatalistic) *sql.DB {
os.Setenv("PGSSLMODE", "disable")
}

conn, err := sql.Open("postgres", "")
conn, err := sql.Open(pq.DriverName(), "")
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion ssl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func TestSNISupport(t *testing.T) {

// We are okay to skip this error as we are polling serverErrChan and we'll get an error
// or timeout from the server side in case of problems here.
db, _ := sql.Open("postgres", connStr)
db, _ := sql.Open(driverNameTest, connStr)
_, _ = db.Exec("SELECT 1")

// Check SNI data
Expand Down
2 changes: 1 addition & 1 deletion url.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func ParseURL(url string) (string, error) {
return "", err
}

if u.Scheme != "postgres" && u.Scheme != "postgresql" {
if u.Scheme != driverName && u.Scheme != "postgresql" {
return "", fmt.Errorf("invalid connection protocol: %s", u.Scheme)
}

Expand Down