lib/lhm/chunker.rb in lhm-2.1.0 vs lib/lhm/chunker.rb in lhm-2.2.0
- old
+ new
@@ -23,11 +23,11 @@
end
def execute
return unless @start && @limit
@next_to_insert = @start
- until @next_to_insert >= @limit
+ while @next_to_insert < @limit || (@next_to_insert == 1 && @start == 1)
stride = @throttler.stride
affected_rows = @connection.update(copy(bottom, top(stride)))
if @throttler && affected_rows > 0
@throttler.run
@@ -48,12 +48,12 @@
def top(stride)
[(@next_to_insert + stride - 1), @limit].min
end
def copy(lowest, highest)
- "insert ignore into `#{ destination_name }` (#{ columns }) " +
- "select #{ select_columns } from `#{ origin_name }` " +
+ "insert ignore into `#{ destination_name }` (#{ destination_columns }) " \
+ "select #{ origin_columns } from `#{ origin_name }` " \
"#{ conditions } `#{ origin_name }`.`id` between #{ lowest } and #{ highest }"
end
def select_start
start = connection.select_value("select min(id) from `#{ origin_name }`")
@@ -63,26 +63,26 @@
def select_limit
limit = connection.select_value("select max(id) from `#{ origin_name }`")
limit ? limit.to_i : nil
end
- #XXX this is extremely brittle and doesn't work when filter contains more
- #than one SQL clause, e.g. "where ... group by foo". Before making any
- #more changes here, please consider either:
+ # XXX this is extremely brittle and doesn't work when filter contains more
+ # than one SQL clause, e.g. "where ... group by foo". Before making any
+ # more changes here, please consider either:
#
- #1. Letting users only specify part of defined clauses (i.e. don't allow
- #`filter` on Migrator to accept both WHERE and INNER JOIN
- #2. Changing query building so that it uses structured data rather than
- #strings until the last possible moment.
+ # 1. Letting users only specify part of defined clauses (i.e. don't allow
+ # `filter` on Migrator to accept both WHERE and INNER JOIN
+ # 2. Changing query building so that it uses structured data rather than
+ # strings until the last possible moment.
def conditions
if @migration.conditions
@migration.conditions.
- sub(/\)\Z/, "").
- #put any where conditions in parens
- sub(/where\s(\w.*)\Z/, "where (\\1)") + " and"
+ sub(/\)\Z/, '').
+ # put any where conditions in parens
+ sub(/where\s(\w.*)\Z/, 'where (\\1)') + ' and'
else
- "where"
+ 'where'
end
end
def destination_name
@migration.destination.name
@@ -90,20 +90,20 @@
def origin_name
@migration.origin.name
end
- def columns
- @columns ||= @migration.intersection.joined
+ def origin_columns
+ @origin_columns ||= @migration.intersection.origin.typed(origin_name)
end
- def select_columns
- @select_columns ||= @migration.intersection.typed("`#{origin_name}`")
+ def destination_columns
+ @destination_columns ||= @migration.intersection.destination.joined
end
def validate
if @start && @limit && @start > @limit
- error("impossible chunk options (limit must be greater than start)")
+ error('impossible chunk options (limit must be greater than start)')
end
end
end
end