Fix incorrect pgsql conn builder behavior (#28085)

Fix #28083 and fix the tests
This commit is contained in:
wxiaoguang 2023-11-17 10:30:57 +08:00 committed by GitHub
parent fce1d5d7dc
commit 17d246cdcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 9 deletions

View file

@ -109,7 +109,7 @@ func DBConnStr() (string, error) {
connStr = fmt.Sprintf("%s:%s@%s(%s)/%s%scharset=%s&parseTime=true&tls=%s", connStr = fmt.Sprintf("%s:%s@%s(%s)/%s%scharset=%s&parseTime=true&tls=%s",
Database.User, Database.Passwd, connType, Database.Host, Database.Name, paramSep, Database.MysqlCharset, tls) Database.User, Database.Passwd, connType, Database.Host, Database.Name, paramSep, Database.MysqlCharset, tls)
case "postgres": case "postgres":
connStr = getPostgreSQLConnectionString(Database.Host, Database.User, Database.Passwd, Database.Name, paramSep, Database.SSLMode) connStr = getPostgreSQLConnectionString(Database.Host, Database.User, Database.Passwd, Database.Name, Database.SSLMode)
case "mssql": case "mssql":
host, port := ParseMSSQLHostPort(Database.Host) host, port := ParseMSSQLHostPort(Database.Host)
connStr = fmt.Sprintf("server=%s; port=%s; database=%s; user id=%s; password=%s;", host, port, Database.Name, Database.User, Database.Passwd) 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) {
return host, port return host, port
} }
func getPostgreSQLConnectionString(dbHost, dbUser, dbPasswd, dbName, dbParam, dbsslMode string) (connStr string) { func getPostgreSQLConnectionString(dbHost, dbUser, dbPasswd, dbName, dbsslMode string) (connStr string) {
dbName, dbParam, _ := strings.Cut(dbName, "?")
host, port := parsePostgreSQLHostPort(dbHost) host, port := parsePostgreSQLHostPort(dbHost)
connURL := url.URL{ connURL := url.URL{
Scheme: "postgres", Scheme: "postgres",

View file

@ -59,38 +59,39 @@ func Test_parsePostgreSQLHostPort(t *testing.T) {
func Test_getPostgreSQLConnectionString(t *testing.T) { func Test_getPostgreSQLConnectionString(t *testing.T) {
tests := []struct { tests := []struct {
Host string Host string
Port string
User string User string
Passwd string Passwd string
Name string Name string
Param string
SSLMode string SSLMode string
Output string Output string
}{ }{
{ {
Host: "/tmp/pg.sock", Host: "/tmp/pg.sock",
Port: "4321",
User: "testuser", User: "testuser",
Passwd: "space space !#$%^^%^```-=?=", Passwd: "space space !#$%^^%^```-=?=",
Name: "gitea", Name: "gitea",
Param: "",
SSLMode: "false", SSLMode: "false",
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", 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",
}, },
{ {
Host: "localhost", Host: "localhost",
Port: "1234",
User: "pgsqlusername", User: "pgsqlusername",
Passwd: "I love Gitea!", Passwd: "I love Gitea!",
Name: "gitea", Name: "gitea",
Param: "",
SSLMode: "true", SSLMode: "true",
Output: "postgres://pgsqlusername:I%20love%20Gitea%21@localhost:5432/gitea?sslmode=true", Output: "postgres://pgsqlusername:I%20love%20Gitea%21@localhost:5432/gitea?sslmode=true",
}, },
{
Host: "localhost:1234",
User: "user",
Passwd: "pass",
Name: "gitea?param=1",
Output: "postgres://user:pass@localhost:1234/gitea?param=1&sslmode=",
},
} }
for _, test := range tests { for _, test := range tests {
connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.Param, test.SSLMode) connStr := getPostgreSQLConnectionString(test.Host, test.User, test.Passwd, test.Name, test.SSLMode)
assert.Equal(t, test.Output, connStr) assert.Equal(t, test.Output, connStr)
} }
} }