diff --git a/lib/tasks/deployment/20220112184331_revise_attestation_templates.rake b/lib/tasks/deployment/20220112184331_revise_attestation_templates.rake index 9478bb490..24116586c 100644 --- a/lib/tasks/deployment/20220112184331_revise_attestation_templates.rake +++ b/lib/tasks/deployment/20220112184331_revise_attestation_templates.rake @@ -3,14 +3,16 @@ namespace :after_party do task revise_attestation_templates: :environment do rake_puts "Running deploy task 'revise_attestation_templates'" - attestation_templates = AttestationTemplate.where.not(procedure_id: nil) - progress = ProgressReport.new(attestation_templates.count) + revisions = ProcedureRevision + .joins(procedure: :attestation_template) + .where(attestation_template_id: nil) + + progress = ProgressReport.new(revisions.count) + + revisions.find_each do |revision| + attestation_template_id = revision.procedure.attestation_template.id + revision.update_column(:attestation_template_id, attestation_template_id) - attestation_templates.find_each do |attestation_template| - ProcedureRevision - .where(procedure_id: attestation_template.procedure_id, attestation_template_id: nil) - .update_all(attestation_template_id: attestation_template) - attestation_template.update_column(:procedure_id, nil) progress.inc end progress.finish diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 0cef3e166..a99eb46f3 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -23,7 +23,7 @@ FactoryBot.define do types_de_champ { [] } types_de_champ_private { [] } updated_at { nil } - attestation_template { } + attestation_template { nil } end after(:build) do |procedure, evaluator| diff --git a/spec/lib/tasks/deployment/20220112184331_revise_attestation_templates_spec.rb b/spec/lib/tasks/deployment/20220112184331_revise_attestation_templates_spec.rb new file mode 100644 index 000000000..d6e620f60 --- /dev/null +++ b/spec/lib/tasks/deployment/20220112184331_revise_attestation_templates_spec.rb @@ -0,0 +1,22 @@ +describe '20220112184331_revise_attestation_templates' do + let(:rake_task) { Rake::Task['after_party:revise_attestation_templates'] } + let(:procedure) { create(:procedure) } + let(:attestation_template) { create(:attestation_template, procedure: procedure) } + + subject(:run_task) do + attestation_template + rake_task.invoke + attestation_template.reload + end + + after { rake_task.reenable } + + context 'add attestation_template_id' do + it 'to revisions' do + expect(attestation_template.procedure.revisions.first.attestation_template_id).to be_nil + run_task + expect(attestation_template.procedure_id).to eq(procedure.id) + expect(attestation_template.procedure.revisions.first.attestation_template_id).to eq(attestation_template.id) + end + end +end