don't parse database driver from URL to also support mysql and sqlite.

This commit is contained in:
Danny van Kooten 2018-07-31 15:29:15 +02:00
parent 8f4a34282e
commit 4ecaff85ac
3 changed files with 25 additions and 23 deletions

View File

@ -1,5 +1,4 @@
FATHOM_DEBUG=true
FATHOM_DATABASE_URL=""
FATHOM_DATABASE_DRIVER="sqlite3"
FATHOM_DATABASE_NAME="./fathom.db"
FATHOM_DATABASE_USER=""

View File

@ -2,9 +2,9 @@ package config
import (
"math/rand"
"net/url"
"os"
"path/filepath"
"net/url"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
@ -54,11 +54,10 @@ func Parse() *Config {
}
if cfg.Database.URL != "" {
u, err := url.Parse(cfg.Database.URL)
_, err := url.Parse(cfg.Database.URL)
if err != nil {
log.Fatalf("Error parsing DATABASE_URL from environment: %s", err)
}
cfg.Database.Driver = u.Scheme
}
// alias sqlite to sqlite3

View File

@ -18,28 +18,32 @@ type Config struct {
func (c *Config) DSN() string {
var dsn string
// if FATHOM_DATABASE_URL was set, use that
// this relies on the user to set the appropriate parameters, eg ?parseTime=true when using MySQL
if c.URL != "" {
return c.URL
}
// otherwise, generate from individual fields
switch c.Driver {
case "postgres":
if c.URL != "" {
dsn = c.URL
} else {
params := map[string]string{
"host": c.Host,
"dbname": c.Name,
"user": c.User,
"password": c.Password,
"sslmode": c.SSLMode,
}
for k, v := range params {
if v == "" {
continue
}
dsn = dsn + k + "=" + v + " "
}
dsn = strings.TrimSpace(dsn)
params := map[string]string{
"host": c.Host,
"dbname": c.Name,
"user": c.User,
"password": c.Password,
"sslmode": c.SSLMode,
}
for k, v := range params {
if v == "" {
continue
}
dsn = dsn + k + "=" + v + " "
}
dsn = strings.TrimSpace(dsn)
case "mysql":
mc := mysql.NewConfig()
mc.User = c.User