Skip to content

Commit

Permalink
Merge pull request ManageIQ#11857 from Fryguy/migration_helper_enhanc…
Browse files Browse the repository at this point in the history
…ements

Migration helper enhancements
  • Loading branch information
jrafanie committed Oct 18, 2016
2 parents 8b43521 + a102176 commit 808b708
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
36 changes: 36 additions & 0 deletions lib/migration_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,42 @@ def sanitize_sql_for_conditions(conditions)
DummyActiveRecordForMigrationHelper.send(:sanitize_sql_for_conditions, conditions)
end

#
# Batching
#

def say_batch_started(count)
say "Processing #{count} rows", :subitem
@batch_total_started = Time.now.utc
@batch_started = Time.now.utc
@batch_total = count
@batch_count = 0
end

def say_batch_processed(count)
Thread.exclusive do
if count > 0
@batch_count += count

progress = @batch_count / @batch_total.to_f * 100
timing = Time.now.utc - @batch_started
estimate = estimate_batch_complete(@batch_total_started, progress)

say "#{count} rows (#{"%.2f" % progress}% - #{@batch_count} total - #{"%.2f" % timing}s - ETA: #{estimate})", :subitem
end

@batch_started = Time.now.utc
@batch_count
end
end

def estimate_batch_complete(start_time, progress)
klass = Class.new { extend ActionView::Helpers::DateHelper }
estimated_end_time = start_time + (Time.now.utc - start_time) / (progress / 100.0)
klass.distance_of_time_in_words(Time.now.utc, estimated_end_time, :include_seconds => true)
end
private :estimate_batch_complete

#
# Triggers
#
Expand Down
3 changes: 3 additions & 0 deletions spec/support/migration_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def migration_context(direction, &block)
end

around do |example|
Rails.logger.debug("============================================================")
clearing_caches(&example)
end

Expand Down Expand Up @@ -66,7 +67,9 @@ def ar_stubs
end

def migrate_under_test
Rails.logger.debug("========= migrate start ====================================")
described_class.migrate(migration_direction)
Rails.logger.debug("========= migrate complete =================================")
end

def migration_direction
Expand Down

0 comments on commit 808b708

Please sign in to comment.