Skip to content

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

clickhouse driver: fix migration queries to be compatible with clickhouse.

spinningbotdd9233a2f455
diff --git a/plugin/db/clickhouse/clickhouse.go b/plugin/db/clickhouse/clickhouse.go
index 0e08cf5a48..cb782a78b3 100644
--- a/plugin/db/clickhouse/clickhouse.go
+++ b/plugin/db/clickhouse/clickhouse.go
@@ -320,7 +320,7 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
issue_id,
payload
)
- VALUES (?, now(), ?, now(), ?, ?, ?, ?, ?, 'PENDING', ?, ?, ?, ?, ?, 0, ?, ?)
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`
updateHistoryQuery := `
ALTER TABLE
@@ -336,7 +336,7 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,
UpdateHistoryQuery: updateHistoryQuery,
TablePrefix: "bytebase.",
}
- return util.ExecuteMigration(ctx, db.MySQL, driver, m, statement, args)
+ return util.ExecuteMigration(ctx, db.ClickHouse, driver, m, statement, args)
}
func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index 2db84adb5a..e0f4f7f3cb 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -414,6 +414,8 @@ func (p *QueryParams) QueryString() string {
return mysqlQuery(p.Names)
case TiDB:
return mysqlQuery(p.Names)
+ case ClickHouse:
+ return mysqlQuery(p.Names)
case Postgres:
return pgQuery(p.Names)
}
diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.go
index 4fb13f26ef..83ee867cc0 100644
--- a/plugin/db/util/driverutil.go
+++ b/plugin/db/util/driverutil.go
@@ -79,7 +79,8 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *
if err != nil {
return -1, "", err
}
- if version != nil {
+ // Clickhouse will always return non-nil version with empty string.
+ if version != nil && len(*version) > 0 {
return -1, "", common.Errorf(common.MigrationOutOfOrder, fmt.Errorf("database %q has already applied version %s which is higher than %s", m.Database, *version, m.Version))
}
@@ -142,20 +143,26 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *
if err := rows.Err(); err != nil {
return -1, "", FormatErrorWithQuery(err, maxIDQuery)
}
+ // Clickhouse sql driver doesn't support taking now() as prepared value.
+ now := time.Now().Unix()
_, err = tx.ExecContext(ctx, args.InsertHistoryQuery,
insertedID,
m.Creator,
+ now,
m.Creator,
+ now,
m.ReleaseVersion,
m.Namespace,
sequence,
m.Engine,
m.Type,
+ "PENDING",
m.Version,
m.Description,
statement,
prevSchemaBuf.String(),
prevSchemaBuf.String(),
+ 0,
m.IssueId,
m.Payload,
)
@@ -299,7 +306,7 @@ func checkOutofOrderVersion(ctx context.Context, dbType db.Type, tx *sql.Tx, nam
queryParams := &db.QueryParams{DatabaseType: dbType}
queryParams.AddParam("namespace", namespace)
queryParams.AddParam("engine", engine.String())
- queryParams.AddParam("? < version", version)
+ queryParams.AddParam("version > ?", version)
query := `
SELECT MIN(version) FROM ` +
tablePrefix + `migration_history ` +