Skip to content

Commit

Permalink
db: add support for cockroachdb
Browse files Browse the repository at this point in the history
add support for cockroachdb, related to ory/hydra#990
  • Loading branch information
lopezator committed Mar 21, 2019
1 parent e8fce87 commit 26b1b9d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
7 changes: 6 additions & 1 deletion dbal/canonicalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ const (
// DriverMySQL is the mysql driver name.
DriverMySQL = "mysql"

// DriverPostgreSQL is the mysql driver name.
// DriverPostgreSQL is the postgres driver name.
DriverPostgreSQL = "postgres"

// DriverPostgreSQL is the cockroach driver name.
DriverCockroachDB = "crdb"

// UnknownDriver is the driver name if the driver is unknown.
UnknownDriver = "unknown"
)
Expand All @@ -20,6 +23,8 @@ func Canonicalize(database string) string {
return DriverMySQL
case "pgx", "pq", "postgres":
return DriverPostgreSQL
case "crdb", "cockroach", "cockroachdb":
return DriverCockroachDB
default:
return UnknownDriver
}
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a
github.com/golang/gddo v0.0.0-20180828051604-96d2a289f41e/go.mod h1:xEhNfoBDX1hzLm2Nf80qUvZ2sVwoMZ8d6IE2SrsQfh4=
github.com/golang/gddo v0.0.0-20181116215533-9bd4a3295021 h1:HYV500jCgk+IC68L5sWrLFIWMpaUFfXXpJSAb7XOoBk=
github.com/golang/gddo v0.0.0-20181116215533-9bd4a3295021/go.mod h1:xEhNfoBDX1hzLm2Nf80qUvZ2sVwoMZ8d6IE2SrsQfh4=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/lint v0.0.0-20180702182130-06c8688daad7/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
Expand All @@ -260,6 +261,7 @@ github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OI
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.0 h1:Jf4mxPC/ziBnoPIdpQdPJ9OeiomAUHLvxmPRSPH9m4s=
github.com/google/uuid v1.1.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go v2.0.0+incompatible h1:j0GKcs05QVmm7yesiZq2+9cxHkNK9YM6zKx4D2qucQU=
github.com/googleapis/gax-go v2.0.0+incompatible/go.mod h1:SFVmujtThgffbyetf+mdk2eWhX2bMyUtNHzFKcPA9HY=
github.com/googleapis/gax-go/v2 v2.0.3 h1:siORttZ36U2R/WjiJuDz8znElWBiAlO9rVt+mqJt0Cc=
github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE08qbEPm1M08qg=
Expand Down
25 changes: 25 additions & 0 deletions sqlcon/dockertest/test_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,31 @@ func ConnectToTestMySQL() (*sqlx.DB, error) {
return db, nil
}

// ConnectToTestCockroachDB connects to a CockroachDB database.
func ConnectToTestCockroachDB() (*sqlx.DB, error) {
if url := os.Getenv("TEST_DATABASE_COCKROACHDB"); url != "" {
log.Println("Found cockroachdb test database config, skipping dockertest...")
return connect("postgres", url)
}

pool, err := dockertest.NewPool("")
if err != nil {
return nil, errors.Wrap(err, "Could not connect to docker")
}

resource, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "cockroachdb/cockroach",
Tag: "v2.1.6",
Cmd: []string{"start --insecure"},
})
if err != nil {
return nil, errors.Wrap(err, "Could not start resource")
}

db := bootstrap("postgres://root@localhost:%s/defaultdb?sslmode=disable", "26257/tcp", "postgres", pool, resource)
return db, nil
}

func bootstrap(u, port, d string, pool *dockertest.Pool, resource *dockertest.Resource) (db *sqlx.DB) {
if err := resilience.Retry(logrus.New(), time.Second*5, time.Minute*5, func() error {
var err error
Expand Down

0 comments on commit 26b1b9d

Please sign in to comment.