Merge pull request #7536 from tchak/fix-destroy-type-de-champ

task(dossier): remove orphan champs
This commit is contained in:
Paul Chavard 2022-07-06 13:43:26 +02:00 committed by GitHub
commit 0ef7a8b9b0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 1 deletions

View file

@ -0,0 +1,34 @@
namespace :after_party do
desc 'Deployment task: remove_unused_champs'
task remove_unused_champs: :environment do
puts "Running deploy task 'remove_unused_champs'"
child_champs = Champ.where.not(parent_id: nil).select(:id, :dossier_id, :type_de_champ_id)
progress = ProgressReport.new(child_champs.size)
types_de_champ_by_dossier = Hash.new do |hash, dossier_id|
dossier = Dossier.select(:revision_id).find_by(id: dossier_id)
if dossier.present?
hash[dossier_id] = ProcedureRevisionTypeDeChamp.where(revision_id: dossier.revision_id).pluck(:type_de_champ_id)
else
hash[dossier_id] = []
end
end
champs_to_destroy = []
child_champs.find_each do |champ|
if !types_de_champ_by_dossier[champ.dossier_id].include?(champ.type_de_champ_id)
champs_to_destroy.push(champ.id)
end
progress.inc
end
progress.finish
Champ.where(id: champs_to_destroy).destroy_all
# 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

View file

@ -0,0 +1,22 @@
describe '20220705164551_remove_unused_champs' do
let(:rake_task) { Rake::Task['after_party:remove_unused_champs'] }
let(:procedure) { create(:procedure, :with_all_champs) }
let(:dossier) { create(:dossier, :with_populated_champs, procedure: procedure) }
let(:champ_repetition) { dossier.champs.find(&:repetition?) }
subject(:run_task) do
dossier
rake_task.invoke
end
before { champ_repetition.champs.first.update(type_de_champ: create(:type_de_champ)) }
after { rake_task.reenable }
describe 'remove_unused_champs' do
it "with bad champs" do
expect(Champ.where(dossier: dossier).count).to eq(39)
run_task
expect(Champ.where(dossier: dossier).count).to eq(38)
end
end
end

View file

@ -1556,8 +1556,9 @@ describe Dossier do
end end
describe "#destroy" do describe "#destroy" do
let(:procedure) { create(:procedure, :with_all_champs, :with_all_annotations) }
let(:transfer) { create(:dossier_transfer) } let(:transfer) { create(:dossier_transfer) }
let(:dossier) { create(:dossier, transfer: transfer) } let(:dossier) { create(:dossier, :with_populated_champs, :with_populated_annotations, transfer: transfer, procedure: procedure) }
before do before do
create(:dossier, transfer: transfer) create(:dossier, transfer: transfer)
@ -1566,6 +1567,12 @@ describe Dossier do
it "can destroy dossier" do it "can destroy dossier" do
expect(dossier.destroy).to be_truthy expect(dossier.destroy).to be_truthy
expect { dossier.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
it "can reset demarche" do
expect { dossier.procedure.reset! }.not_to raise_error
expect { dossier.reload }.to raise_error(ActiveRecord::RecordNotFound)
end end
end end