-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add clickhouse support (#166)
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
1 parent
0114b1d
commit d70fdc7
Showing
14 changed files
with
319 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
_obj | ||
_test | ||
.vscode | ||
.idea | ||
|
||
# Architecture specific extensions/prefixes | ||
*.[568vq] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.