Skip to content

Commit

Permalink
feature: add clickhouse support (#166)
Browse files Browse the repository at this point in the history
Closes #51
Closes #81
Closes #115
Closes #162

Co-authored-by: Denis Titusov <[email protected]>
Co-authored-by: shumorkiniv <[email protected]>
Co-authored-by: Aurélien SCHILTZ <[email protected]>
  • Loading branch information
4 people authored May 1, 2023
1 parent 0114b1d commit d70fdc7
Show file tree
Hide file tree
Showing 14 changed files with 319 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
_obj
_test
.vscode
.idea

# Architecture specific extensions/prefixes
*.[568vq]
Expand Down
1 change: 1 addition & 0 deletions .sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ MYSQL_CONN_STRING="root:@/testfixtures_test?multiStatements=true"
SQLITE_CONN_STRING="testdb.sqlite3"
SQLSERVER_CONN_STRING="server=localhost\SQLExpress;database=testfixtures_test;user id=sa;password=sqlserver;encrypt=disable"
CRDB_CONN_STRING="host=cockroachdb user=root dbname=defaultdb port=26257 sslmode=disable"
CLICKHOUSE_CONN_STRING="clickhouse://clickhouse:clickhouse@clickhouse:9000/testfixtures_test?debug=false"
8 changes: 7 additions & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ tasks:
- task: test-db
vars: {DATABASE: cockroachdb}

test:clickhouse:
desc: Test Clickhouse
cmds:
- task: test-db
vars: {DATABASE: clickhouse}

test-db:
cmds:
- go test -v -tags {{.DATABASE}}
Expand All @@ -67,4 +73,4 @@ tasks:
docker:test:
cmds:
- docker-compose down -v
- docker-compose run testfixtures go test -v -tags 'postgresql sqlite mysql sqlserver cockroachdb'
- docker-compose run testfixtures go test -v -tags 'postgresql sqlite mysql sqlserver cockroachdb clickhouse'
99 changes: 99 additions & 0 deletions clickhouse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package testfixtures

import (
"database/sql"
"fmt"

_ "github.com/ClickHouse/clickhouse-go/v2"
)

type clickhouse struct {
baseHelper

cleanTableFn func(string) string
}

func (h *clickhouse) init(_ *sql.DB) error {
if h.cleanTableFn == nil {
h.cleanTableFn = func(tableName string) string {
return fmt.Sprintf("TRUNCATE TABLE %s", tableName)
}
}

return nil
}

func (*clickhouse) paramType() int {
return paramTypeDollar
}

func (*clickhouse) databaseName(q queryable) (string, error) {
var dbName string
err := q.QueryRow("SELECT DATABASE()").Scan(&dbName)
return dbName, err
}

func (h *clickhouse) tableNames(q queryable) ([]string, error) {
query := `
SELECT name
FROM system.tables
WHERE database = $1;
`
dbName, err := h.databaseName(q)
if err != nil {
return nil, err
}

rows, err := q.Query(query, dbName)
if err != nil {
return nil, err
}
defer func() {
_ = rows.Close()
}()

var tables []string
for rows.Next() {
var table string
if err = rows.Scan(&table); err != nil {
return nil, err
}
tables = append(tables, table)
}
if err = rows.Err(); err != nil {
return nil, err
}
return tables, nil

}

func (h *clickhouse) disableReferentialIntegrity(db *sql.DB, loadFn loadFunction) (err error) {
tx, err := db.Begin()
if err != nil {
return err
}
defer func() {
_ = tx.Rollback()
}()

err = loadFn(tx)
if err != nil {
return err
}

return tx.Commit()
}

// splitter is a batchSplitter interface implementation. We need it for
// ClickHouseDB because clickhouse doesn't support multi-statements.
func (*clickhouse) splitter() []byte {
return []byte(";\n")
}

func (h *clickhouse) cleanTableQuery(tableName string) string {
if h.cleanTableFn == nil {
return h.baseHelper.cleanTableQuery(tableName)
}

return h.cleanTableFn(tableName)
}
20 changes: 20 additions & 0 deletions clickhouse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build clickhouse
// +build clickhouse

package testfixtures

import (
"os"
"testing"

_ "github.com/ClickHouse/clickhouse-go/v2"
)

func TestClickhouse(t *testing.T) {
testLoader(
t,
"clickhouse",
os.Getenv("CLICKHOUSE_CONN_STRING"),
"testdata/schema/clickhouse.sql",
)
}
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ services:
- mysql
- sqlserver
- cockroachdb
- clickhouse
environment:
PGPASSWORD: postgres
PG_CONN_STRING: host=postgresql user=postgres dbname=testfixtures_test port=5432 sslmode=disable
Expand All @@ -20,6 +21,8 @@ services:

CRDB_CONN_STRING: host=cockroachdb user=root dbname=defaultdb port=26257 sslmode=disable

CLICKHOUSE_CONN_STRING: clickhouse://clickhouse:clickhouse@clickhouse:9000/testfixtures_test?debug=false

postgresql:
image: postgres:12.1-alpine
environment:
Expand All @@ -42,3 +45,13 @@ services:
cockroachdb:
image: cockroachdb/cockroach:v20.1.3
command: start-single-node --store /cockroach-data --insecure --advertise-host cockroachdb

clickhouse:
image: yandex/clickhouse-server:22-alpine
ports:
- 9010:9000
- 8133:8123
environment:
CLICKHOUSE_DB: testfixtures_test
CLICKHOUSE_USER: clickhouse
CLICKHOUSE_PASSWORD: clickhouse
8 changes: 1 addition & 7 deletions dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,7 @@ func (d *Dumper) Dump() error {
func (d *Dumper) dumpTable(table string) error {
query := fmt.Sprintf("SELECT * FROM %s", d.helper.quoteKeyword(table))

stmt, err := d.db.Prepare(query)
if err != nil {
return err
}
defer stmt.Close()

rows, err := stmt.Query()
rows, err := d.db.Query(query)
if err != nil {
return err
}
Expand Down
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/go-testfixtures/testfixtures/v3

require (
github.com/ClickHouse/clickhouse-go/v2 v2.9.1
github.com/denisenkom/go-mssqldb v0.12.3
github.com/go-sql-driver/mysql v1.7.1
github.com/jackc/pgx/v4 v4.18.1
Expand All @@ -12,16 +13,30 @@ require (
)

require (
github.com/ClickHouse/ch-go v0.55.0 // indirect
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/go-faster/city v1.0.1 // indirect
github.com/go-faster/errors v0.6.1 // indirect
github.com/golang-sql/civil v0.0.0-20190719163853-cb61b32ac6fe // indirect
github.com/golang-sql/sqlexp v0.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.2 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/paulmach/orb v0.9.0 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
go.opentelemetry.io/otel v1.15.0 // indirect
go.opentelemetry.io/otel/trace v1.15.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.7.0 // indirect
)

Expand Down
Loading

0 comments on commit d70fdc7

Please sign in to comment.