diff --git a/app/jobs/migrations/backfill_stable_id_job.rb b/app/jobs/migrations/backfill_stable_id_job.rb index 2ccfaeaba..e145d621c 100644 --- a/app/jobs/migrations/backfill_stable_id_job.rb +++ b/app/jobs/migrations/backfill_stable_id_job.rb @@ -1,13 +1,18 @@ class Migrations::BackfillStableIdJob < ApplicationJob queue_as :low_priority - def perform(dossier_id) + BATCH = 100_000 + + def perform(iteration) sql = "UPDATE champs SET stable_id = t.stable_id, stream = 'main' FROM types_de_champ t WHERE champs.type_de_champ_id = t.id - AND champs.dossier_id = ? - AND champs.stable_id IS NULL;" - query = ActiveRecord::Base.sanitize_sql_array([sql, dossier_id]) + AND champs.id IN (SELECT id FROM champs WHERE champs.stable_id IS NULL LIMIT ?);" + query = ActiveRecord::Base.sanitize_sql_array([sql, BATCH]) ActiveRecord::Base.connection.execute(query) + + if Champ.exists?(stable_id: nil) + Migrations::BackfillStableIdJob.set(wait: 2.seconds).perform_later(iteration + 1) + end end end diff --git a/app/tasks/maintenance/backfill_champs_stable_id_task.rb b/app/tasks/maintenance/backfill_champs_stable_id_task.rb index db0ab2eff..040d78d04 100644 --- a/app/tasks/maintenance/backfill_champs_stable_id_task.rb +++ b/app/tasks/maintenance/backfill_champs_stable_id_task.rb @@ -2,19 +2,10 @@ module Maintenance class BackfillChampsStableIdTask < MaintenanceTasks::Task - def collection - Dossier.select(:id) - end + no_collection - def process(dossier) - if Champ.exists?(dossier_id: dossier.id, stable_id: nil) - day = 24 * 60 * 60 # 24 hours in seconds - wait = rand(0...(day * 4)).seconds # every second over 4 days - - Migrations::BackfillStableIdJob - .set(wait:) - .perform_later(dossier.id) - end + def process + Migrations::BackfillStableIdJob.perform_later(0) end end end