Skip to content

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

clickhouse driver: handle multi-statement execution in Execute function because clickhouse itself doesn't support it.

spinningbote35dd1f77a49
diff --git a/plugin/db/clickhouse/clickhouse.go b/plugin/db/clickhouse/clickhouse.go
index 28f2517b9b..0e08cf5a48 100644
--- a/plugin/db/clickhouse/clickhouse.go
+++ b/plugin/db/clickhouse/clickhouse.go
@@ -238,7 +238,7 @@ func (driver *Driver) Execute(ctx context.Context, statement string) error {
}
defer tx.Rollback()
- _, err = tx.ExecContext(ctx, statement)
+ err = driver.ExecuteStatement(ctx, tx, statement)
if err := tx.Commit(); err != nil {
return err
@@ -247,6 +247,16 @@ func (driver *Driver) Execute(ctx context.Context, statement string) error {
return err
}
+func (driver *Driver) ExecuteStatement(ctx context.Context, tx *sql.Tx, statement string) error {
+ stmts := db.SplitStatements(statement)
+ for _, stmt := range stmts {
+ if _, err := tx.ExecContext(ctx, stmt); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
// Migration related
func (driver *Driver) NeedsSetupMigration(ctx context.Context) (bool, error) {
const query = `
@@ -269,7 +279,7 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {
zap.String("environment", driver.connectionCtx.EnvironmentName),
zap.String("database", driver.connectionCtx.InstanceName),
)
- if _, err := driver.db.ExecContext(ctx, migrationSchema); err != nil {
+ if err := driver.Execute(ctx, migrationSchema); err != nil {
driver.l.Error("Failed to initialize migration schema.",
zap.Error(err),
zap.String("environment", driver.connectionCtx.EnvironmentName),
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index bafe073037..2db84adb5a 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -419,3 +419,17 @@ func (p *QueryParams) QueryString() string {
}
return ""
}
+
+// SplitStatements will split the sql statements into multiple statement parts.
+// TODO(spinningbot): making this more robust.
+func SplitStatements(stmt string) []string {
+ stmts := strings.Split(stmt, ";")
+ var ret []string
+ for _, stmt := range stmts {
+ stmt = strings.Trim(stmt, " \n")
+ if len(stmt) > 0 {
+ ret = append(ret, stmt)
+ }
+ }
+ return ret
+}
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index 6c881251c8..4fb13f26ef 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -208,8 +208,7 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *
sqldb = d
}
// MySQL executes DDL in its own transaction, so there is no need to supply a transaction.
- _, err = sqldb.ExecContext(ctx, statement)
- if err != nil {
+ if err = driver.Execute(ctx, statement); err != nil {
return -1, "", formatError(err)
}
}