fixup! Add postgres support

This commit is contained in:
Jan Tojnar 2020-02-15 11:43:35 +01:00 committed by kolaente
parent a9c762421f
commit 5afced95c8
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
4 changed files with 25 additions and 17 deletions

View File

@ -42,7 +42,7 @@ You might be prompted for the password of the database user.
To restore it, simply pipe it back into the `psql` command:
{{< highlight bash >}}
mysql -U <user> -h <db-host> <database> < vikunja-backup.sql
psql -U <user> -h <db-host> <database> < vikunja-backup.sql
{{< /highlight >}}
For more information, please visit the [relevant PostgreSQL documentation](https://www.postgresql.org/docs/12/backup-dump.html).

View File

@ -91,6 +91,9 @@ database:
maxidleconnections: 50
# The maximum lifetime of a single db connection in miliseconds.
maxconnectionlifetime: 10000
# Secure connection mode. Only used with postgres.
# (see https://pkg.go.dev/github.com/lib/pq?tab=doc#hdr-Connection_String_Parameters)
sslmode: disable
cache:
# If cache is enabled or not

View File

@ -55,6 +55,7 @@ const (
DatabaseMaxOpenConnections Key = `database.maxopenconnections`
DatabaseMaxIdleConnections Key = `database.maxidleconnections`
DatabaseMaxConnectionLifetime Key = `database.maxconnectionlifetime`
DatabaseSslMode Key = `database.sslmode`
CacheEnabled Key = `cache.enabled`
CacheType Key = `cache.type`
@ -181,6 +182,7 @@ func InitDefaultConfig() {
DatabaseMaxOpenConnections.setDefault(100)
DatabaseMaxIdleConnections.setDefault(50)
DatabaseMaxConnectionLifetime.setDefault(10000)
DatabaseSslMode.setDefault("disable")
// Cacher
CacheEnabled.setDefault(false)

View File

@ -120,26 +120,29 @@ func initMysqlEngine() (engine *xorm.Engine, err error) {
return
}
func postgresConnStrAppend(connStr *strings.Builder, name string, value string) {
if name != "" {
value = strings.ReplaceAll(value, "\\", "\\\\")
value = strings.ReplaceAll(value, "'", "\\'")
if connStr.Len() > 0 {
connStr.WriteString(" ")
}
connStr.WriteString(fmt.Sprintf("%s='%s'", name, value))
}
}
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
postgresConnStrAppend(&connStr, "user", config.DatabaseUser.GetString())
postgresConnStrAppend(&connStr, "password", config.DatabasePassword.GetString())
postgresConnStrAppend(&connStr, "host", config.DatabaseHost.GetString())
postgresConnStrAppend(&connStr, "dbname", config.DatabaseDatabase.GetString())
params := map[string]string{
"user": config.DatabaseUser.GetString(),
"password": config.DatabasePassword.GetString(),
"host": config.DatabaseHost.GetString(),
"dbname": config.DatabaseDatabase.GetString(),
"sslmode": 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())
if err != nil {