umbrellio-sequel-plugins 0.16.1 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df0f6c6befdcdc3b6d69c32c7044a48a93c5f5de74494ac38562fb7ddcb2b82f
4
- data.tar.gz: d01ca778e900b85fb8d27ba59b32eb20ca78f78381633aa06b02e0becdf68e53
3
+ metadata.gz: b7572a3f551bf5409ea0cca6442366ec84ae4cf7776e379f3e020f099fe41f4c
4
+ data.tar.gz: f3dc5382d920669c883e7ae4d77e340f97df35c3d9bffbac3b258b03653b9744
5
5
  SHA512:
6
- metadata.gz: '01987d0c2cad01c5d87d7107b8f78e32457ad73959c73550f411e89e7dd9cbc437454bc343ebc86ee83e16108a3991d2c773025bd49a33c2b5b21c1f85a76161'
7
- data.tar.gz: ad1158104b963fd2a34cc08ba22d7cce4ca58521f4c4e8fb946222376a694d77501105a4d22035325eb5f54d9e585aaef5330481a62f51f34bc4077f71a8db8d
6
+ metadata.gz: c0381a1a25eae8e70624c830a3c09554efe042efb97c8218ef5137a7fc04056ebac42d5f976eb425c6aee9c5ccf711a945811213833e0e7acabcb608315e31f9
7
+ data.tar.gz: 640655214164e3f23290c4b3d2f60df248638ecdadb98687dc68f85d49276af31e768bbde3c1dfdbf11b29f8589427ade0a44ae39c4d6d0a461427dbf4ea544b
@@ -57,9 +57,6 @@ jobs:
57
57
  id-token: write
58
58
  contents: write
59
59
 
60
- env:
61
- PUBLISH_JOB: true
62
-
63
60
  # Run on push to master branch
64
61
  if: github.event_name == 'push' && github.ref == 'refs/heads/master'
65
62
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- umbrellio-sequel-plugins (0.16.1)
4
+ umbrellio-sequel-plugins (0.17.0)
5
5
  sequel
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -485,6 +485,130 @@ Overrides Rails default `dbconsole` and `db` commands. In order to use it, you h
485
485
  require "umbrellio_sequel_plugins/rails_db_command"
