Skip to content

Commit cc2110a

Browse files
author
quackable
committed
Fixed empty database in DSN while using config.WithDatabase
1 parent b262476 commit cc2110a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

internal/dsn/dsn.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ func Parse(dsn string) (info parsedInfo, err error) {
3535
info.Options = append(info.Options,
3636
config.WithSecure(uri.Scheme != insecureSchema),
3737
config.WithEndpoint(uri.Host),
38-
config.WithDatabase(uri.Path),
3938
)
39+
if uri.Path != "" {
40+
info.Options = append(info.Options, config.WithDatabase(uri.Path))
41+
}
4042
if uri.User != nil {
4143
password, _ := uri.User.Password()
4244
info.UserInfo = &UserInfo{

internal/dsn/dsn_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,15 @@ func TestParseConnectionString(t *testing.T) {
124124
})
125125
}
126126
}
127+
128+
func TestParseConnectionStringEmptyDatabase(t *testing.T) {
129+
info, err := Parse("grpc://ydb-ru.yandex.net:2135")
130+
if err != nil {
131+
t.Fatalf("Received unexpected error:\n%+v", err)
132+
}
133+
c := config.New(config.WithDatabase("mydb"))
134+
c = c.With(info.Options...)
135+
require.Equal(t, false, c.Secure())
136+
require.Equal(t, "ydb-ru.yandex.net:2135", c.Endpoint())
137+
require.Equal(t, "mydb", c.Database())
138+
}

0 commit comments

Comments
 (0)