Skip to content

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

clickhouse driver: support dump and restore schema.

spinningbote11d3fd28a7c
diff --git a/plugin/db/clickhouse/clickhouse.go b/plugin/db/clickhouse/clickhouse.go
index 1b80e46f96..1a049c49d9 100644
--- a/plugin/db/clickhouse/clickhouse.go
+++ b/plugin/db/clickhouse/clickhouse.go
@@ -10,6 +10,7 @@ import (
"strings"
clickhouse "github.com/ClickHouse/clickhouse-go"
+ "github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/plugin/db/util"
"go.uber.org/zap"
@@ -472,12 +473,233 @@ func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.Mig
return util.FindMigrationHistoryList(ctx, db.MySQL, driver, find, baseQuery)
}
+// Dump and restore
+const (
+ databaseHeaderFmt = "" +
+ "--\n" +
+ "-- ClickHouse database structure for `%s`\n" +
+ "--\n"
+ useDatabaseFmt = "USE `%s`;\n\n"
+ tableStmtFmt = "" +
+ "--\n" +
+ "-- Table structure for `%s`\n" +
+ "--\n" +
+ "%s;\n"
+ viewStmtFmt = "" +
+ "--\n" +
+ "-- View structure for `%s`\n" +
+ "--\n" +
+ "%s;\n"
+)
+
func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) error {
- // TODO(spinningbot): implement it.
+ txn, err := driver.db.BeginTx(ctx, &sql.TxOptions{})
+ if err != nil {
+ return err
+ }
+ defer txn.Rollback()
+
+ if err := dumpTxn(ctx, txn, database, out, schemaOnly); err != nil {
+ return err
+ }
+
+ if err := txn.Commit(); err != nil {
+ return err
+ }
+
+ return nil
+}
+
+// dumpTxn will dump the input database. schemaOnly isn't supported yet and true by default.
+func dumpTxn(ctx context.Context, txn *sql.Tx, database string, out io.Writer, schemaOnly bool) error {
+ // Find all dumpable databases
+ dbNames, err := getDatabases(txn)
+ if err != nil {
+ return fmt.Errorf("failed to get databases: %s", err)
+ }
+
+ var dumpableDbNames []string
+ if database != "" {
+ exist := false
+ for _, n := range dbNames {
+ if n == database {
+ exist = true
+ break
+ }
+ }
+ if !exist {
+ return common.Errorf(common.NotFound, fmt.Errorf("database %s not found", database))
+ }
+ dumpableDbNames = []string{database}
+ } else {
+ for _, dbName := range dbNames {
+ if systemDatabases[dbName] {
+ continue
+ }
+ dumpableDbNames = append(dumpableDbNames, dbName)
+ }
+ }
+
+ for _, dbName := range dumpableDbNames {
+ // Database header.
+ header := fmt.Sprintf(databaseHeaderFmt, dbName)
+ if _, err := io.WriteString(out, header); err != nil {
+ return err
+ }
+ // Include "USE DATABASE xxx" if dumping multiple databases.
+ if len(dumpableDbNames) > 1 {
+ dbStmt, err := getDatabaseStmt(txn, dbName)
+ if err != nil {
+ return fmt.Errorf("failed to get database %q: %s", dbName, err)
+ }
+ if _, err := io.WriteString(out, dbStmt); err != nil {
+ return err
+ }
+ // Use database statement.
+ useStmt := fmt.Sprintf(useDatabaseFmt, dbName)
+ if _, err := io.WriteString(out, useStmt); err != nil {
+ return err
+ }
+ }
+
+ // Table and view statement.
+ tables, err := getTables(txn, dbName)
+ if err != nil {
+ return fmt.Errorf("failed to get tables of database %q: %s", dbName, err)
+ }
+ for _, tbl := range tables {
+ if _, err := io.WriteString(out, fmt.Sprintf("%s\n", tbl.statement)); err != nil {
+ return err
+ }
+ }
+ }
+
return nil
}
+// getDatabases gets all databases of an instance.
+func getDatabases(txn *sql.Tx) ([]string, error) {
+ var dbNames []string
+ rows, err := txn.Query("SELECT name FROM system.databases")
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+
+ for rows.Next() {
+ var name string
+ if err := rows.Scan(&name); err != nil {
+ return nil, err
+ }
+ dbNames = append(dbNames, name)
+ }
+ return dbNames, nil
+}
+
+// getDatabaseStmt gets the create statement of a database.
+func getDatabaseStmt(txn *sql.Tx, dbName string) (string, error) {
+ query := fmt.Sprintf("SHOW CREATE DATABASE IF NOT EXISTS %s;", dbName)
+ rows, err := txn.Query(query)
+ if err != nil {
+ return "", err
+ }
+ defer rows.Close()
+
+ if rows.Next() {
+ var stmt, unused string
+ if err := rows.Scan(&unused, &stmt); err != nil {
+ return "", err
+ }
+ return fmt.Sprintf("%s;\n", stmt), nil
+ }
+ return "", fmt.Errorf("query %q returned empty row", query)
+}
+
+// tableSchema describes the schema of a table or view.
+type tableSchema struct {
+ name string
+ tableType string
+ statement string
+}
+
+// getTables gets all tables of a database.
+func getTables(txn *sql.Tx, dbName string) ([]*tableSchema, error) {
+ var tables []*tableSchema
+ query := fmt.Sprintf("SELECT name, engine, create_table_query FROM system.tables WHERE database='%s';", dbName)
+ rows, err := txn.Query(query)
+ if err != nil {
+ return nil, err
+ }
+ defer rows.Close()
+
+ for rows.Next() {
+ var tbl tableSchema
+ if err := rows.Scan(&tbl.name, &tbl.tableType, &tbl.statement); err != nil {
+ return nil, err
+ }
+ tables = append(tables, &tbl)
+ }
+
+ for _, tbl := range tables {
+ // Remove the database prefix from statement.
+ tbl.statement = strings.ReplaceAll(tbl.statement, fmt.Sprintf(" %s.%s ", dbName, tbl.name), fmt.Sprintf(" %s ", tbl.name))
+ if tbl.tableType == "View" {
+ tbl.statement = fmt.Sprintf(viewStmtFmt, tbl.name, tbl.statement)
+ } else {
+ tbl.statement = fmt.Sprintf(tableStmtFmt, tbl.name, tbl.statement)
+ }
+ }
+ return tables, nil
+}
+
func (driver *Driver) Restore(ctx context.Context, sc *bufio.Scanner) (err error) {
- // TODO(spinningbot): implement it.
+ txn, err := driver.db.BeginTx(ctx, nil)
+ if err != nil {
+ return err
+ }
+ defer txn.Rollback()
+
+ s := ""
+ delimiter := false
+ for sc.Scan() {
+ line := sc.Text()
+
+ execute := false
+ switch {
+ case s == "" && line == "":
+ continue
+ case strings.HasPrefix(line, "--"):
+ continue
+ case line == "DELIMITER ;;":
+ delimiter = true
+ continue
+ case line == "DELIMITER ;" && delimiter:
+ delimiter = false
+ execute = true
+ case strings.HasSuffix(line, ";"):
+ s = s + line + "\n"
+ if !delimiter {
+ execute = true
+ }
+ default:
+ s = s + line + "\n"
+ continue
+ }
+ if execute {
+ _, err := txn.Exec(s)
+ if err != nil {
+ return fmt.Errorf("execute query %q failed: %v", s, err)
+ }
+ s = ""
+ }
+ }
+ if err := sc.Err(); err != nil {
+ return err
+ }
+
+ if err := txn.Commit(); err != nil {
+ return err
+ }
+
return nil
}
diff --git a/server/issue.go b/server/issue.go
index 1a49756aea..405b538981 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -409,6 +409,8 @@ func (s *Server) CreateIssue(ctx context.Context, issueCreate *api.IssueCreate,
} else {
payload.Statement = fmt.Sprintf("CREATE DATABASE \"%s\" ENCODING %q LC_COLLATE %q", taskCreate.DatabaseName, taskCreate.CharacterSet, taskCreate.Collation)
}
+ case db.ClickHouse:
+ payload.Statement = fmt.Sprintf("CREATE DATABASE `%s`", taskCreate.DatabaseName)
}
bytes, err := json.Marshal(payload)
if err != nil {