Merge pull request #10267 from tchak/change-stable-id-task-again
chore(task): another attempt to backfill stable_id
This commit is contained in:
commit
bb88be7d9c
4 changed files with 37 additions and 23 deletions
13
app/jobs/migrations/backfill_stable_id_job.rb
Normal file
13
app/jobs/migrations/backfill_stable_id_job.rb
Normal file
|
@ -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
|
20
app/tasks/maintenance/backfill_champs_stable_id_task.rb
Normal file
20
app/tasks/maintenance/backfill_champs_stable_id_task.rb
Normal file
|
@ -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
|
|
@ -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
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue