Skip to content

Commit

Permalink
Remove potentially heavy queries used to get the ranges of a backgrou…
Browse files Browse the repository at this point in the history
…nd migration
  • Loading branch information
fatkodima committed Jan 18, 2024
1 parent 2ab0784 commit 111746e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## master (unreleased)

- Remove potentially heavy queries used to get the ranges of a background migration

## 0.12.0 (2024-01-18)

- Require passing model name for background migration helpers when using multiple databases
Expand Down
17 changes: 6 additions & 11 deletions lib/online_migrations/background_migrations/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,8 @@ def next_batch_range
on_shard do
# rubocop:disable Lint/UnreachableLoop
iterator.each_batch(of: batch_size, column: batch_column_name, start: next_min_value) do |relation|
min = relation.arel_table[batch_column_name].minimum
max = relation.arel_table[batch_column_name].maximum
batch_range = relation.pick(min, max)
arel_column = relation.arel_table[batch_column_name]
batch_range = relation.pick(arel_column.minimum, arel_column.maximum)

break
end
Expand Down Expand Up @@ -249,14 +248,10 @@ def set_defaults
self.min_value = self.max_value = self.rows_count = -1 # not relevant
else
on_shard do
self.min_value ||= migration_relation.minimum(batch_column_name)
self.max_value ||= migration_relation.maximum(batch_column_name)

# This can be the case when run in development on empty tables
if min_value.nil?
# integer IDs minimum value is 1
self.min_value = self.max_value = 1
end
# Getting exact min/max values can be a very heavy operation
# and is not needed practically.
self.min_value ||= 1
self.max_value ||= migration_model.unscoped.maximum(batch_column_name) || self.min_value

count = migration_object.count
self.rows_count = count if count != :no_count
Expand Down
8 changes: 4 additions & 4 deletions test/background_migrations/migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@ def test_status_transitions
end

def test_sets_defaults
user1 = User.create!
_user1 = User.create!
user2 = User.create!

m = create_migration

assert m.enqueued?
assert_equal "id", m.batch_column_name
assert_equal user1.id, m.min_value
assert_equal 1, m.min_value
assert_equal user2.id, m.max_value
assert_not m.composite?
assert_nil m.parent
Expand Down Expand Up @@ -350,15 +350,15 @@ def test_creates_child_migrations_for_sharded_migration
child1, child2, child3 = children

assert_equal "default", child1.shard
assert_equal 10, child1.min_value
assert_equal 1, child1.min_value
assert_equal 100, child1.max_value
assert_equal 2, child1.rows_count

# Everything else is same as for "default".
assert_equal "shard_one", child2.shard

assert_equal "shard_two", child3.shard
assert_equal 200, child3.min_value
assert_equal 1, child3.min_value
assert_equal 300, child3.max_value
assert_equal 3, child3.rows_count
end
Expand Down

0 comments on commit 111746e

Please sign in to comment.