Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

clickhouse: add clickhouse driver skeleton.

spinningbot692b26498092
diff --git a/bin/server/main.go b/bin/server/main.go
index 7078baec2e..66a5a43181 100644
--- a/bin/server/main.go
+++ b/bin/server/main.go
@@ -6,6 +6,8 @@ import (
"github.com/bytebase/bytebase/bin/server/cmd"
+ // Register clickhouse driver
+ _ "github.com/bytebase/bytebase/plugin/db/clickhouse"
// Register mysql driver
_ "github.com/bytebase/bytebase/plugin/db/mysql"
// Register postgres driver
diff --git a/go.mod b/go.mod
index ba6c589652..a5f2ac6af0 100644
--- a/go.mod
+++ b/go.mod
@@ -3,6 +3,7 @@ module github.com/bytebase/bytebase
go 1.16
require (
+ github.com/ClickHouse/clickhouse-go v1.5.1 // indirect
github.com/VictoriaMetrics/fastcache v1.6.0
github.com/casbin/casbin/v2 v2.29.2
github.com/go-sql-driver/mysql v1.6.0
diff --git a/go.sum b/go.sum
index e6dcfefa72..fd1bae6f5e 100644
--- a/go.sum
+++ b/go.sum
@@ -43,6 +43,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
+github.com/ClickHouse/clickhouse-go v1.5.1 h1:I8zVFZTz80crCs0FFEBJooIxsPcV0xfthzK1YrkpJTc=
+github.com/ClickHouse/clickhouse-go v1.5.1/go.mod h1:EaI/sW7Azgz9UATzd5ZdZHRUhHgv5+JMS9NSr2smCJI=
github.com/Jeffail/gabs/v2 v2.5.1/go.mod h1:xCn81vdHKxFUuWWAaD5jCTQDNPBMh5pPs9IJ+NcziBI=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
@@ -79,6 +81,7 @@ github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+Ce
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
+github.com/bkaradzic/go-lz4 v1.0.0/go.mod h1:0YdlkowM3VswSROI7qDxhRvJ3sLhlFrRRwjwegp5jy4=
github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM=
github.com/blacktear23/go-proxyprotocol v0.0.0-20180807104634-af7a81e8dd0d/go.mod h1:VKt7CNAQxpFpSDz3sXyj9hY/GbVsQCr0sB3w59nE7lU=
github.com/casbin/casbin/v2 v2.1.0/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
@@ -98,6 +101,8 @@ github.com/chzyer/readline v0.0.0-20171208011716-f6d7a1f6fbf3/go.mod h1:nSuG5e5P
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
+github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58 h1:F1EaeKL/ta07PY/k9Os/UFtwERei2/XzGemhpGnBKNg=
+github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58/go.mod h1:EOBUe0h4xcZ5GoxqC5SDxFQ8gwyZPKQoEzownBlhI80=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
@@ -207,6 +212,7 @@ github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTM
github.com/go-playground/overalls v0.0.0-20180201144345-22ec1a223b7c/go.mod h1:UqxAgEOt89sCiXlrc/ycnx00LVvUO/eS8tMUkWX4R7w=
github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
+github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
@@ -366,6 +372,7 @@ github.com/jinzhu/now v1.0.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.3.0 h1:OS12ieG61fsCg5+qLJ+SsW9NicxNkg3b25OyT2yCeUc=
github.com/jmespath/go-jmespath v0.3.0/go.mod h1:9QtRXoHjLGCJ5IBSaohpXITPlowMeeYCZ7fLUTSywik=
+github.com/jmoiron/sqlx v1.2.0/go.mod h1:1FEQNm3xlJgrMD+FBdI9+xvCksHtbpVBBw5dYhBSsks=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
@@ -407,6 +414,7 @@ github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
+github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.1.1/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.10.2 h1:AqzbZs4ZoCBp+GtejcpCpcxM3zlSMx29dXbUSeVtJb8=
github.com/lib/pq v1.10.2/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
@@ -487,6 +495,7 @@ github.com/pelletier/go-toml v1.3.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUr
github.com/pelletier/go-toml v1.9.3/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5/go.mod h1:jvVRKCrJTQWu0XVbaOlby/2lO20uSCHEMzzplHXte1o=
github.com/phf/go-queue v0.0.0-20170504031614-9abe38d0371d/go.mod h1:lXfE4PvvTW5xOjO6Mba8zDPyw8M93B6AQ7frTGnMlA8=
+github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pingcap-incubator/tidb-dashboard v0.0.0-20200407064406-b2b8ad403d01/go.mod h1:77fCh8d3oKzC5ceOJWeZXAS/mLzVgdZ7rKniwmOyFuo=
github.com/pingcap-incubator/tidb-dashboard v0.0.0-20200514075710-eecc9a4525b5/go.mod h1:8q+yDx0STBPri8xS4A2duS1dAf+xO0cMtjwe0t6MWJk=
github.com/pingcap/br v0.0.0-20200426093517-dd11ae28b885/go.mod h1:4w3meMnk7HDNpNgjuRAxavruTeKJvUiXxoEWTjzXPnA=
diff --git a/plugin/db/clickhouse/clickhouse.go b/plugin/db/clickhouse/clickhouse.go
new file mode 100644
index 0000000000..ed76d9937b
--- /dev/null
+++ b/plugin/db/clickhouse/clickhouse.go
@@ -0,0 +1,167 @@
+package clickhouse
+
+import (
+ "bufio"
+ "context"
+ "database/sql"
+ _ "embed"
+ "fmt"
+ "io"
+ "strings"
+
+ clickhouse "github.com/ClickHouse/clickhouse-go"
+ "github.com/bytebase/bytebase/plugin/db"
+ "go.uber.org/zap"
+)
+
+//go:embed clickhouse_migration_schema.sql
+var migrationSchema string
+
+var (
+ _ db.Driver = (*Driver)(nil)
+)
+
+func init() {
+ db.Register(db.ClickHouse, newDriver)
+}
+
+type Driver struct {
+ l *zap.Logger
+ connectionCtx db.ConnectionContext
+ dbType db.Type
+
+ db *sql.DB
+}
+
+func newDriver(config db.DriverConfig) db.Driver {
+ return &Driver{
+ l: config.Logger,
+ }
+}
+
+func (driver *Driver) Open(ctx context.Context, dbType db.Type, config db.ConnectionConfig, connCtx db.ConnectionContext) (db.Driver, error) {
+ protocol := "tcp"
+ if strings.HasPrefix(config.Host, "/") {
+ protocol = "unix"
+ }
+
+ params := []string{}
+
+ port := config.Port
+ if port == "" {
+ port = "9000"
+ }
+ baseDSN := fmt.Sprintf("%s://%s:%s?", protocol, config.Host, port)
+
+ // Default user name is "default".
+ params = append(params, fmt.Sprintf("username=%s", config.Username))
+ // Password is constructed later, after loggedDSN is constructed.
+ if config.Database != "" {
+ params = append(params, fmt.Sprintf("database=%s", config.Database))
+ }
+ // Set SSL configuration.
+ tlsConfig, err := config.TlsConfig.GetSslConfig()
+ if err != nil {
+ return nil, fmt.Errorf("sql: tls config error: %v", err)
+ }
+ tlsKey := "db.clickhouse.tls"
+ if tlsConfig != nil {
+ if err := clickhouse.RegisterTLSConfig(tlsKey, tlsConfig); err != nil {
+ return nil, fmt.Errorf("sql: failed to register tls config: %v", err)
+ }
+ // TLS config is only used during sql.Open, so should be safe to deregister afterwards.
+ defer clickhouse.DeregisterTLSConfig(tlsKey)
+ params = append(params, fmt.Sprintf("tls_config=%s", tlsKey))
+ }
+
+ loggedDSN := fmt.Sprintf("%s%s&password=<<redacted password>>", baseDSN, strings.Join(params, "&"))
+
+ if config.Password != "" {
+ params = append(params, fmt.Sprintf("password=%s", config.Password))
+ }
+ dsn := fmt.Sprintf("%s%s", baseDSN, strings.Join(params, "&"))
+
+ driver.l.Debug("Opening ClickHouse driver",
+ zap.String("dsn", loggedDSN),
+ zap.String("environment", connCtx.EnvironmentName),
+ zap.String("database", connCtx.InstanceName),
+ )
+ db, err := sql.Open("clickhouse", dsn)
+ if err != nil {
+ panic(err)
+ }
+ driver.dbType = dbType
+ driver.db = db
+ driver.connectionCtx = connCtx
+
+ return driver, nil
+}
+
+func (driver *Driver) Close(ctx context.Context) error {
+ return driver.db.Close()
+}
+
+func (driver *Driver) Ping(ctx context.Context) error {
+ return driver.db.PingContext(ctx)
+}
+
+func (driver *Driver) GetDbConnection(ctx context.Context, database string) (*sql.DB, error) {
+ return driver.db, nil
+}
+
+func (driver *Driver) GetVersion(ctx context.Context) (string, error) {
+ // TODO(spinningbot): implement it.
+ return "", nil
+}
+
+func (driver *Driver) SyncSchema(ctx context.Context) ([]*db.DBUser, []*db.DBSchema, error) {
+ // TODO(spinningbot): implement it.
+ return nil, nil, nil
+}
+
+func (driver *Driver) Execute(ctx context.Context, statement string) error {
+ tx, err := driver.db.BeginTx(ctx, nil)
+ if err != nil {
+ return err
+ }
+ defer tx.Rollback()
+
+ _, err = tx.ExecContext(ctx, statement)
+
+ if err := tx.Commit(); err != nil {
+ return err
+ }
+
+ return err
+}
+
+// Migration related
+func (driver *Driver) NeedsSetupMigration(ctx context.Context) (bool, error) {
+ // TODO(spinningbot): implement it.
+ return false, nil
+}
+
+func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {
+ // TODO(spinningbot): implement it.
+ return nil
+}
+
+// ExecuteMigration will execute the migration for MySQL.
+func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo, statement string) (int64, string, error) {
+ // TODO(spinningbot): implement it.
+ return 0, "", nil
+}
+
+func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
+ return nil, nil
+}
+
+func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) error {
+ // TODO(spinningbot): implement it.
+ return nil
+}
+
+func (driver *Driver) Restore(ctx context.Context, sc *bufio.Scanner) (err error) {
+ // TODO(spinningbot): implement it.
+ return nil
+}
diff --git a/plugin/db/clickhouse/clickhouse_migration_schema.sql b/plugin/db/clickhouse/clickhouse_migration_schema.sql
new file mode 100644
index 0000000000..e69de29bb2