diff --git a/lib/tasks/deployment/20220211090402_reassign_redundant_attestation_templates.rake b/lib/tasks/deployment/20220211090402_reassign_redundant_attestation_templates.rake new file mode 100644 index 000000000..094e567bc --- /dev/null +++ b/lib/tasks/deployment/20220211090402_reassign_redundant_attestation_templates.rake @@ -0,0 +1,28 @@ +namespace :after_party do + desc 'Deployment task: reassign_redundant_attestation_templates' + task reassign_redundant_attestation_templates: :environment do + rake_puts "Running deploy task 'reassign_redundant_attestation_templates'" + + procedures = Procedure.publiees_ou_closes.joins(:draft_attestation_template, :published_attestation_template) + progress = ProgressReport.new(procedures.count) + + # On all published procedures with disabled revisions draft_attestation_template should be the same as published_attestation_template + # Let's not destroy redundant attestation_templates for now. We can clean orphans later. + procedures.find_each do |procedure| + progress.inc + if !procedure.feature_enabled?(:procedure_revisions) + draft_attestation_template = procedure.draft_attestation_template + published_attestation_template = procedure.published_attestation_template + if draft_attestation_template && published_attestation_template && draft_attestation_template != published_attestation_template + procedure.published_revision.update(attestation_template_id: draft_attestation_template.id) + end + end + end + progress.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 +end diff --git a/spec/lib/tasks/deployment/20220211090402_reassign_redundant_attestation_templates_spec.rb b/spec/lib/tasks/deployment/20220211090402_reassign_redundant_attestation_templates_spec.rb new file mode 100644 index 000000000..908aeaa4a --- /dev/null +++ b/spec/lib/tasks/deployment/20220211090402_reassign_redundant_attestation_templates_spec.rb @@ -0,0 +1,51 @@ +describe '20220211090402_reassign_redundant_attestation_templates' do + let(:rake_task) { Rake::Task['after_party:reassign_redundant_attestation_templates'] } + let(:procedure) { create(:procedure, :published) } + let(:procedure_with_revisions) { create(:procedure, :published) } + + before do + procedure.published_revision.update(attestation_template: create(:attestation_template)) + procedure.draft_revision.update(attestation_template: procedure.published_attestation_template.dup) + + Flipper.enable(:procedure_revisions, procedure_with_revisions) + procedure_with_revisions.published_revision.update(attestation_template: create(:attestation_template)) + procedure_with_revisions.draft_revision.update(attestation_template: procedure_with_revisions.published_attestation_template.dup) + end + + subject(:run_task) do + rake_task.invoke + procedure.reload + procedure_with_revisions.reload + end + + after { rake_task.reenable } + + describe 'reassign_redundant_attestation_templates' do + it 'reassign draft attestation template as published attestation template on procedures without revisions' do + expect(procedure.published_attestation_template).not_to be_nil + expect(procedure.draft_attestation_template).not_to be_nil + expect(procedure.draft_attestation_template).not_to eq(procedure.published_attestation_template) + + expect(procedure_with_revisions.published_attestation_template).not_to be_nil + expect(procedure_with_revisions.draft_attestation_template).not_to be_nil + expect(procedure_with_revisions.draft_attestation_template).not_to eq(procedure_with_revisions.published_attestation_template) + + orphans = AttestationTemplate.where(procedure_id: nil).left_outer_joins(:revisions).filter { |a| a.revisions.empty? } + expect(orphans).to eq([]) + to_be_orphan = procedure.published_attestation_template + + run_task + + expect(procedure.published_attestation_template).not_to be_nil + expect(procedure.draft_attestation_template).not_to be_nil + expect(procedure.draft_attestation_template).to eq(procedure.published_attestation_template) + + expect(procedure_with_revisions.published_attestation_template).not_to be_nil + expect(procedure_with_revisions.draft_attestation_template).not_to be_nil + expect(procedure_with_revisions.draft_attestation_template).not_to eq(procedure_with_revisions.published_attestation_template) + + orphans = AttestationTemplate.where(procedure_id: nil).left_outer_joins(:revisions).filter { |a| a.revisions.empty? } + expect(orphans).to eq([to_be_orphan]) + end + end +end