task(dossier): remove orphan champs
This commit is contained in:
parent
adfc4f8cdd
commit
e9d0ccbdf9
2 changed files with 56 additions and 0 deletions
|
@ -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
|
|
@ -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
|
Loading…
Reference in a new issue