diff --git a/.env.example b/.env.example index 5e2d216..030d1bd 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,4 @@ FATHOM_DEBUG=true -FATHOM_DATABASE_URL="" FATHOM_DATABASE_DRIVER="sqlite3" FATHOM_DATABASE_NAME="./fathom.db" FATHOM_DATABASE_USER="" diff --git a/pkg/config/config.go b/pkg/config/config.go index 9f18f5c..27de0f2 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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 diff --git a/pkg/datastore/sqlstore/config.go b/pkg/datastore/sqlstore/config.go index 3ad375d..93177c2 100644 --- a/pkg/datastore/sqlstore/config.go +++ b/pkg/datastore/sqlstore/config.go @@ -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