Skip to content

Commit

Permalink
Add #started_at/#finished_at to background data migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Apr 23, 2024
1 parent 5f3f731 commit da28eb7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/online_migrations/background_migrations/migration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ def retry_failed_jobs
end
end

# Returns the time this migration started running.
def started_at
# To be precise, we should get the minimum of `started_at` amongst the children jobs
# (for simple migrations) and amongst the children migrations (for composite migrations).
# But we do not have an appropriate index on the jobs table and using this will lead to
# N+1 queries if used inside some dashboard, for example.
created_at
end

# Returns the time this migration finished running.
def finished_at
updated_at if completed?
end

# @private
def on_shard(&block)
abstract_class = Utils.find_connection_class(migration_model)
Expand Down
13 changes: 13 additions & 0 deletions test/background_migrations/migration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,19 @@ def test_retry_failed_jobs
assert m.enqueued?
end

def test_started_at
m = create_migration
assert_equal m.created_at, m.started_at
end

def test_finished_at
m = create_migration
assert_nil m.finished_at
2.times { run_migration_job(m) }
assert m.succeeded?
assert_equal m.updated_at, m.finished_at
end

def test_next_batch_range
user1, user2, user3 = 3.times.map { User.create! }
m = create_migration(batch_size: 2, sub_batch_size: 1)
Expand Down

0 comments on commit da28eb7

Please sign in to comment.