Skip to content

Commit

Permalink
Fix a bug when retried background data migration can not start
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed May 7, 2024
1 parent 3f1c222 commit edf5de1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## master (unreleased)

- Fix a bug when retried background data migration can not start
- Do not run multiple background schema migrations on the same table at the same time

## 0.17.1 (2024-04-28)
Expand Down
17 changes: 4 additions & 13 deletions lib/online_migrations/background_migrations/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ def last_job
migration_jobs.order(:max_value).last
end

def last_completed_job
migration_jobs.completed.order(:finished_at).last
end

# Returns the progress of the background migration.
#
# @return [Float, nil]
Expand Down Expand Up @@ -154,15 +150,10 @@ def migration_model
# @return [Boolean]
#
def interval_elapsed?
last_active_job = migration_jobs.active.order(:updated_at).last

if last_active_job && !last_active_job.stuck?
false
elsif batch_pause > 0 && (job = last_completed_job)
job.finished_at + batch_pause <= Time.current
else
true
end
last_job = migration_jobs.order(:updated_at).last
return true if last_job.nil?

last_job.enqueued? || (last_job.updated_at + batch_pause <= Time.current)
end

# Manually retry failed jobs.
Expand Down
11 changes: 1 addition & 10 deletions test/background_migrations/migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@ def test_migration_relation
end

def test_interval_elapsed_p
_user1 = User.create!
user2 = User.create!

2.times { User.create! }
m = create_migration(batch_pause: 2.minutes, batch_size: 1, sub_batch_size: 1)

assert m.interval_elapsed?
Expand All @@ -221,13 +219,6 @@ def test_interval_elapsed_p
Time.stub(:current, 1.minute.from_now) do
assert_not m.interval_elapsed?
end

Time.stub(:current, 3.minutes.from_now) do
assert m.interval_elapsed?

_job = m.migration_jobs.create!(min_value: user2.id, max_value: user2.id, status: "running")
assert_not m.interval_elapsed?
end
end

def test_retry_failed_jobs
Expand Down

0 comments on commit edf5de1

Please sign in to comment.