Skip to content

Commit

Permalink
Apply journal only if journaling is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
konalegi committed Oct 15, 2021
1 parent 1f139ba commit 69c6395
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## master (unreleased)
[#817](https://github.com/toptal/chewy/pull/817): Show warning messsge during rake chewy:reset or chewy:upgrade if journaling is disabled. ([@konalegi][])

### New Features

Expand Down
22 changes: 21 additions & 1 deletion lib/chewy/rake_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class << self
# @param output [IO] output io for logging
# @return [Array<Chewy::Index>] indexes that were reset
def reset(only: nil, except: nil, parallel: nil, output: $stdout)
warn_missing_index(output)

subscribed_task_stats(output) do
indexes_from(only: only, except: except).each do |index|
reset_one(index, output, parallel: parallel)
Expand All @@ -58,6 +60,8 @@ def reset(only: nil, except: nil, parallel: nil, output: $stdout)
# @param output [IO] output io for logging
# @return [Array<Chewy::Index>] indexes that were actually reset
def upgrade(only: nil, except: nil, parallel: nil, output: $stdout)
warn_missing_index(output)

subscribed_task_stats(output) do
indexes = indexes_from(only: only, except: except)

Expand Down Expand Up @@ -271,7 +275,23 @@ def human_duration(seconds)

def reset_one(index, output, parallel: false)
output.puts "Resetting #{index}"
index.reset!((Time.now.to_f * 1000).round, parallel: parallel)
index.reset!((Time.now.to_f * 1000).round, parallel: parallel, apply_journal: journal_exists?)
end

def warn_missing_index(output)
return if journal_exists?

output.puts "##########################################################\n" \
"WARN: You are risking to lose some changes during the reset.\n" \
" Please consider enabling journaling.\n" \
" See https://github.com/toptal/chewy#journaling\n" \
'##########################################################'
end

def journal_exists?
@journal_exists = Chewy::Stash::Journal.exists? if @journal_exists.nil?

@journal_exists
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions spec/chewy/rake_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
before { Chewy.massacre }

before do
described_class.instance_variable_set(:@journal_exists, journal_exists)

stub_model(:city)
stub_model(:country)

Expand All @@ -20,6 +22,7 @@
allow(described_class).to receive(:all_indexes) { [CitiesIndex, CountriesIndex, UsersIndex] }
end

let(:journal_exists) { true }
let!(:cities) { Array.new(3) { |i| City.create!(name: "Name#{i + 1}") } }
let!(:countries) { Array.new(2) { |i| Country.create!(name: "Name#{i + 1}") } }
let(:journal) do
Expand Down Expand Up @@ -92,6 +95,22 @@
Total: \\d+s\\Z
OUTPUT
end

context 'when journal is missing' do
let(:journal_exists) { false }

specify do
output = StringIO.new
expect { described_class.reset(only: [CitiesIndex], output: output) }
.to update_index(CitiesIndex)
expect(output.string).to include(
"##########################################################\n"\
"WARN: You are risking to lose some changes during the reset.\n" \
" Please consider enabling journaling.\n" \
' See https://github.com/toptal/chewy#journaling'
)
end
end
end

describe '.upgrade' do
Expand Down

0 comments on commit 69c6395

Please sign in to comment.