Add postgres support #135
57
pkg/db/db.go
|
@ -21,6 +21,7 @@ import (
|
|||
"code.vikunja.io/api/pkg/log"
|
||||
"encoding/gob"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -120,31 +121,43 @@ func initMysqlEngine() (engine *xorm.Engine, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
|
||||
// parsePostgreSQLHostPort parses given input in various forms defined in
|
||||
// https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
|
||||
// and returns proper host and port number.
|
||||
func parsePostgreSQLHostPort(info string) (string, string) {
|
||||
host, port := "127.0.0.1", "5432"
|
||||
if strings.Contains(info, ":") && !strings.HasSuffix(info, "]") {
|
||||
idx := strings.LastIndex(info, ":")
|
||||
host = info[:idx]
|
||||
port = info[idx+1:]
|
||||
} else if len(info) > 0 {
|
||||
host = info
|
||||
}
|
||||
return host, port
|
||||
}
|
||||
|
||||
jtojnar
commented
We will probably need to allow passing the rest of the options. The Go library recommended by xorm decided to stray from libpq’s defaults and forces Or maybe we can just accept raw connection string? We will probably need to allow passing the rest of the options. The Go library recommended by xorm decided to stray from libpq’s defaults and forces `sslmode=required`, which fails on my laptop where I do not have TLS set up.
Or maybe we can just accept raw connection string?
konrad
commented
I'd say we probably should add a setting for I'd say we probably should add a setting for `sslmode`.
|
||||
func initPostgresEngine() (engine *xorm.Engine, err error) {
|
||||
var connStr strings.Builder
|
||||
|
||||
// https://pkg.go.dev/github.com/lib/pq?tab=doc#hdr-Connection_String_Parameters
|
||||
params := map[string]string{
|
||||
"user": config.DatabaseUser.GetString(),
|
||||
"password": config.DatabasePassword.GetString(),
|
||||
"host": config.DatabaseHost.GetString(),
|
||||
"dbname": config.DatabaseDatabase.GetString(),
|
||||
"sslmode": config.DatabaseSslMode.GetString(),
|
||||
var connStr string
|
||||
host, port := parsePostgreSQLHostPort(config.DatabaseHost.GetString())
|
||||
if strings.HasPrefix(config.DatabaseHost.GetString(), "/") { // looks like a unix socket
|
||||
connStr = fmt.Sprintf("postgres://%s:%s@:%s/%ssslmode=%s&host=%s",
|
||||
jtojnar
commented
Why not just use the Why not just use the `attr=name` format instead of URI format? Or use this one for both UNIX socket and TCP connection? This only needs to be used for UNIX sockets because URI hostnames cannot contain `/` but the other way around is fine.
konrad
commented
Good point. What do think would be better suited, using the same for both or using the Good point. What do think would be better suited, using the same for both or using the `attr=name` format?
jtojnar
commented
I think I think `attr=%s` is clearer.
konrad
commented
Then lets change it. Then lets change it.
|
||||
url.PathEscape(config.DatabaseUser.GetString()),
|
||||
url.PathEscape(config.DatabasePassword.GetString()),
|
||||
port,
|
||||
config.DatabaseDatabase.GetString(),
|
||||
config.DatabaseSslMode.GetString(),
|
||||
jtojnar
commented
I am not sure if these flags are needed. I am not sure if these flags are needed.
jtojnar
commented
I am not sure if these flags are needed. I am not sure if these flags are needed.
konrad
commented
I'd say yes, since the max number of open connections can be a limiting factor for performance. I'd say yes, since the max number of open connections can be a limiting factor for performance.
|
||||
host)
|
||||
} else {
|
||||
connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%ssslmode=%s",
|
||||
url.PathEscape(config.DatabaseUser.GetString()),
|
||||
url.PathEscape(config.DatabasePassword.GetString()),
|
||||
host,
|
||||
port,
|
||||
config.DatabaseDatabase.GetString(),
|
||||
config.DatabaseSslMode.GetString())
|
||||
}
|
||||
|
||||
for name, value := range params {
|
||||
if name != "" {
|
||||
value = strings.ReplaceAll(value, "\\", "\\\\")
|
||||
value = strings.ReplaceAll(value, "'", "\\'")
|
||||
|
||||
if connStr.Len() > 0 {
|
||||
connStr.WriteString(" ")
|
||||
}
|
||||
connStr.WriteString(fmt.Sprintf("%s='%s'", name, value))
|
||||
}
|
||||
}
|
||||
|
||||
engine, err = xorm.NewEngine("postgres", connStr.String())
|
||||
engine, err = xorm.NewEngine("postgres", connStr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
This function is super ugly but I am not sure if there is a better way to do this. Maybe iterating over
A much cleaner way would be to use something like
Well, we still want to be able to not pass some of those keys. For example, I connect to the database via socket so username and password does not make sense.
I think we could use something like this:
ad1b6d439f/modules/setting/database.go (L152-L162)