Skip to content

Commit

Permalink
feat: Add more gorm-driver (#138)
Browse files Browse the repository at this point in the history
* Update go.yml

* feat: otgorm add more driver

* fix: Hound Check

* test: undo too many grom drivers tests

* test: test actions/cache@v2

Co-authored-by: 谷溪 <guxi99@gmail.com>
  • Loading branch information
GGXXLL and Reasno authored Jun 4, 2021
1 parent 0a8a387 commit ec2c97c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 48 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ jobs:
ports:
- 9000:9000
options: --name minio-server

steps:
- uses: actions/checkout@v2

Expand All @@ -75,10 +76,19 @@ jobs:
with:
go-version: ${{ matrix.go-version }}

- uses: actions/cache@v2
id: cache-go-mod
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ matrix.go-version }}-${{ hashFiles('go.mod') }}
restore-keys: |
${{ runner.os }}-go-${{ matrix.go-version }}
- name: Environment Variables from Dotenv
uses: c-py/action-dotenv-to-setenv@v3

- name: Build
if: steps.cache-go-mod.outputs.cache-hit != 'true'
run: go build -v ./...

- name: Test
Expand Down
5 changes: 3 additions & 2 deletions config/remote/remote_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func TestError(t *testing.T) {
)

cfg := &clientv3.Config{
Endpoints: []string{},
Endpoints: []string{},
DialTimeout: 2 * time.Second,
}

r = Provider("config.yaml", cfg)
Expand All @@ -88,7 +89,7 @@ func TestError(t *testing.T) {
assert.Error(t, err)

cfg = &clientv3.Config{
Endpoints: []string{os.Getenv("ETCD_ADDR")},
Endpoints: envDefaultEtcdAddrs,
DialTimeout: 2 * time.Second,
}
r = Provider("config-test1", cfg)
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/DoNewsCode/core
go 1.14

require (
github.com/ClickHouse/clickhouse-go v1.4.5 // indirect
github.com/HdrHistogram/hdrhistogram-go v1.0.1 // indirect
github.com/Reasno/ifilter v0.1.2
github.com/aws/aws-sdk-go v1.37.16
Expand All @@ -17,6 +18,7 @@ require (
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/hashicorp/go-multierror v1.1.0
github.com/hashicorp/go-version v1.3.0 // indirect
github.com/heptiolabs/healthcheck v0.0.0-20180807145615-6ff867650f40
github.com/klauspost/compress v1.12.2 // indirect
github.com/knadh/koanf v0.15.0
Expand All @@ -40,15 +42,17 @@ require (
go.uber.org/atomic v1.7.0
go.uber.org/dig v1.10.0
go.uber.org/zap v1.16.0
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2 // indirect
golang.org/x/text v0.3.5 // indirect
google.golang.org/grpc v1.35.0
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
gorm.io/driver/clickhouse v0.1.0
gorm.io/driver/mysql v1.0.4
gorm.io/driver/postgres v1.1.0
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.20.12
gorm.io/driver/sqlserver v1.0.7
gorm.io/gorm v1.21.10
)
66 changes: 46 additions & 20 deletions go.sum

Large diffs are not rendered by default.

15 changes: 11 additions & 4 deletions otgorm/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ import (
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/opentracing/opentracing-go"
"gorm.io/driver/clickhouse"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)
Expand Down Expand Up @@ -136,11 +139,15 @@ type databaseOut struct {
// provideDialector provides a gorm.Dialector. Mean to be used as an intermediate
// step to create *gorm.DB
func provideDialector(conf *databaseConf) (gorm.Dialector, error) {
if conf.Database == "mysql" {
return mysql.Open(conf.Dsn), nil
drivers := map[string]func(dsn string) gorm.Dialector{
"mysql": mysql.Open,
"sqlite": sqlite.Open,
"postgres": postgres.Open,
"sqlserver": sqlserver.Open,
"clickhouse": clickhouse.Open,
}
if conf.Database == "sqlite" {
return sqlite.Open(conf.Dsn), nil
if driver, ok := drivers[conf.Database]; ok {
return driver(conf.Dsn), nil
}
return nil, fmt.Errorf("unknow database type %s", conf.Database)
}
Expand Down
42 changes: 22 additions & 20 deletions otgorm/dependency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,29 @@ import (
)

func TestProvideDBFactory(t *testing.T) {
gorms := map[string]databaseConf{
"default": {
Database: "sqlite",
Dsn: ":memory:",
},
"alternative": {
Database: "mysql",
Dsn: envDefaultMysqlDsn,
},
}
factory, cleanup := provideDBFactory(databaseIn{
Conf: config.MapAdapter{"gorm": map[string]databaseConf{
"default": {
Database: "sqlite",
Dsn: ":memory:",
},
"alternative": {
Database: "mysql",
Dsn: envDefaultMysqlDsn,
},
}},
Conf: config.MapAdapter{"gorm": gorms},
Logger: log.NewNopLogger(),
Tracer: nil,
})
alt, err := factory.Make("alternative")
assert.NoError(t, err)
assert.NotNil(t, alt)
def, err := factory.Make("default")
assert.NoError(t, err)
assert.NotNil(t, def)
cleanup()
defer cleanup()
for driverName := range gorms {
t.Run(driverName, func(t *testing.T) {
db, err := factory.Make(driverName)
assert.NoError(t, err)
assert.NotNil(t, db)
})
}
}

func TestGorm(t *testing.T) {
Expand All @@ -44,9 +46,9 @@ func TestGorm(t *testing.T) {
d1 Maker,
d2 Factory,
d3 struct {
di.In
Cfg []config.ExportedConfig `group:"config"`
},
di.In
Cfg []config.ExportedConfig `group:"config"`
},
d4 *gorm.DB,
) {
a := assert.New(t)
Expand Down
1 change: 1 addition & 0 deletions otgorm/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func TestMain(m *testing.M) {
fmt.Println("Set env MYSQL_DSN to run otgorm tests")
os.Exit(0)
}

os.Exit(m.Run())
}

Expand Down

0 comments on commit ec2c97c

Please sign in to comment.