fix(attestation_template): fix migration task

This commit is contained in:
Paul Chavard 2022-01-26 18:27:57 +01:00
parent 12151acb99
commit 1fc7db2562
3 changed files with 32 additions and 8 deletions

View file

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

View file

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

View file

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