Add postgres support #135

Merged
konrad merged 26 commits from jtojnar/api:pgsql into master 2020-02-16 21:42:05 +00:00
1 changed files with 8 additions and 18 deletions
Showing only changes of commit fed3a01617 - Show all commits

View File

@ -137,25 +137,15 @@ func parsePostgreSQLHostPort(info string) (string, 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?

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?

I'd say we probably should add a setting for sslmode.

I'd say we probably should add a setting for `sslmode`.
func initPostgresEngine() (engine *xorm.Engine, err error) {
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/%s?sslmode=%s&host=%s",
url.PathEscape(config.DatabaseUser.GetString()),
url.PathEscape(config.DatabasePassword.GetString()),
port,
config.DatabaseDatabase.GetString(),
config.DatabaseSslMode.GetString(),
host)
} else {
connStr = fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s",
url.PathEscape(config.DatabaseUser.GetString()),
url.PathEscape(config.DatabasePassword.GetString()),
host,
port,
config.DatabaseDatabase.GetString(),
config.DatabaseSslMode.GetString())
}
connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
host,
port,

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.

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.

Good point. What do think would be better suited, using the same for both or using the attr=name format?

Good point. What do think would be better suited, using the same for both or using the `attr=name` format?

I think attr=%s is clearer.

I think `attr=%s` is clearer.

Then lets change it.

Then lets change it.
url.PathEscape(config.DatabaseUser.GetString()),
url.PathEscape(config.DatabasePassword.GetString()),
config.DatabaseDatabase.GetString(),
config.DatabaseSslMode.GetString(),
)

I am not sure if these flags are needed.

I am not sure if these flags are needed.

I am not sure if these flags are needed.

I am not sure if these flags are needed.

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.
engine, err = xorm.NewEngine("postgres", connStr)
if err != nil {