Skip to content

Commit

Permalink
meta/pg:fix pg socket connection (#2607)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhijian-pro authored Aug 26, 2022
1 parent 42c66bc commit bb84127
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/en/guide/how_to_set_up_metadata_engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ When using PostgreSQL as the metadata storage engine, you need to create a datab
<TabItem value="tcp" label="TCP">

```
postgres://<username>[:<password>]@<host>[:5432]/<database-name>[?parameters]
postgres://[username][:<password>]@<host>[:5432]/<database-name>[?parameters]
```

</TabItem>
<TabItem value="unix-socket" label="Unix socket">

```
postgres:///<database-name>?host=<socket-directories-path>[&user=<user>&password=<password>]
postgres://[username][:<password>]@/<database-name>?host=<socket-directories-path>[&parameters]
```

</TabItem>
Expand Down
4 changes: 2 additions & 2 deletions docs/zh_cn/guide/how_to_set_up_metadata_engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ KeyDB 的数据复制是异步的,使用 Active Active(双活)功能可能
<TabItem value="tcp" label="TCP">

```
postgres://<username>[:<password>]@<host>[:5432]/<database-name>[?parameters]
postgres://[username][:<password>]@<host>[:5432]/<database-name>[?parameters]
```

</TabItem>
<TabItem value="unix-socket" label="Unix socket">

```
postgres:///<database-name>?host=<socket-directories-path>[&user=<user>&password=<password>]
postgres://[username][:<password>]@/<database-name>?host=<socket-directories-path>[&parameters]
```

</TabItem>
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func setPasswordFromEnv(uri string) (string, error) {
dIndex := strings.Index(uri, "://") + 3
s := strings.Split(uri[dIndex:atIndex], ":")

if len(s) > 2 || s[0] == "" {
if len(s) > 2 {
return "", fmt.Errorf("invalid uri: %s", uri)
}

Expand Down
15 changes: 12 additions & 3 deletions pkg/meta/interface_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ func Test_setPasswordFromEnv(t *testing.T) {
args: "mysql://root@(127.0.0.1:3306)/juicefs",
want: "mysql://root:dbPasswd@(127.0.0.1:3306)/juicefs",
},
// no user is ok
{
args: "mysql://(127.0.0.1:3306)/juicefs",
want: "",
args: "mysql://:@(127.0.0.1:3306)/juicefs",
want: "mysql://:dbPasswd@(127.0.0.1:3306)/juicefs",
},
{
args: "mysql://:pwd@(127.0.0.1:3306)/juicefs",
want: "",
want: "mysql://:pwd@(127.0.0.1:3306)/juicefs",
},
{
args: "mysql://a:b:c:@(127.0.0.1:3306)/juicefs",
Expand All @@ -66,6 +67,14 @@ func Test_setPasswordFromEnv(t *testing.T) {
args: "postgres://[email protected]:5432/juicefs",
want: "postgres://root:[email protected]:5432/juicefs",
},
{
args: "postgres://root@/pgtest?host=/tmp/pgsocket/&port=5433",
want: "postgres://root:dbPasswd@/pgtest?host=/tmp/pgsocket/&port=5433",
},
{
args: "postgres://@/pgtest?host=/tmp/pgsocket/&port=5433&user=pguser",
want: "postgres://:dbPasswd@/pgtest?host=/tmp/pgsocket/&port=5433&user=pguser",
},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
Expand Down

0 comments on commit bb84127

Please sign in to comment.