summaryrefslogtreecommitdiff
path: root/app/models/concerns/each_batch.rb
diff options
context:
space:
mode:
authorRobert Speicher <rspeicher@gmail.com>2021-01-20 13:34:23 -0600
committerRobert Speicher <rspeicher@gmail.com>2021-01-20 13:34:23 -0600
commit6438df3a1e0fb944485cebf07976160184697d72 (patch)
tree00b09bfd170e77ae9391b1a2f5a93ef6839f2597 /app/models/concerns/each_batch.rb
parent42bcd54d971da7ef2854b896a7b34f4ef8601067 (diff)
downloadgitlab-ce-6438df3a1e0fb944485cebf07976160184697d72.tar.gz
Add latest changes from gitlab-org/gitlab@13-8-stable-eev13.8.0-rc42
Diffstat (limited to 'app/models/concerns/each_batch.rb')
-rw-r--r--app/models/concerns/each_batch.rb16
1 files changed, 10 insertions, 6 deletions
diff --git a/app/models/concerns/each_batch.rb b/app/models/concerns/each_batch.rb
index af5f4e30d06..a59f00d73ec 100644
--- a/app/models/concerns/each_batch.rb
+++ b/app/models/concerns/each_batch.rb
@@ -47,7 +47,7 @@ module EachBatch
# order_hint does not affect the search results. For example,
# `ORDER BY id ASC, updated_at ASC` means the same thing as `ORDER
# BY id ASC`.
- def each_batch(of: 1000, column: primary_key, order_hint: nil)
+ def each_batch(of: 1000, column: primary_key, order: :asc, order_hint: nil)
unless column
raise ArgumentError,
'the column: argument must be set to a column name to use for ordering rows'
@@ -55,7 +55,7 @@ module EachBatch
start = except(:select)
.select(column)
- .reorder(column => :asc)
+ .reorder(column => order)
start = start.order(order_hint) if order_hint
start = start.take
@@ -66,10 +66,12 @@ module EachBatch
arel_table = self.arel_table
1.step do |index|
+ start_cond = arel_table[column].gteq(start_id)
+ start_cond = arel_table[column].lteq(start_id) if order == :desc
stop = except(:select)
.select(column)
- .where(arel_table[column].gteq(start_id))
- .reorder(column => :asc)
+ .where(start_cond)
+ .reorder(column => order)
stop = stop.order(order_hint) if order_hint
stop = stop
@@ -77,12 +79,14 @@ module EachBatch
.limit(1)
.take
- relation = where(arel_table[column].gteq(start_id))
+ relation = where(start_cond)
if stop
stop_id = stop[column]
start_id = stop_id
- relation = relation.where(arel_table[column].lt(stop_id))
+ stop_cond = arel_table[column].lt(stop_id)
+ stop_cond = arel_table[column].gt(stop_id) if order == :desc
+ relation = relation.where(stop_cond)
end
# Any ORDER BYs are useless for this relation and can lead to less