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: implemenet driver setup migration.
spinningbot582c4724e9b0
diff --git a/plugin/db/clickhouse/clickhouse.go b/plugin/db/clickhouse/clickhouse.goindex 086498ba9a..5f369442ba 100644--- a/plugin/db/clickhouse/clickhouse.go+++ b/plugin/db/clickhouse/clickhouse.go@@ -249,12 +249,40 @@ func (driver *Driver) Execute(ctx context.Context, statement string) error {// Migration relatedfunc (driver *Driver) NeedsSetupMigration(ctx context.Context) (bool, error) {- // TODO(spinningbot): implement it.- return false, nil+ const query = `+ SELECT+ 1+ FROM system.tables+ WHERE database = 'bytebase' AND name = 'migration_history'+ `+ return util.NeedsSetupMigrationSchema(ctx, driver.db, query)}func (driver *Driver) SetupMigrationIfNeeded(ctx context.Context) error {- // TODO(spinningbot): implement it.+ setup, err := driver.NeedsSetupMigration(ctx)+ if err != nil {+ return nil+ }++ if setup {+ driver.l.Info("Bytebase migration schema not found, creating schema...",+ zap.String("environment", driver.connectionCtx.EnvironmentName),+ zap.String("database", driver.connectionCtx.InstanceName),+ )+ if _, err := driver.db.ExecContext(ctx, migrationSchema); err != nil {+ driver.l.Error("Failed to initialize migration schema.",+ zap.Error(err),+ zap.String("environment", driver.connectionCtx.EnvironmentName),+ zap.String("database", driver.connectionCtx.InstanceName),+ )+ return util.FormatErrorWithQuery(err, migrationSchema)+ }+ driver.l.Info("Successfully created migration schema.",+ zap.String("environment", driver.connectionCtx.EnvironmentName),+ zap.String("database", driver.connectionCtx.InstanceName),+ )+ }+return nil}@@ -265,7 +293,29 @@ func (driver *Driver) ExecuteMigration(ctx context.Context, m *db.MigrationInfo,}func (driver *Driver) FindMigrationHistoryList(ctx context.Context, find *db.MigrationHistoryFind) ([]*db.MigrationHistory, error) {- return nil, nil+ baseQuery := `+ SELECT+ 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+ FROM bytebase.migration_history `+ return util.FindMigrationHistoryList(ctx, db.MySQL, driver, find, baseQuery)}func (driver *Driver) Dump(ctx context.Context, database string, out io.Writer, schemaOnly bool) error {diff --git a/plugin/db/clickhouse/clickhouse_migration_schema.sql b/plugin/db/clickhouse/clickhouse_migration_schema.sqlindex e69de29bb2..0b00807521 100644--- a/plugin/db/clickhouse/clickhouse_migration_schema.sql+++ b/plugin/db/clickhouse/clickhouse_migration_schema.sql@@ -0,0 +1,42 @@+-- This is the bytebase schema to track migration info for MySQL+-- Create a database called bytebase+CREATE DATABASE bytebase;++-- Create migration_history table+-- Note, we don't create trigger to update created_ts and updated_ts because that may causes error:+-- ERROR 1419 (HY000): You do not have the SUPER privilege and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)+CREATE TABLE bytebase.migration_history (+ id INTEGER,+ created_by TEXT NOT NULL,+ created_ts BIGINT NOT NULL,+ updated_by TEXT NOT NULL,+ updated_ts BIGINT NOT NULL,+ -- Record the client version creating this migration history. For Bytebase, we use its binary release version. Different Bytebase release might+ -- record different history info and thie field helps to handle such situation properly. Moreover, it helps debugging.+ release_version TEXT NOT NULL,+ -- Allows granular tracking of migration history (e.g If an application manages schemas for a multi-tenant service and each tenant has its own schema, that application can use namespace to record the tenant name to track the per-tenant schema migration)+ -- Since bytebase also manages different application databases from an instance, it leverages this field to track each database migration history.+ namespace TEXT NOT NULL,+ -- Used to detect out of order migration together with 'namespace' and 'version' column.+ sequence INTEGER UNSIGNED NOT NULL,+ -- We call it engine because maybe we could load history from other migration tool.+ `engine` ENUM('UI' = 0, 'VCS' = 1) NOT NULL,+ `type` ENUM('BASELINE' = 0, 'MIGRATE' = 1, 'BRANCH' = 2) NOT NULL,+ -- MySQL runs DDL in its own transaction, so we can't record DDL and migration_history into a single transaction.+ -- Thus, we create a "PENDING" record before applying the DDL and update that record to "DONE" after applying the DDL.+ `status` ENUM('PENDING' = 0, 'DONE' = 1) NOT NULL,+ -- Record the migration version.+ version TEXT NOT NULL,+ description TEXT NOT NULL,+ -- Record the migration statement+ statement TEXT NOT NULL,+ -- Record the schema after migration+ `schema` TEXT NOT NULL,+ -- Record the schema before migration. Though we could also fetch it from the previous migration history, it would complicate fetching logic.+ -- Besides, by storing the schema_prev, we can perform consistency check to see if the migration history has any gaps.+ schema_prev TEXT NOT NULL,+ execution_duration INTEGER NOT NULL,+ issue_id TEXT NOT NULL,+ payload TEXT NOT NULL+) ENGINE = MergeTree()+PRIMARY KEY id;