class DbBlaster::FinderSql

Creates the SQL needed to find records for the provided source_table

Attributes

source_table[R]

Public Class Methods

new(source_table) click to toggle source
# File lib/db_blaster/finder_sql.rb, line 8
def initialize(source_table)
  @source_table = source_table
end
sql_for_source_table(source_table) click to toggle source
# File lib/db_blaster/finder_sql.rb, line 12
def self.sql_for_source_table(source_table)
  new(source_table).select_sql
end

Public Instance Methods

from_updated_at() click to toggle source
# File lib/db_blaster/finder_sql.rb, line 34
def from_updated_at
  @from_updated_at ||= source_table.last_published_updated_at
end
last_published_id() click to toggle source
# File lib/db_blaster/finder_sql.rb, line 38
def last_published_id
  @last_published_id ||= source_table.last_published_id
end
select_sql() click to toggle source
# File lib/db_blaster/finder_sql.rb, line 16
def select_sql
  "SELECT * FROM #{source_table.name} #{where} ORDER BY updated_at ASC LIMIT #{source_table.batch_size}"
end
where() click to toggle source

if we just use updated_at > from_updated_at, it's possible to miss records that share the same `updated_at` if we use updated_at >= from_updated_at, we'll get redundant records on every run settled on the approach below

# File lib/db_blaster/finder_sql.rb, line 24
def where
  return '' unless from_updated_at

  ActiveRecord::Base.sanitize_sql_for_conditions(
    ['WHERE updated_at > :updated_at OR (updated_at = :updated_at AND id <> :updated_id)',
     { updated_at: from_updated_at.to_s(:db),
       updated_id: last_published_id }]
  )
end