Skip to content

Commit 7ae8c5b

Browse files
authored
Merge pull request #22 from dmi3-bu/USAFE-8826-run-clickhouse-migrations-in-ci
2 parents 2c0595e + c8b3443 commit 7ae8c5b

File tree

7 files changed

+114
-4
lines changed

7 files changed

+114
-4
lines changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
ci-helper (0.8.0)
4+
ci-helper (0.9.0)
55
colorize (~> 1.1)
66
dry-inflector (~> 1.0)
77
umbrellio-sequel-plugins (~> 0.14)

lib/ci_helper/commands/check_db_development.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ class CheckDBDevelopment < BaseCommand
66
def call
77
create_and_migrate_database!
88
execute("bundle exec rake db:seed")
9+
create_and_migrate_clickhouse_database! if with_clickhouse?
10+
0
911
end
1012

1113
private
1214

1315
def env
1416
:development
1517
end
18+
19+
def with_clickhouse?
20+
boolean_option(:with_clickhouse)
21+
end
1622
end
1723
end
1824
end

lib/ci_helper/commands/check_db_rollback.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,24 @@ def call
77
create_and_migrate_database!
88
execute_with_env("bundle exec rake db:rollback_new_migrations")
99
execute_with_env("bundle exec rake db:migrate")
10+
11+
if with_clickhouse?
12+
create_and_migrate_clickhouse_database!
13+
execute_with_env("bundle exec rake ch:rollback_new_migrations")
14+
execute_with_env("bundle exec rake ch:migrate")
15+
end
16+
0
1017
end
1118

1219
private
1320

1421
def env
1522
:test
1623
end
24+
25+
def with_clickhouse?
26+
boolean_option(:with_clickhouse)
27+
end
1728
end
1829
end
1930
end

lib/ci_helper/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module CIHelper
4-
VERSION = "0.8.0"
4+
VERSION = "0.9.0"
55
end

lib/tasks/sequel_management.rake

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class SequelManagement
1313
self.logger = Logger.new($stdout)
1414

1515
define_db_task!
16+
define_ch_task!
1617
end
1718

1819
private
@@ -23,6 +24,10 @@ class SequelManagement
2324
self.class::MIGRATIONS_PATH
2425
end
2526

27+
def ch_migrations_path
28+
"db/migrate/clickhouse"
29+
end
30+
2631
def define_db_task!
2732
namespace :db do
2833
desc "Rollback all migrations, which doesn't exist in master branch"
@@ -47,6 +52,31 @@ class SequelManagement
4752
end
4853
end
4954

55+
def define_ch_task!
56+
namespace :ch do
57+
desc "Rollback all migrations, which doesn't exist in master branch"
58+
task rollback_new_migrations: :environment do
59+
abort "Shouldn't run in production mode!" if Rails.env.production?
60+
61+
logger.info "Begin rolling back new migrations"
62+
63+
git_command = "git ls-tree --name-only origin/master #{ch_migrations_path}/"
64+
migration_files, error, status = Open3.capture3(git_command)
65+
abort "Can't get list of migration files:\n#{error}" unless status.success?
66+
67+
original_migrations = migration_files.split.map { |path| File.basename(path) }
68+
migrations_to_rollback = (ch_migrator.applied_migrations - original_migrations).sort.reverse
69+
70+
next if migrations_to_rollback.empty?
71+
72+
logger.info "Rolling back migrations:"
73+
logger.info migrations_to_rollback.join("\n")
74+
75+
ch_rollback!(migrations_to_rollback)
76+
end
77+
end
78+
end
79+
5080
def git_command
5181
"git ls-tree --name-only origin/master #{migrations_path}/"
5282
end
@@ -58,6 +88,14 @@ class SequelManagement
5888
end
5989
end
6090

91+
def ch_migrator
92+
@ch_migrator ||= begin
93+
full_path = Rails.root.join(ch_migrations_path)
94+
Sequel::TimestampMigrator.new(DB, full_path, allow_missing_migration_files: true,
95+
table: "clickhouse_migrations")
96+
end
97+
end
98+
6199
def rollback!(migrations)
62100
migrations.each do |migration|
63101
migrator.undo(migration.to_i)
@@ -69,6 +107,18 @@ class SequelManagement
69107
end
70108
end
71109
end
110+
111+
def ch_rollback!(migrations)
112+
migrations.each do |migration|
113+
ch_migrator.undo(migration.to_i)
114+
rescue Sequel::Migrator::Error => error
115+
if error.message.include?("does not exist in the filesystem")
116+
logger.info error.message
117+
else
118+
raise error
119+
end
120+
end
121+
end
72122
end
73123

74124
SequelManagement.new

spec/ci_helper/commands/check_db_development_spec.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
describe CIHelper::Commands::CheckDBDevelopment do
66
include_context "commands context"
77

8-
subject(:command) { described_class.call! }
8+
subject(:command) { described_class.call!(**options) }
9+
10+
let(:options) { Hash[] }
911

1012
let(:expected_commands) do
1113
[
@@ -19,4 +21,22 @@
1921
expect(popen_executed_commands.size).to eq(2)
2022
expect(popen_executed_commands).to eq(expected_commands)
2123
end
24+
25+
context "when with_clickhouse options" do
26+
let(:options) { Hash[with_clickhouse: "true"] }
27+
28+
let(:expected_commands) do
29+
[
30+
"export RAILS_ENV=development && bundle exec rake db:drop db:create db:migrate",
31+
"bundle exec rake db:seed",
32+
"export RAILS_ENV=development && bundle exec rake ch:create ch:migrate",
33+
]
34+
end
35+
36+
it "executes proper command and exits with success" do
37+
expect(command).to eq(0)
38+
expect(popen_executed_commands.size).to eq(3)
39+
expect(popen_executed_commands).to eq(expected_commands)
40+
end
41+
end
2242
end

spec/ci_helper/commands/check_db_rollback_spec.rb

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
describe CIHelper::Commands::CheckDBRollback do
66
include_context "commands context"
77

8-
subject(:command) { described_class.call! }
8+
subject(:command) { described_class.call!(**options) }
9+
10+
let(:options) { Hash[] }
911

1012
let(:expected_commands) do
1113
[
@@ -20,4 +22,25 @@
2022
expect(popen_executed_commands.size).to eq(3)
2123
expect(popen_executed_commands).to eq(expected_commands)
2224
end
25+
26+
context "when with_clickhouse options" do
27+
let(:options) { Hash[with_clickhouse: "true"] }
28+
29+
let(:expected_commands) do
30+
[
31+
"export RAILS_ENV=test && bundle exec rake db:drop db:create db:migrate",
32+
"export RAILS_ENV=test && bundle exec rake db:rollback_new_migrations",
33+
"export RAILS_ENV=test && bundle exec rake db:migrate",
34+
"export RAILS_ENV=test && bundle exec rake ch:create ch:migrate",
35+
"export RAILS_ENV=test && bundle exec rake ch:rollback_new_migrations",
36+
"export RAILS_ENV=test && bundle exec rake ch:migrate",
37+
]
38+
end
39+
40+
it "executes proper command and exits with success" do
41+
expect(command).to eq(0)
42+
expect(popen_executed_commands.size).to eq(6)
43+
expect(popen_executed_commands).to eq(expected_commands)
44+
end
45+
end
2346
end

0 commit comments

Comments
 (0)