class DbBlaster::Finder

Find records and yield them a `batch_size` at a time

Attributes

block_on_find[R]
offset[R]
source_table[R]

Public Class Methods

find(source_table, &block) click to toggle source
# File lib/db_blaster/finder.rb, line 17
def self.find(source_table, &block)
  new(source_table, &block).find
end
new(source_table, &block) click to toggle source
# File lib/db_blaster/finder.rb, line 9
def initialize(source_table, &block)
  @source_table = source_table
  @block_on_find = block
  @offset = 0
end

Public Instance Methods

find() click to toggle source
# File lib/db_blaster/finder.rb, line 21
def find
  verify_source_table_name

  find_records_in_batches do |batch|
    filtered = batch.collect(&method(:filter_columns))
    next if filtered.blank?

    Chunker.chunk(source_table, filtered) do |chunked|
      block_on_find.call(chunked)
    end
  end
end

Private Instance Methods

filter_columns(selected_row) click to toggle source
# File lib/db_blaster/finder.rb, line 47
def filter_columns(selected_row)
  selected_row.except(*source_table.ignored_columns)
end
find_records_in_batches() { |result| ... } click to toggle source
# File lib/db_blaster/finder.rb, line 36
def find_records_in_batches
  select_sql = FinderSql.sql_for_source_table(source_table)
  loop do
    result = ActiveRecord::Base.connection.execute("#{select_sql} OFFSET #{offset}")
    yield(result)
    break if result.count != source_table_batch_size

    @offset += source_table_batch_size
  end
end
invalid_source_table_message() click to toggle source
# File lib/db_blaster/finder.rb, line 55
def invalid_source_table_message
  "source_table.name: '#{source_table_name}' does not exist!"
end
verify_source_table_name() click to toggle source
# File lib/db_blaster/finder.rb, line 51
def verify_source_table_name
  raise invalid_source_table_message unless available_tables.include?(source_table_name)
end