backfill missing experts_procedure_id on avis table

This commit is contained in:
kara Diaby 2021-03-22 17:33:33 +01:00
parent b730774118
commit 81d4db2afc

View file

@ -1,7 +1,7 @@
namespace :after_party do
desc 'Deployment task: backfill_experts_procedure_id_missing_on_avis'
task backfill_experts_procedure_id_missing_on_avis: :environment do
puts "Running deploy task 'backfill_experts_procedure_id_missing_on_avis'"
desc 'Deployment task: backfill_experts_procedure_on_avis_2'
task backfill_experts_procedure_on_avis_2: :environment do
puts "Running deploy task 'backfill_experts_procedure_on_avis_2'"
without_instructeur = Avis.where(experts_procedure_id: nil, answer: nil, instructeur_id: nil).where.not(email: nil)
with_instructeur = Avis.where(experts_procedure_id: nil, answer: nil, email: nil).where.not(instructeur_id: nil)
@ -10,10 +10,11 @@ namespace :after_party do
without_instructeur.find_each do |avis|
# if the avis email is valid then we create the associated expert
if Devise.email_regexp.match?(avis.email)
user = User.create_or_promote_to_expert(avis.email, SecureRandom.hex)
email = avis.email.strip
if Devise.email_regexp.match?(email)
user = User.create_or_promote_to_expert(email, SecureRandom.hex)
user.reload
experts_procedure = ExpertsProcedure.find_or_create_by(procedure: avis.dossier.procedure, expert: user.expert)
experts_procedure = ExpertsProcedure.find_or_create_by!(procedure: avis.dossier.procedure, expert: user.expert)
avis.update_column(:experts_procedure_id, experts_procedure.id)
end
progress.inc
@ -25,13 +26,15 @@ namespace :after_party do
if instructeur && instructeur.user
user = User.create_or_promote_to_expert(instructeur.user.email, SecureRandom.hex)
user.reload
experts_procedure = ExpertsProcedure.find_or_create_by(procedure: avis.dossier.procedure, expert: user.expert)
experts_procedure = ExpertsProcedure.find_or_create_by!(procedure: avis.dossier.procedure, expert: user.expert)
avis.update_column(:experts_procedure_id, experts_procedure.id)
end
progress2.inc
end
progress2.finish
# Update task as completed. If you remove the line below, the task will
# run with every deploy (or every time you call after_party:run).
AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end