Skip to content

Commit

Permalink
show only database name and maybe host when logging successful db con…
Browse files Browse the repository at this point in the history
…nection. #174
  • Loading branch information
dannyvankooten committed Nov 2, 2018
1 parent 4dec431 commit cb25b26
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 31 deletions.
67 changes: 41 additions & 26 deletions pkg/datastore/sqlstore/config.go
@@ -1,69 +1,84 @@
package sqlstore

import (
"regexp"
"strings"

mysql "github.com/go-sql-driver/mysql"
)

type Config struct {
Driver string `default:"sqlite3"`
url string `default:""`
host string `default:""`
user string `default:""`
password string `default:""`
name string `default:"fathom.db"`
sslmode string `default:""`
URL string `default:""`
Host string `default:""`
User string `default:""`
Password string `default:""`
Name string `default:"fathom.db"`
SSLMode string `default:""`
}

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
if c.URL != "" {
return c.URL
}

// otherwise, generate from individual fields
switch c.Driver {
case POSTGRES:

if c.host != "" {
dsn += " host=" + c.host
if c.Host != "" {
dsn += " host=" + c.Host
}
if c.name != "" {
dsn += " dbname=" + c.name
if c.Name != "" {
dsn += " dbname=" + c.Name
}
if c.user != "" {
dsn += " user=" + c.user
if c.User != "" {
dsn += " user=" + c.User
}
if c.password != "" {
dsn += " password=" + c.password
if c.Password != "" {
dsn += " password=" + c.Password
}
if c.sslmode != "" {
dsn += " sslmode=" + c.sslmode
if c.SSLMode != "" {
dsn += " sslmode=" + c.SSLMode
}

dsn = strings.TrimSpace(dsn)
case MYSQL:
mc := mysql.NewConfig()
mc.User = c.user
mc.Passwd = c.password
mc.Addr = c.host
mc.User = c.User
mc.Passwd = c.Password
mc.Addr = c.Host
mc.Net = "tcp"
mc.DBName = c.name
mc.DBName = c.Name
mc.Params = map[string]string{
"parseTime": "true",
"loc": "Local",
}
if c.sslmode != "" {
mc.Params["tls"] = c.sslmode
if c.SSLMode != "" {
mc.Params["tls"] = c.SSLMode
}
dsn = mc.FormatDSN()
case SQLITE:
dsn = c.name + "?_loc=auto&_busy_timeout=5000"
dsn = c.Name + "?_loc=auto&_busy_timeout=5000"
}

return dsn
}

// Dbname returns the database name, either from config values or from the connection URL
func (c *Config) Dbname() string {
if c.Name != "" {
return c.Name
}

re := regexp.MustCompile(`(?:dbname=|[^\/]?\/)(\w+)`)
m := re.FindStringSubmatch(c.URL)
if len(m) > 1 {
return m[1]
}

return ""
}
25 changes: 25 additions & 0 deletions pkg/datastore/sqlstore/config_test.go
Expand Up @@ -27,3 +27,28 @@ func TestConfigDSN(t *testing.T) {
t.Errorf("Invalid DSN. Expected %s, got %s", e, v)
}
}

func TestConfigDbname(t *testing.T) {
var c Config

c = Config{
url: "postgres://pqgotest:password@localhost/pqgotest?sslmode=verify-full",
}
if e, v := "pqgotest", c.Dbname(); v != e {
t.Errorf("Expected %q, got %q", e, v)
}

c = Config{
url: "root@tcp(host.myhost)/mysqltest?loc=Local",
}
if e, v := "mysqltest", c.Dbname(); v != e {
t.Errorf("Expected %q, got %q", e, v)
}

c = Config{
url: "/mysqltest?loc=Local&parseTime=true",
}
if e, v := "mysqltest", c.Dbname(); v != e {
t.Errorf("Expected %q, got %q", e, v)
}
}
12 changes: 7 additions & 5 deletions pkg/datastore/sqlstore/sqlstore.go
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"database/sql"
"errors"
"regexp"
"time"

_ "github.com/go-sql-driver/mysql" // mysql driver
Expand Down Expand Up @@ -34,15 +33,18 @@ var ErrNoResults = errors.New("datastore: query returned 0 results")

// New creates a new database pool
func New(c *Config) *sqlstore {
dbx, err := sqlx.Connect(c.Driver, c.DSN())
dsn := c.DSN()
dbx, err := sqlx.Connect(c.Driver, dsn)
if err != nil {
log.Fatalf("Error connecting to database: %s", err)
}
db := &sqlstore{dbx, c.Driver, c}

// write log statement, sanitize password
re := regexp.MustCompile(`password=[^ ]+`)
log.Printf("Connected to %s database: %s", c.Driver, re.ReplaceAllString(c.DSN(), `xxxxx`))
if c.Host == "" || c.Driver == SQLITE {
log.Printf("Connected to %s database: %s", c.Driver, c.Dbname())
} else {
log.Printf("Connected to %s database: %s on %s", c.Driver, c.Dbname(), c.Host)
}

// run migrations
db.Migrate()
Expand Down

0 comments on commit cb25b26

Please sign in to comment.