Skip to content

Commit

Permalink
fix: clarify env usage (#174)
Browse files Browse the repository at this point in the history
* fix: clarify env usage

* fix: setup test correctly

* refactor: move drivers to a new type

* refactor: move drivers to a new type
  • Loading branch information
Reasno authored Aug 4, 2021
1 parent d2abd07 commit 2dcbb5a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
21 changes: 7 additions & 14 deletions otgorm/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ import (
"github.com/DoNewsCode/core/di"
"github.com/go-kit/kit/log"
"github.com/opentracing/opentracing-go"
"gorm.io/driver/clickhouse"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
Expand Down Expand Up @@ -91,11 +88,11 @@ type factoryIn struct {

Conf contract.ConfigAccessor
Logger log.Logger
GormConfigInterceptor GormConfigInterceptor `optional:"true"`
Tracer opentracing.Tracer `optional:"true"`
Gauges *Gauges `optional:"true"`
Dispatcher contract.Dispatcher `optional:"true"`
Drivers map[string]func(dsn string) gorm.Dialector `optional:"true"`
GormConfigInterceptor GormConfigInterceptor `optional:"true"`
Tracer opentracing.Tracer `optional:"true"`
Gauges *Gauges `optional:"true"`
Dispatcher contract.Dispatcher `optional:"true"`
Drivers Drivers `optional:"true"`
}

// databaseOut is the result of provideDatabaseFactory. *gorm.DB is not a interface
Expand All @@ -110,7 +107,7 @@ type databaseOut struct {

// provideDialector provides a gorm.Dialector. Mean to be used as an intermediate
// step to create *gorm.DB
func provideDialector(conf *databaseConf, drivers map[string]func(dsn string) gorm.Dialector) (gorm.Dialector, error) {
func provideDialector(conf *databaseConf, drivers Drivers) (gorm.Dialector, error) {
if driver, ok := drivers[conf.Database]; ok {
return driver(conf.Dsn), nil
}
Expand Down Expand Up @@ -201,11 +198,7 @@ func provideDBFactory(p factoryIn) (Factory, func()) {
return di.Pair{}, fmt.Errorf("database configuration %s not valid: %w", name, err)
}
if p.Drivers == nil {
p.Drivers = map[string]func(dsn string) gorm.Dialector{
"mysql": mysql.Open,
"sqlite": sqlite.Open,
"clickhouse": clickhouse.Open,
}
p.Drivers = getDefaultDrivers()
}
dialector, err := provideDialector(&conf, p.Drivers)
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions otgorm/drivers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package otgorm

import (
"gorm.io/driver/clickhouse"
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

// Drivers is a map of string names and gorm.Dialector constructors. Inject Drivers to DI container to customize dialectors.
type Drivers map[string]func(dsn string) gorm.Dialector

func getDefaultDrivers() Drivers {
return map[string]func(dsn string) gorm.Dialector{
"mysql": mysql.Open,
"sqlite": sqlite.Open,
"clickhouse": clickhouse.Open,
}
}

0 comments on commit 2dcbb5a

Please sign in to comment.