486
486
  ```
487
487
 
488
+ # Database Tasks for ClickHouse and Sequel
489
+
490
+ ## ClickHouse Rake Tasks
491
+
492
+ We have added a set of Rake tasks to manage ClickHouse database migrations and database operations. These tasks are located in the `namespace :ch`.
493
+
494
+ ### Task: `ch:create`
495
+
496
+ Creates a ClickHouse database in the specified cluster.
497
+
498
+ ```bash
499
+ rake ch:create
500
+ ```
501
+ This task will create a ClickHouse database as defined in the configuration file with the option to specify the cluster using the ClickHouse.config.database.
502
+
503
+ Example:
504
+ ```ruby
505
+ ClickHouse.config do |config|
506
+ config.assign Rails.application.config_for(:clickhouse)
507
+ end
508
+ ```
509
+
510
+ ### Task: `ch:create_migration_table`
511
+
512
+ Creates a migration tracking table for ClickHouse in PostgreSQL. This table will be used to track applied migrations for the ClickHouse database.
513
+
514
+ ```bash
515
+ rake ch:create_migration_table
516
+ ```
517
+
518
+ ### Task: `ch:drop`
519
+
520
+ Drops the ClickHouse database and truncates the migration tracking table.
521
+
522
+ ```bash
523
+ rake ch:drop
524
+ ```
525
+
526
+ ### Task: `ch:migrate`
527
+
528
+ Runs the migrations for the ClickHouse database from the db/migrate/clickhouse directory.
529
+
530
+ ```bash
531
+ rake ch:migrate
532
+ ```
533
+
534
+ You can specify a version to migrate to using the VERSION environment variable.
535
+
536
+ ### Task: `ch:rollback`
537
+
538
+ Rollbacks the migrations for the ClickHouse database to a specified version.
539
+
540
+ ```bash
541
+ rake ch:rollback VERSION=<version_number>
542
+ ```
543
+
544
+ If no version is provided, it rolls back the last migration.
545
+
546
+ ### Task: `ch:reset`
547
+
548
+ Drops, recreates, and runs all migrations for the ClickHouse database. This is useful for resetting the entire ClickHouse setup.
549
+
550
+ ```bash
551
+ rake ch:reset
552
+ ```
553
+
554
+ ### Task: `ch:rollback_missing_migrations`
555
+
556
+ Rollbacks any missing migrations for the ClickHouse database by comparing applied migrations to the available migration files.
557
+
558
+ ```bash
559
+ rake ch:rollback_missing_migrations
560
+ ```
561
+
562
+ ### Sequel Rake Tasks
563
+
564
+ Several tasks have been added under the namespace :sequel to provide better management of migrations and rollbacks in Sequel. These tasks help in managing PostgreSQL and ClickHouse migrations.
565
+
566
+ ### Task: `sequel:archive_migrations`
567
+
568
+ Archives migration source code into a PostgreSQL table for tracking purposes. This task can now accept custom paths for migrations and source tables.
569
+
570
+ ```bash
571
+ rake sequel:archive_migrations[migrations_path, migration_table_source]
572
+ ```
573
+
574
+ - `migrations_path`: Path to the migration files (default is `db/migrate/*.rb`).
575
+ - `migration_table_source`: Table to store migration source code (default is `:schema_migrations_sources`).
576
+
577
+ ### `Task: sequel:rollback_archived_migrations`
578
+
579
+ Rollbacks migrations that were applied but are no longer present in the current release. The task supports additional options such as custom migration paths, tables, and transaction settings.
580
+
581
+ ```bash
582
+ rake sequel:rollback_archived_migrations[migrations_path, migration_table, migration_table_source, use_transactions]
583
+ ```
584
+
585
+ - `migrations_path`: Path to the migration files (default is `db/migrate/*.rb`).
586
+ - `migration_table`: Table used to track applied migrations (default is `:schema_migrations`).
587
+ - `migration_table_source`: Table storing migration source code (default is `:schema_migrations_sources`).
588
+ - `use_transactions`: Whether to use transactions for rolling back (default is `false`).
589
+
590
+ ### Task: `sequel:rollback_missing_migrations`
591
+
592
+ Rollbacks migrations that are absent in the current release when deploying to staging or production. This task helps ensure consistency between different versions.
593
+
594
+ ```bash
595
+ rake sequel:rollback_missing_migrations[table, use_transactions]
596
+ ```
597
+
598
+ - `table`: The table used to track migrations (optional).
599
+ - `use_transactions`: Whether to use transactions during rollback (default is `false`).
600
+
601
+ ### Task: `sequel:rollback_missing_migrations`
602
+
603
+ This task specifically helps during deployment by rolling back any migrations that are not present in the current release.
604
+
605
+ ```bash
606
+ rake sequel:rollback_missing_migrations[table, use_transactions]
607
+ ```
608
+
609
+ - `table`: The table used to track migrations (optional).
610
+ - `use_transactions`: Whether or not to use transactions when rolling back (optional).
611
+
488
612
  ## License
489
613
 
490
614
  Released under MIT License.
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :nocov:
4
+ module Clickhouse
5
+ module Migrator
6
+ module_function
7
+
8
+ def migrate(to: nil)
9
+ if to.present?
10
+ migrator(target: to.to_i).run
11
+ else
12
+ migrator.run
13
+ end
14
+ end
15
+
16
+ def rollback(to: nil)
17
+ target = to || migrator.applied_migrations.reverse[1]
18
+ migrator(target: target.to_i).run
19
+ end
20
+
21
+ def migrator(**opts)
22
+ Sequel::TimestampMigrator.new(
23
+ DB,
24
+ Rails.root.join("db/migrate/clickhouse"),
25
+ table: :clickhouse_migrations,
26
+ use_transactions: false,
27
+ **opts,
28
+ )
29
+ end
30
+ end
31
+ end
32
+ # :nocov:
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "clickhouse/migrator"
4
+
5
+ namespace :ch do
6
+ desc "Create a ClickHouse database in the specified cluster"
7
+ task create: :environment do
8
+ CH.create_database(ClickHouse.config.database, cluster: "click_cluster")
9
+ end
10
+
11
+ desc "Create a migration tracking table for ClickHouse in PostgreSQL"
12
+ task create_migration_table: :environment do
13
+ DB.create_table Sequel[:public][:clickhouse_migrations] do
14
+ column :filename, :text, null: false, primary_key: true
15
+ end
16
+ end
17
+
18
+ desc "Drop the ClickHouse database and truncate the migration tracking table"
19
+ task drop: :environment do
20
+ CH.drop_database(ClickHouse.config.database, cluster: "click_cluster")
21
+ DB.from(Sequel[:public][:clickhouse_migrations]).truncate
22
+ DB.from(Sequel[:public][:clickhouse_migrations_sources]).truncate
23
+ end
24
+
25
+ desc "Run migrations for the ClickHouse database"
26
+ task migrate: :environment do
27
+ path = "db/migrate/clickhouse/*.rb"
28
+ migrations_table = :clickhouse_migrations
29
+ migrations_sources_table = :clickhouse_migrations_sources
30
+ use_transactions = "false"
31
+
32
+ Rake::Task["sequel:archive_migrations"].reenable
33
+ Rake::Task["sequel:archive_migrations"].invoke(path, migrations_sources_table)
34
+
35
+ Rake::Task["sequel:rollback_archived_migrations"].reenable
36
+ Rake::Task["sequel:rollback_archived_migrations"]
37
+ .invoke(path, migrations_table, migrations_sources_table, use_transactions)
38
+
39
+ Clickhouse::Migrator.migrate(to: ENV.fetch("VERSION", nil))
40
+ end
41
+
42
+ desc "Rollback migrations for the ClickHouse database"
43
+ task rollback: :environment do
44
+ Clickhouse::Migrator.rollback(to: ENV.fetch("VERSION", nil))
45
+ end
46
+
47
+ desc "Reset the ClickHouse database: drop, recreate, and run all migrations"
48
+ task reset: :environment do
49
+ Rake::Task["ch:drop"].invoke
50
+ Rake::Task["ch:create"].invoke
51
+ Rake::Task["ch:migrate"].invoke
52
+ end
53
+
54
+ desc "Rollback any missing migrations for ClickHouse"
55
+ task rollback_missing_migrations: :environment do
56
+ Rake::Task["sequel:rollback_missing_migrations"].reenable
57
+ Rake::Task["sequel:rollback_missing_migrations"].invoke(:clickhouse_migrations, "false")
58
+ end
59
+ end
@@ -2,14 +2,18 @@
2
2
 
3
3
  namespace :sequel do
4
4
  desc "Archive migrations source code"
5
- task archive_migrations: :environment do
6
- DB.create_table?(:schema_migrations_sources) do
5
+ task :archive_migrations,
6
+ [:migrations_path, :migration_table_source] => :environment do |_t, args|
7
+ migrations_path = args[:migrations_path] || "db/migrate/*.rb"
8
+ migration_table_source = args[:migration_table_source]&.to_sym || :schema_migrations_sources
9
+
10
+ DB.create_table?(migration_table_source) do
7
11
  column :version, "numeric", primary_key: true
8
12
  column :filename, "text", null: false
9
13
  column :source, "text", null: false
10
14
  end
11
15
 
12
- migrations = Rails.root.glob("db/migrate/*.rb").map do |file|
16
+ migrations = Rails.root.glob(migrations_path).map do |file|
13
17
  filename = file.basename.to_s
14
18
  { version: filename.to_i, filename: filename, source: file.read }
15
19
  end
@@ -19,6 +23,6 @@ namespace :sequel do
19
23
  update: { filename: Sequel[:excluded][:filename], source: Sequel[:excluded][:source] },
20
24
  }
21
25
 
22
- DB[:schema_migrations_sources].insert_conflict(**conflict_options).multi_insert(migrations)
26
+ DB[migration_table_source].insert_conflict(**conflict_options).multi_insert(migrations)
23
27
  end
24
28
  end
@@ -4,19 +4,30 @@ require "sequel/timestamp_migrator_undo_extension"
4
4
 
5
5
  namespace :sequel do
6
6
  desc "Rollback migrations that were applied earlier but are not present in current release"
7
- task rollback_archived_migrations: :environment do
7
+ task :rollback_archived_migrations,
8
+ [:migrations_path, :migration_table, :migration_table_source,
9
+ :use_transactions] => :environment do |_t, args|
10
+ migrations_path = args[:migrations_path] || "db/migrate/*.rb"
11
+ migration_table_source = args[:migration_table_source]&.to_sym || :schema_migrations_sources
12
+ use_transactions = args[:use_transactions].nil? ? nil : args[:use_transactions] == "true"
13
+
8
14
  DB.log_info("Finding applied migrations not present in current release...")
9
15
 
10
16
  Dir.mktmpdir do |tmpdir|
11
- DB[:schema_migrations_sources].each do |migration|
17
+ DB[migration_table_source].each do |migration|
12
18
  path = File.join(tmpdir, migration.fetch(:filename))
13
19
  File.write(path, migration.fetch(:source))
14
20
  end
15
21
 
16
- migrator = Sequel::TimestampMigrator.new(DB, tmpdir, allow_missing_migration_files: true)
22
+ migrator_args = {
23
+ table: args[:migration_table],
24
+ use_transactions: use_transactions,
25
+ allow_missing_migration_files: false,
26
+ }.compact
27
+ migrator = Sequel::TimestampMigrator.new(DB, tmpdir, migrator_args)
17
28
 
18
29
  applied_migrations = migrator.applied_migrations.map(&:to_i)
19
- filesystem_migrations = Rails.root.glob("db/migrate/*.rb").map { |x| File.basename(x).to_i }
30
+ filesystem_migrations = Rails.root.glob(migrations_path).map { |x| File.basename(x).to_i }
20
31
  missing_migrations = applied_migrations - filesystem_migrations
21
32
 
22
33
  if missing_migrations.any?
@@ -4,7 +4,9 @@ require "sequel/timestamp_migrator_undo_extension"
4
4
 
5
5
  namespace :sequel do
6
6
  desc "Rollback migrations that are absent in revision when deploying on staging"
7
- task rollback_missing_migrations: :environment do
7
+ task :rollback_missing_migrations, [:table, :use_transactions] => :environment do |_t, args|
8
+ use_transactions = args[:use_transactions].nil? ? nil : args[:use_transactions] == "true"
9
+
8
10
  extract_migrations = lambda do |path|
9
11
  Dir.glob("#{path}/db/migrate/*.rb").map { |filename| File.basename(filename).to_i }
10
12
  end
@@ -19,7 +21,12 @@ namespace :sequel do
19
21
  puts migrations_to_rollback
20
22
 
21
23
  path = Rails.root.join("db/migrate")
22
- migrator = Sequel::TimestampMigrator.new(DB, path, allow_missing_migration_files: true)
24
+ migrator_args = {
25
+ table: args[:table],
26
+ use_transactions: use_transactions,
27
+ allow_missing_migration_files: false,
28
+ }.compact
29
+ migrator = Sequel::TimestampMigrator.new(DB, path, migrator_args)
23
30
  applied_migrations = migrator.applied_migrations.map(&:to_i)
24
31
  migrations = applied_migrations.select { |m| m.in?(migrations_to_rollback) }.sort.reverse
25
32
 
@@ -5,7 +5,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "umbrellio-sequel-plugins"
8
- spec.version = "0.16.1"
8
+ spec.version = "0.17.0"
9
9
  spec.required_ruby_version = ">= 3.0"
10
10
 
11
11
  spec.authors = ["Team Umbrellio"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: umbrellio-sequel-plugins
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.1
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Team Umbrellio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-13 00:00:00.000000000 Z
11
+ date: 2024-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -42,6 +42,7 @@ files:
42
42
  - Rakefile
43
43
  - bin/console
44
44
  - bin/setup
45
+ - lib/clickhouse/migrator.rb
45
46
  - lib/sequel/extensions/currency_rates.rb
46
47
  - lib/sequel/extensions/deferrable_foreign_keys.rb
47
48
  - lib/sequel/extensions/fibered_connection_pool.rb
@@ -61,6 +62,7 @@ files:
61
62
  - lib/sequel/plugins/upsert.rb
62
63
  - lib/sequel/plugins/with_lock.rb
63
64
  - lib/sequel/timestamp_migrator_undo_extension.rb
65
+ - lib/tasks/clickhouse.rake
64
66
  - lib/tasks/sequel/archive_migrations.rake
65
67
  - lib/tasks/sequel/rollback_archived_migrations.rake
66
68
  - lib/tasks/sequel/rollback_missing_migrations.rake