Skip to content

Commit

Permalink
Feat/add dockertest (#51)
Browse files Browse the repository at this point in the history
* refactor driver enum into database enum

* implement docker manager singleton

* remove duplicate testdata folder

* refactor global constants into iota
  • Loading branch information
kenji-yamane authored Jul 4, 2022
1 parent 889e2e9 commit 51f8b4f
Show file tree
Hide file tree
Showing 18 changed files with 429 additions and 18 deletions.
7 changes: 5 additions & 2 deletions cmd/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (

"github.com/migratemgr8/mgr8/domain"
"github.com/migratemgr8/mgr8/drivers"
"github.com/migratemgr8/mgr8/global"
"github.com/migratemgr8/mgr8/infrastructure"

"github.com/spf13/cobra"
)

var defaultDriverName = string(drivers.Postgres)
var defaultDriverName = "postgres"

type CommandExecutor interface {
execute(args []string, databaseURL string, migrationsDir string, driver domain.Driver, verbosity infrastructure.LogLevel) error
Expand All @@ -35,7 +36,9 @@ func (c *Command) Execute(cmd *cobra.Command, args []string) {
panic(err)
}

driver, err := drivers.GetDriver(c.driverName)
var d global.Database
d.FromStr(c.driverName)
driver, err := drivers.GetDriver(d)
if err != nil {
log.Fatal(err)
}
Expand Down
19 changes: 6 additions & 13 deletions drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,19 @@ package drivers

import (
"fmt"
"github.com/migratemgr8/mgr8/global"

"github.com/migratemgr8/mgr8/domain"
"github.com/migratemgr8/mgr8/drivers/mysql"
"github.com/migratemgr8/mgr8/drivers/postgres"
)

type DriverName string

const (
MySql DriverName = "mysql"
Postgres DriverName = "postgres"
)

func GetDriver(driverName string) (domain.Driver, error) {
driver := DriverName(driverName)
switch driver {
case Postgres:
func GetDriver(d global.Database) (domain.Driver, error) {
switch d {
case global.Postgres:
return postgres.NewPostgresDriver(), nil
case MySql:
case global.MySql:
return mysql.NewMySqlDriver(), nil
}
return nil, fmt.Errorf("inexistent driver %s", driverName)
return nil, fmt.Errorf("inexistent driver %s", d)
}
35 changes: 35 additions & 0 deletions global/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package global

import "sync"

type Database int

const (
Postgres Database = iota
MySql Database = iota
)

var toStr = map[Database]string{
Postgres: "postgres",
MySql: "mysql",
}

var fromStr map[string]Database

var initFromStrOnce sync.Once

func initFromStr() {
fromStr = make(map[string]Database, len(toStr))
for k, v := range toStr {
fromStr[v] = k
}
}

func (d Database) String() string {
return toStr[d]
}

func (d *Database) FromStr(s string) {
initFromStrOnce.Do(initFromStr)
*d = fromStr[s]
}
39 changes: 39 additions & 0 deletions global/database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package global

import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Check Command", func() {
var (
subject Database
)

Context("FromStr", func() {
When("not used", func() {
It("uses default value", func() {
Expect(subject).To(Equal(Postgres))
})
})

for k, v := range toStr {
When(fmt.Sprintf("str is %s", v), func() {
subject.FromStr(v)
It(fmt.Sprintf("updates to %s domain", v), func() {
Expect(subject).To(Equal(k))
})
})
}
})

Context("ToStr", func() {
for k, v := range toStr {
When(v, func() {
subject = k
Expect(fmt.Sprintf("%s", subject)).To(Equal(v))
})
}
})
})
23 changes: 22 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,46 @@ require (

require (
github.com/golang/mock v1.6.0
github.com/ory/dockertest/v3 v3.9.1
github.com/pingcap/parser v0.0.0-20210831085004-b5390aa83f65
github.com/pingcap/tidb v1.1.0-beta.0.20220418060002-fe5aa9760b58
go.uber.org/zap v1.21.0
)

require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/benbjohnson/clock v1.1.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 // indirect
github.com/danjacques/gofslock v0.0.0-20191023191349-0a45f885bc37 // indirect
github.com/docker/cli v20.10.14+incompatible // indirect
github.com/docker/docker v20.10.7+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/moby/term v0.0.0-20201216013528-df9cb8a40635 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.2 // indirect
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/pingcap/errors v0.11.5-0.20211224045212-9687c2b0f87c // indirect
github.com/pingcap/failpoint v0.0.0-20210316064728-7acb0f0a3dfd // indirect
Expand All @@ -54,18 +71,22 @@ require (
github.com/prometheus/procfs v0.0.8 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
github.com/shirou/gopsutil v3.21.3+incompatible // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tikv/client-go/v2 v2.0.0-alpha.0.20211221092530-2f48d74d6949 // indirect
github.com/tikv/pd v1.1.0-beta.0.20210818112400-0c5667766690 // indirect
github.com/uber/jaeger-client-go v2.22.1+incompatible // indirect
github.com/uber/jaeger-lib v2.4.1+incompatible // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
go.etcd.io/etcd v0.5.0-alpha.5.0.20200824191128-ae9734ed278b // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20200305110556-506484158171 // indirect
google.golang.org/grpc v1.29.1 // indirect
Expand Down
Loading

0 comments on commit 51f8b4f

Please sign in to comment.