Skip to content

Commit 17d246c

Browse files
authored
Fix incorrect pgsql conn builder behavior (#28085)
Fix #28083 and fix the tests
1 parent fce1d5d commit 17d246c

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

modules/setting/database.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ func DBConnStr() (string, error) {
109109
connStr = fmt.Sprintf("%s:%s@%s(%s)/%s%scharset=%s&parseTime=true&tls=%s",
110110
Database.User, Database.Passwd, connType, Database.Host, Database.Name, paramSep, Database.MysqlCharset, tls)
111111
case "postgres":
112-
connStr = getPostgreSQLConnectionString(Database.Host, Database.User, Database.Passwd, Database.Name, paramSep, Database.SSLMode)
112+
connStr = getPostgreSQLConnectionString(Database.Host, Database.User, Database.Passwd, Database.Name, Database.SSLMode)
113113
case "mssql":
114114
host, port := ParseMSSQLHostPort(Database.Host)
115115
connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, Database.Name, Database.User, Database.Passwd)
@@ -157,7 +157,8 @@ func parsePostgreSQLHostPort(info string) (host, port string) {
157157
return host, port
158158
}
159159

160-
func getPostgreSQLConnectionString(dbHost, dbUser, dbPasswd, dbName, dbParam, dbsslMode string) (connStr string) {
160+
func getPostgreSQLConnectionString(dbHost, dbUser, dbPasswd, dbName, dbsslMode string) (connStr string) {
161+
dbName, dbParam, _ := strings.Cut(dbName, "?")
161162
host, port := parsePostgreSQLHostPort(dbHost)
162163
connURL := url.URL{
163164
Scheme: "postgres",

modules/setting/database_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,39 @@ func Test_parsePostgreSQLHostPort(t *testing.T) {
5959
func Test_getPostgreSQLConnectionString(t *testing.T) {
6060
tests := []struct {
6161
Host string
62-
Port string
6362
User string
6463
Passwd string
6564
Name string
66-
Param string
6765
SSLMode string
6866
Output string
6967
}{
7068
{
7169
Host: "/tmp/pg.sock",
72-
Port: "4321",
7370
User: "testuser",
7471
Passwd: "space space !#$%^^%^```-=?=",
7572
Name: "gitea",
76-
Param: "",
7773
SSLMode: "false",
7874
Output: "postgres://testuser:space%20space%20%21%23$%25%5E%5E%25%5E%60%60%60-=%3F=@:5432/gitea?host=%2Ftmp%2Fpg.sock&sslmode=false",
7975
},
8076
{
8177
Host: "localhost",
82-
Port: "1234",
8378
User: "pgsqlusername",
8479
Passwd: "I love Gitea!",
8580
Name: "gitea",
86-
Param: "",
8781
SSLMode: "true",
8882
Output: "postgres://pgsqlusername:I%20love%20Gitea%21@localhost:5432/gitea?sslmode=true",
8983
},
84+
{
85+
Host: "localhost:1234",
86+
User: "user",
87+
Passwd: "pass",
88+
Name: "gitea?param=1",
89+
Output: "postgres://user:pass@localhost:1234/gitea?param=1&sslmode=",
90+
},
9091
}
9192

9293
for _, test := range tests {
93-
connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.Param, test.SSLMode)
94+
connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.SSLMode)
9495
assert.Equal(t, test.Output, connStr)
9596
}
9697
}

0 commit comments

Comments
 (0)