diff --git a/app/jobs/migrations/backfill_stable_id_job.rb b/app/jobs/migrations/backfill_stable_id_job.rb new file mode 100644 index 000000000..2ccfaeaba --- /dev/null +++ b/app/jobs/migrations/backfill_stable_id_job.rb @@ -0,0 +1,13 @@ +class Migrations::BackfillStableIdJob < ApplicationJob + queue_as :low_priority + + def perform(dossier_id) + 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]) + ActiveRecord::Base.connection.execute(query) + end +end diff --git a/app/tasks/maintenance/backfill_champs_stable_id_task.rb b/app/tasks/maintenance/backfill_champs_stable_id_task.rb new file mode 100644 index 000000000..db0ab2eff --- /dev/null +++ b/app/tasks/maintenance/backfill_champs_stable_id_task.rb @@ -0,0 +1,20 @@ +# frozen_string_literal: true + +module Maintenance + class BackfillChampsStableIdTask < MaintenanceTasks::Task + def collection + Dossier.select(:id) + end + + 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 + end + end +end diff --git a/app/tasks/maintenance/fill_champs_stable_id_task.rb b/app/tasks/maintenance/fill_champs_stable_id_task.rb deleted file mode 100644 index bf6c6c9f6..000000000 --- a/app/tasks/maintenance/fill_champs_stable_id_task.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -module Maintenance - class FillChampsStableIdTask < MaintenanceTasks::Task - BATCH = 20 - - def collection - (Dossier.last.id / BATCH).ceil.times.to_a - end - - def process(batch_number) - dossier_id_start = batch_number * BATCH - dossier_id_end = dossier_id_start + BATCH - Champ - .where(dossier_id: dossier_id_start..dossier_id_end, stable_id: nil) - .joins(:type_de_champ) - .select('champs.id, types_de_champ.stable_id as type_de_champ_stable_id') - .find_each do |champ| - champ.update_columns(stable_id: champ.type_de_champ_stable_id, stream: 'main') - end - end - end -end diff --git a/config/initializers/transition_to_sidekiq.rb b/config/initializers/transition_to_sidekiq.rb index 7f319a64c..8c12e200e 100644 --- a/config/initializers/transition_to_sidekiq.rb +++ b/config/initializers/transition_to_sidekiq.rb @@ -49,5 +49,9 @@ if Rails.env.production? && sidekiq_enabled class DossierUpdateSearchTermsJob < ApplicationJob self.queue_adapter = :sidekiq end + + class Migrations::BackfillStableIdJob + self.queue_adapter = :sidekiq + end end end