tasks: delete dossiers without procedures

Fix #3279
This commit is contained in:
Pierre de La Morinerie 2019-01-23 15:13:57 +00:00
parent db8289a19d
commit bb1cc1eef8
2 changed files with 67 additions and 0 deletions

View file

@ -0,0 +1,25 @@
require Rails.root.join("lib", "tasks", "task_helper")
namespace :after_party do
desc 'Deployment task: delete_dossiers_without_procedure'
task delete_dossiers_without_procedure: :environment do
rake_puts "Running deploy task 'delete_dossiers_without_procedure'"
dossiers_without_procedure = Dossier.left_outer_joins(:procedure).where(procedures: { id: nil })
total = dossiers_without_procedure.count
expected_dossiers_count = 60
if total > expected_dossiers_count
raise "Error: #{expected_dossiers_count} dossiers expected, but found #{total}. Aborting."
end
dossiers_without_procedure.each do |dossier|
rake_puts "Destroy dossier #{dossier.id}"
dossier.destroy!
end
rake_puts "#{total} dossiers without procedure were destroyed."
AfterParty::TaskRecord.create version: '20190117154829'
end # task :delete_dossiers_without_procedure
end # namespace :after_party

View file

@ -0,0 +1,42 @@
describe '20190117154829_delete_dossiers_without_procedure.rake' do
let(:rake_task) { Rake::Task['after_party:delete_dossiers_without_procedure'] }
subject do
rake_task.invoke
end
after do
rake_task.reenable
end
context 'when the procedure of some dossiers has been deleted' do
let!(:procedure1) { create(:procedure_with_dossiers, dossiers_count: 2) }
let!(:procedure2) { create(:procedure_with_dossiers, :published, dossiers_count: 2) }
let!(:procedure3) { create(:procedure_with_dossiers, :published, dossiers_count: 2) }
let!(:procedure4) { create(:procedure_with_dossiers, :archived, dossiers_count: 2) }
let(:procedure_2_dossier_ids) { procedure2.dossiers.pluck(:id) }
before do
procedure_2_dossier_ids
procedure2.delete
expect(procedure_2_dossier_ids.count).to eq(2)
expect(Dossier.find_by(id: procedure_2_dossier_ids.first).procedure).to be nil
expect(Dossier.find_by(id: procedure_2_dossier_ids.second).procedure).to be nil
end
it 'destroy dossiers without an existing procedure' do
subject
expect(Dossier.unscoped.find_by(id: procedure_2_dossier_ids.first)).to be nil
expect(Dossier.unscoped.find_by(id: procedure_2_dossier_ids.last)).to be nil
end
it 'doesnt destroy other dossiers' do
subject
expect(Dossier.all.count).to eq(6)
expect(procedure1.reload.dossiers.count).to eq(2)
expect(procedure3.reload.dossiers.count).to eq(2)
expect(procedure4.reload.dossiers.count).to eq(2)
end
end
end