Add datastore database driver for Hanzo Datastore
Registers "datastore://" as a URL scheme that delegates to the ClickHouse driver. This allows golang-migrate to natively connect to Hanzo Datastore instances without URL rewriting in scripts.
This commit is contained in:
@@ -8,3 +8,4 @@ vendor/
|
||||
.vscode/
|
||||
.idea
|
||||
dist/
|
||||
migrate
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
SOURCE ?= file go_bindata github github_ee bitbucket aws_s3 google_cloud_storage godoc_vfs gitlab
|
||||
DATABASE ?= postgres mysql redshift cassandra spanner cockroachdb yugabytedb clickhouse mongodb sqlserver firebird neo4j pgx pgx5 rqlite
|
||||
DATABASE ?= postgres mysql redshift cassandra spanner cockroachdb yugabytedb clickhouse datastore mongodb sqlserver firebird neo4j pgx pgx5 rqlite
|
||||
DATABASE_TEST ?= $(DATABASE) sqlite sqlite3 sqlcipher
|
||||
VERSION ?= $(shell git describe --tags 2>/dev/null | cut -c 2-)
|
||||
TEST_FLAGS ?=
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
// Package datastore implements the database.Driver interface for Hanzo Datastore
|
||||
// (a ClickHouse-compatible database engine). It registers the "datastore://" URL
|
||||
// scheme so that golang-migrate can connect to Datastore instances natively.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// import _ "github.com/golang-migrate/migrate/v4/database/datastore"
|
||||
//
|
||||
// Then use a URL like:
|
||||
//
|
||||
// datastore://host:9000?username=default&database=default&x-multi-statement=true
|
||||
package datastore
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/url"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4/database"
|
||||
"github.com/golang-migrate/migrate/v4/database/clickhouse"
|
||||
|
||||
_ "github.com/ClickHouse/clickhouse-go"
|
||||
)
|
||||
|
||||
func init() {
|
||||
database.Register("datastore", &Datastore{})
|
||||
}
|
||||
|
||||
// Datastore wraps the ClickHouse driver, registering under the "datastore" scheme.
|
||||
type Datastore struct {
|
||||
clickhouse.ClickHouse
|
||||
}
|
||||
|
||||
// Open rewrites the "datastore://" scheme to "clickhouse://" and delegates
|
||||
// to the underlying ClickHouse driver.
|
||||
func (d *Datastore) Open(dsn string) (database.Driver, error) {
|
||||
u, err := url.Parse(dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.Scheme = "clickhouse"
|
||||
return d.ClickHouse.Open(u.String())
|
||||
}
|
||||
|
||||
// WithInstance creates a new Datastore driver from an existing sql.DB connection.
|
||||
func WithInstance(conn *sql.DB, config *clickhouse.Config) (database.Driver, error) {
|
||||
return clickhouse.WithInstance(conn, config)
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
//go:build datastore
|
||||
|
||||
package cli
|
||||
|
||||
import (
|
||||
_ "github.com/ClickHouse/clickhouse-go"
|
||||
_ "github.com/golang-migrate/migrate/v4/database/datastore"
|
||||
)
|
||||
Reference in New Issue
Block a user