diff --git a/connector.go b/connector.go index 4c318662a..1827fdbdd 100644 --- a/connector.go +++ b/connector.go @@ -758,14 +758,12 @@ func (cfg *Config) setFromTag(o map[string]string, tag string) error { // Set run-time; we delete map keys as they're set in the struct. if tag == "postgres" { - // Make sure database= sets dbname=; in startup() we send database for - // dbname, and if we have both set it's inconsistent as the loop order - // is a map. + // Make sure database= sets dbname=, as that previously worked (kind of + // by accident). + // TODO(v2): remove if d, ok := o["database"]; ok { + cfg.Database = d delete(o, "database") - if o["dbname"] == "" { - o["dbname"] = d - } } cfg.Runtime = o } diff --git a/connector_test.go b/connector_test.go index 77fc91554..370f9b216 100644 --- a/connector_test.go +++ b/connector_test.go @@ -113,8 +113,29 @@ func TestNewConnector(t *testing.T) { defer db.Close() useConn(t, db) }) + + t.Run("database=", func(t *testing.T) { + want1, want2 := `pq: database "err" does not exist (3D000)`, + `pq: database "two" does not exist (3D000)` + if pqtest.Pgbouncer() { + want1, want2 = `pq: no such database: err (08P01)`, `pq: no such database: two (08P01)` + } + + // Make sure database= consistently take precedence over dbname= + for i := 0; i < 10; i++ { + err := pqtest.MustDB(t, "database=err").Ping() + if err == nil || err.Error() != want1 { + t.Errorf("wrong error:\nhave: %s\nwant: %s", err, want1) + } + err = pqtest.MustDB(t, "dbname=one database=two").Ping() + if err == nil || err.Error() != want2 { + t.Errorf("wrong error:\nhave: %s\nwant: %s", err, want2) + } + } + }) } +// TODO: this can be merged with TestNewConfig, I think? func TestParseOpts(t *testing.T) { tests := []struct { in string @@ -130,6 +151,7 @@ func TestParseOpts(t *testing.T) { {"dbname=データベース password=パスワード", map[string]string{"dbname": "データベース", "password": "パスワード"}, ""}, {"dbname=hello user=''", map[string]string{"dbname": "hello", "user": ""}, ""}, {"user='' dbname=hello", map[string]string{"dbname": "hello", "user": ""}, ""}, + // The last option value is an empty string if there's no non-whitespace after its = {"dbname=hello user= ", map[string]string{"dbname": "hello", "user": ""}, ""}, @@ -229,6 +251,8 @@ func TestRuntimeParameters(t *testing.T) { }) } } + +// TODO: this can be merged with TestNewConfig, I think? func TestParseEnviron(t *testing.T) { tests := []struct { in []string