-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for custom migration paths and table names in Migration A…
…rchiving Tasks (#40) ## [0.17.0] ### Added - Rake task for managing ClickHouse database creation, migration, and rollback. - Support for tracking ClickHouse migrations in PostgreSQL via a migration tracking table. ### Changed - Modified `archive_migrations` Rake task to accept custom migration paths and source tables as arguments. - Modified `rollback_archived_migrations` Rake task to support configurable migration paths, migration tables, and transaction settings. - Modified `rollback_missing_migrations` Rake task to support optional transaction handling and custom migration tables.
- Loading branch information
1 parent
888c0dd
commit 579a775
Showing
8 changed files
with
249 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
umbrellio-sequel-plugins (0.16.1) | ||
umbrellio-sequel-plugins (0.17.0) | ||
sequel | ||
|
||
GEM | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# frozen_string_literal: true | ||
|
||
# :nocov: | ||
module Clickhouse | ||
module Migrator | ||
module_function | ||
|
||
def migrate(to: nil) | ||
if to.present? | ||
migrator(target: to.to_i).run | ||
else | ||
migrator.run | ||
end | ||
end | ||
|
||
def rollback(to: nil) | ||
target = to || migrator.applied_migrations.reverse[1] | ||
migrator(target: target.to_i).run | ||
end | ||
|
||
def migrator(**opts) | ||
Sequel::TimestampMigrator.new( | ||
DB, | ||
Rails.root.join("db/migrate/clickhouse"), | ||
table: :clickhouse_migrations, | ||
use_transactions: false, | ||
**opts, | ||
) | ||
end | ||
end | ||
end | ||
# :nocov: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require "clickhouse/migrator" | ||
|
||
namespace :ch do | ||
desc "Create a ClickHouse database in the specified cluster" | ||
task create: :environment do | ||
CH.create_database(ClickHouse.config.database, cluster: "click_cluster") | ||
end | ||
|
||
desc "Create a migration tracking table for ClickHouse in PostgreSQL" | ||
task create_migration_table: :environment do | ||
DB.create_table Sequel[:public][:clickhouse_migrations] do | ||
column :filename, :text, null: false, primary_key: true | ||
end | ||
end | ||
|
||
desc "Drop the ClickHouse database and truncate the migration tracking table" | ||
task drop: :environment do | ||
CH.drop_database(ClickHouse.config.database, cluster: "click_cluster") | ||
DB.from(Sequel[:public][:clickhouse_migrations]).truncate | ||
DB.from(Sequel[:public][:clickhouse_migrations_sources]).truncate | ||
end | ||
|
||
desc "Run migrations for the ClickHouse database" | ||
task migrate: :environment do | ||
path = "db/migrate/clickhouse/*.rb" | ||
migrations_table = :clickhouse_migrations | ||
migrations_sources_table = :clickhouse_migrations_sources | ||
use_transactions = "false" | ||
|
||
Rake::Task["sequel:archive_migrations"].reenable | ||
Rake::Task["sequel:archive_migrations"].invoke(path, migrations_sources_table) | ||
|
||
Rake::Task["sequel:rollback_archived_migrations"].reenable | ||
Rake::Task["sequel:rollback_archived_migrations"] | ||
.invoke(path, migrations_table, migrations_sources_table, use_transactions) | ||
|
||
Clickhouse::Migrator.migrate(to: ENV.fetch("VERSION", nil)) | ||
end | ||
|
||
desc "Rollback migrations for the ClickHouse database" | ||
task rollback: :environment do | ||
Clickhouse::Migrator.rollback(to: ENV.fetch("VERSION", nil)) | ||
end | ||
|
||
desc "Reset the ClickHouse database: drop, recreate, and run all migrations" | ||
task reset: :environment do | ||
Rake::Task["ch:drop"].invoke | ||
Rake::Task["ch:create"].invoke | ||
Rake::Task["ch:migrate"].invoke | ||
end | ||
|
||
desc "Rollback any missing migrations for ClickHouse" | ||
task rollback_missing_migrations: :environment do | ||
Rake::Task["sequel:rollback_missing_migrations"].reenable | ||
Rake::Task["sequel:rollback_missing_migrations"].invoke(:clickhouse_migrations, "false") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters