diff --git a/config.yaml b/config.yaml index 44f823d..01d3a33 100644 --- a/config.yaml +++ b/config.yaml @@ -1,2 +1,4 @@ DatabaseURI: "/Users/paul/Develop/weesvc-gorilla/gorm.db" +# Specify dialect as "postgres" or "sqlite3". "sqlite3" is default. +#Dialect: sqlite3 #Verbose: true diff --git a/db/config.go b/db/config.go index f6e1c0c..d59358c 100644 --- a/db/config.go +++ b/db/config.go @@ -8,13 +8,16 @@ import ( // Config provides database connection information. type Config struct { + Dialect string DatabaseURI string Verbose bool } // InitConfig initializes the database configuration from external settings. func InitConfig() (*Config, error) { + viper.SetDefault("Dialect", "sqlite3") config := &Config{ + Dialect: viper.GetString("Dialect"), DatabaseURI: viper.GetString("DatabaseURI"), Verbose: viper.GetBool("Verbose"), } diff --git a/db/db.go b/db/db.go index fa157ca..95377f3 100644 --- a/db/db.go +++ b/db/db.go @@ -2,7 +2,7 @@ package db import ( "github.com/jinzhu/gorm" - // Notes the database dialect to be used + _ "github.com/jinzhu/gorm/dialects/postgres" _ "github.com/jinzhu/gorm/dialects/sqlite" "github.com/pkg/errors" ) @@ -14,9 +14,9 @@ type Database struct { // New creates a new instance of the data access object given configuration settings. func New(config *Config) (*Database, error) { - db, err := gorm.Open("sqlite3", config.DatabaseURI) + db, err := gorm.Open(config.Dialect, config.DatabaseURI) if err != nil { - return nil, errors.Wrap(err, "unable to connect to database") + return nil, errors.Wrapf(err, "unable to connect to %s database", config.Dialect) } db.LogMode(config.Verbose) diff --git a/go.mod b/go.mod index 7d57c34..37e3498 100644 --- a/go.mod +++ b/go.mod @@ -19,6 +19,7 @@ require ( github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jinzhu/inflection v1.0.0 // indirect + github.com/lib/pq v1.1.1 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect