Skip to content

Commit d84da8f

Browse files
techknowlogicklafriks
authored andcommitted
Change parsing of postgresql settings (#4275)
* Change parsing of postgresql settings Fix #4200 * Add copyright * update postgresql connection string * add tests
1 parent a93f138 commit d84da8f

File tree

2 files changed

+54
-8
lines changed

2 files changed

+54
-8
lines changed

models/models.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2014 The Gogs Authors. All rights reserved.
2+
// Copyright 2018 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -184,6 +185,18 @@ func parsePostgreSQLHostPort(info string) (string, string) {
184185
return host, port
185186
}
186187

188+
func getPostgreSQLConnectionString(DBHost, DBUser, DBPasswd, DBName, DBParam, DBSSLMode string) (connStr string) {
189+
host, port := parsePostgreSQLHostPort(DBHost)
190+
if host[0] == '/' { // looks like a unix socket
191+
connStr = fmt.Sprintf("postgres://%s:%s@:%s/%s%ssslmode=%s&host=%s",
192+
url.PathEscape(DBUser), url.PathEscape(DBPasswd), port, DBName, DBParam, DBSSLMode, host)
193+
} else {
194+
connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s",
195+
url.PathEscape(DBUser), url.PathEscape(DBPasswd), host, port, DBName, DBParam, DBSSLMode)
196+
}
197+
return
198+
}
199+
187200
func parseMSSQLHostPort(info string) (string, string) {
188201
host, port := "127.0.0.1", "1433"
189202
if strings.Contains(info, ":") {
@@ -214,14 +227,7 @@ func getEngine() (*xorm.Engine, error) {
214227
DbCfg.User, DbCfg.Passwd, DbCfg.Host, DbCfg.Name, Param)
215228
}
216229
case "postgres":
217-
host, port := parsePostgreSQLHostPort(DbCfg.Host)
218-
if host[0] == '/' { // looks like a unix socket
219-
connStr = fmt.Sprintf("postgres://%s:%s@:%s/%s%ssslmode=%s&host=%s",
220-
url.QueryEscape(DbCfg.User), url.QueryEscape(DbCfg.Passwd), port, DbCfg.Name, Param, DbCfg.SSLMode, host)
221-
} else {
222-
connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s%ssslmode=%s",
223-
url.QueryEscape(DbCfg.User), url.QueryEscape(DbCfg.Passwd), host, port, DbCfg.Name, Param, DbCfg.SSLMode)
224-
}
230+
connStr = getPostgreSQLConnectionString(DbCfg.Host, DbCfg.User, DbCfg.Passwd, DbCfg.Name, Param, DbCfg.SSLMode)
225231
case "mssql":
226232
host, port := parseMSSQLHostPort(DbCfg.Host)
227233
connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, DbCfg.Name, DbCfg.User, DbCfg.Passwd)

models/models_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2016 The Gogs Authors. All rights reserved.
2+
// Copyright 2018 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -53,3 +54,42 @@ func Test_parsePostgreSQLHostPort(t *testing.T) {
5354
assert.Equal(t, test.Port, port)
5455
}
5556
}
57+
58+
func Test_getPostgreSQLConnectionString(t *testing.T) {
59+
tests := []struct {
60+
Host string
61+
Port string
62+
User string
63+
Passwd string
64+
Name string
65+
Param string
66+
SSLMode string
67+
Output string
68+
}{
69+
{
70+
Host: "/tmp/pg.sock",
71+
Port: "4321",
72+
User: "testuser",
73+
Passwd: "space space !#$%^^%^```-=?=",
74+
Name: "gitea",
75+
Param: "",
76+
SSLMode: "false",
77+
Output: "postgres://testuser:space%20space%20%21%23$%25%5E%5E%25%5E%60%60%60-=%3F=@:5432/giteasslmode=false&host=/tmp/pg.sock",
78+
},
79+
{
80+
Host: "localhost",
81+
Port: "1234",
82+
User: "pgsqlusername",
83+
Passwd: "I love Gitea!",
84+
Name: "gitea",
85+
Param: "",
86+
SSLMode: "true",
87+
Output: "postgres://pgsqlusername:I%20love%20Gitea%21@localhost:5432/giteasslmode=true",
88+
},
89+
}
90+
91+
for _, test := range tests {
92+
connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.Param, test.SSLMode)
93+
assert.Equal(t, test.Output, connStr)
94+
}
95+
}

0 commit comments

Comments
 (0)