From cb25b267da598b6a0b795121b7670c89ac659965 Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Fri, 2 Nov 2018 12:30:34 +0100 Subject: [PATCH] show only database name and maybe host when logging successful db connection. #174 --- pkg/datastore/sqlstore/config.go | 67 ++++++++++++++++----------- pkg/datastore/sqlstore/config_test.go | 25 ++++++++++ pkg/datastore/sqlstore/sqlstore.go | 12 +++-- 3 files changed, 73 insertions(+), 31 deletions(-) diff --git a/pkg/datastore/sqlstore/config.go b/pkg/datastore/sqlstore/config.go index 68bbe2c7..f2941511 100644 --- a/pkg/datastore/sqlstore/config.go +++ b/pkg/datastore/sqlstore/config.go @@ -1,6 +1,7 @@ package sqlstore import ( + "regexp" "strings" mysql "github.com/go-sql-driver/mysql" @@ -8,12 +9,12 @@ import ( 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 { @@ -21,49 +22,63 @@ func (c *Config) 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 "" +} diff --git a/pkg/datastore/sqlstore/config_test.go b/pkg/datastore/sqlstore/config_test.go index c5e5bf57..8e6f2c9d 100644 --- a/pkg/datastore/sqlstore/config_test.go +++ b/pkg/datastore/sqlstore/config_test.go @@ -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) + } +} diff --git a/pkg/datastore/sqlstore/sqlstore.go b/pkg/datastore/sqlstore/sqlstore.go index 739944d4..c08ca123 100644 --- a/pkg/datastore/sqlstore/sqlstore.go +++ b/pkg/datastore/sqlstore/sqlstore.go @@ -4,7 +4,6 @@ import ( "context" "database/sql" "errors" - "regexp" "time" _ "github.com/go-sql-driver/mysql" // mysql driver @@ -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()