Skip to content

Commit

Permalink
Fix running background migrations over relations with duplicate records
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Nov 9, 2024
1 parent ee2049f commit 2bb3d18
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## master (unreleased)

- Fix running background migrations over relations with duplicate records

## 0.20.1 (2024-11-05)

- Fix background data migrations to work with `includes`/`eager_load` on relation
Expand Down
3 changes: 2 additions & 1 deletion lib/online_migrations/batch_iterator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ def each_batch(of: 1000, column: relation.primary_key, start: nil, finish: nil,
# Retaining the results in the query cache would undermine the point of batching.
batch_relation.uncached { yield batch_relation, start_id, last_id }

break if stop_row.nil?
break if last_id == finish

start_id = stop_id
stop_id = nil
end
end

Expand Down
10 changes: 10 additions & 0 deletions test/background_migrations/background_migrations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ def process_batch(_users)
end
end

class RelationWithJoins < OnlineMigrations::BackgroundMigration
def relation
User.joins(:projects)
end

def process_batch(_users)
# no-op
end
end

class MigrationWithCount < OnlineMigrations::BackgroundMigration
def relation
User.all
Expand Down
9 changes: 9 additions & 0 deletions test/background_migrations/migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ def test_relation_with_includes
assert m.succeeded?
end

def test_relation_with_duplicate_records
user = User.create!
3.times { user.projects.create! }
m = create_migration(migration_name: "RelationWithJoins", batch_size: 2, sub_batch_size: 2)

run_all_migration_jobs(m)
assert m.succeeded?
end

def test_progress_succeded_migration
m = build_migration(status: :succeeded)
assert_in_delta 100.0, m.progress
Expand Down

0 comments on commit 2bb3d18

Please sign in to comment.