Skip to content
Merged
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
10 changes: 4 additions & 6 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
24 changes: 24 additions & 0 deletions connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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": ""}, ""},

Expand Down Expand Up @@ -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
Expand Down
Loading