chore(task): another attempt to backfill stable_id

This commit is contained in:
Paul Chavard 2024-04-04 16:28:57 +02:00
parent 9c26d8486f
commit 81c325f938
4 changed files with 37 additions and 23 deletions

View 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

View 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

View file

@ -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

View file

@ -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