Skip to content

Commit

Permalink
Enable configurable support for postgres
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Balogh <[email protected]>
  • Loading branch information
javaducky committed Jan 13, 2024
1 parent 4a1d78c commit 2f4281f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 3 deletions.
2 changes: 2 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
Expand Down
6 changes: 3 additions & 3 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2f4281f

Please sign in to comment.