Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
clickhouse: add ExecuteMigration to driver.
spinningbot080fd5f18530
diff --git a/plugin/db/clickhouse/clickhouse.go b/plugin/db/clickhouse/clickhouse.goindex 5f369442ba..28f2517b9b 100644--- a/plugin/db/clickhouse/clickhouse.go+++ b/plugin/db/clickhouse/clickhouse.go@@ -288,8 +288,45 @@ func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {// ExecuteMigration will execute the migration for MySQL.func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo, statement string) (int64, string, error) {- // TODO(spinningbot): implement it.- return 0, "", nil+ insertHistoryQuery := `+ INSERT INTO bytebase.migration_history (+ id,+ created_by,+ created_ts,+ updated_by,+ updated_ts,+ release_version,+ namespace,+ sequence,+ engine,+ type,+ status,+ version,+ description,+ statement,+ ` + "`schema`," + `+ schema_prev,+ execution_duration,+ issue_id,+ payload+ )+ VALUES (?, now(), ?, now(), ?, ?, ?, ?, ?, 'PENDING', ?, ?, ?, ?, ?, 0, ?, ?)+`+ updateHistoryQuery := `+ ALTER TABLE+ bytebase.migration_history+ UPDATE+ status = 'DONE',+ execution_duration = ?,+ ` + "`schema` = ?" + `+ WHERE id = ?+ `+ args := util.MigrationExecutionArgs{+ InsertHistoryQuery: insertHistoryQuery,+ UpdateHistoryQuery: updateHistoryQuery,+ TablePrefix: "bytebase.",+ }+ return util.ExecuteMigration(ctx, db.MySQL, driver, m, statement, args)}func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {diff --git a/plugin/db/util/driverutil.go b/plugin/db/util/driverutil.goindex b846fd2e6c..6c881251c8 100644--- a/plugin/db/util/driverutil.go+++ b/plugin/db/util/driverutil.go@@ -107,7 +107,7 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *// MySQL runs DDL in its own transaction, so we can't commit migration history together with DDL in a single transaction.// Thus we sort of doing a 2-phase commit, where we first write a PENDING migration record, and after migration completes, we then// update the record to DONE together with the updated schema.- insertedId := int64(-1)+ insertedID := int64(-1)if dbType == db.Postgres {tx.QueryRowContext(ctx, args.InsertHistoryQuery,m.Creator,@@ -124,7 +124,44 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *prevSchemaBuf.String(),m.IssueId,m.Payload,- ).Scan(&insertedId)+ ).Scan(&insertedID)+ } else if dbType == db.ClickHouse {+ maxIDQuery := "SELECT MAX(id)+1 FROM bytebase.migration_history"+ rows, err := tx.QueryContext(ctx, maxIDQuery)+ if err != nil {+ return -1, "", FormatErrorWithQuery(err, maxIDQuery)+ }+ defer rows.Close()+ for rows.Next() {+ if err := rows.Scan(+ &insertedID,+ ); err != nil {+ return -1, "", FormatErrorWithQuery(err, maxIDQuery)+ }+ }+ if err := rows.Err(); err != nil {+ return -1, "", FormatErrorWithQuery(err, maxIDQuery)+ }+ _, err = tx.ExecContext(ctx, args.InsertHistoryQuery,+ insertedID,+ m.Creator,+ m.Creator,+ m.ReleaseVersion,+ m.Namespace,+ sequence,+ m.Engine,+ m.Type,+ m.Version,+ m.Description,+ statement,+ prevSchemaBuf.String(),+ prevSchemaBuf.String(),+ m.IssueId,+ m.Payload,+ )+ if err != nil {+ return -1, "", FormatErrorWithQuery(err, args.InsertHistoryQuery)+ }} else {res, err := tx.ExecContext(ctx, args.InsertHistoryQuery,m.Creator,@@ -147,7 +184,7 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *return -1, "", FormatErrorWithQuery(err, args.InsertHistoryQuery)}- insertedId, err = res.LastInsertId()+ insertedID, err = res.LastInsertId()if err != nil {return -1, "", FormatErrorWithQuery(err, args.InsertHistoryQuery)}@@ -197,7 +234,7 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *_, err = afterTx.ExecContext(ctx, args.UpdateHistoryQuery,duration,afterSchemaBuf.String(),- insertedId,+ insertedID,)if err != nil {@@ -208,7 +245,7 @@ func ExecuteMigration(ctx context.Context, dbType db.Type, driver db.Driver, m *return -1, "", err}- return insertedId, afterSchemaBuf.String(), nil+ return insertedID, afterSchemaBuf.String(), nil}func findBaseline(ctx context.Context, dbType db.Type, tx *sql.Tx, namespace, tablePrefix string) (bool, error) {