[Fix #3192] Fix data

This commit is contained in:
Mathieu Magnin 2018-12-20 11:13:58 +01:00
parent b40c706d98
commit 904d8d208b
2 changed files with 64 additions and 0 deletions

View file

@ -0,0 +1,32 @@
namespace :after_party do
desc 'Deployment task: clone_service_for_transferred_procedures'
task clone_service_for_transferred_procedures: :environment do
puts "Running deploy task 'clone_service_for_transferred_procedures'"
procedures = Procedure.includes(:service).where.not(service_id: nil)
procedures_to_fix_in_array = procedures.select do |p|
p.administrateur_id != p.service.administrateur_id
end
procedures_to_fix = Procedure.where(id: procedures_to_fix_in_array.map(&:id))
service_and_admin_list = procedures_to_fix.group(:service_id, :administrateur_id).count.keys
progress = ProgressReport.new(service_and_admin_list.count)
service_and_admin_list.each do |service_id, administrateur_id|
cloned_service = Service.find(service_id).clone_and_assign_to_administrateur(Administrateur.find(administrateur_id))
cloned_service.save!
procedures_to_fix
.where(service_id: service_id, administrateur_id: administrateur_id)
.update_all(service_id: cloned_service.id)
progress.inc
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: '20181219164523'
end # task :clone_service_for_transferred_procedures
end # namespace :after_party

View file

@ -0,0 +1,32 @@
describe '20181219164523_clone_service_for_transferred_procedures.rake' do
let(:rake_task) { Rake::Task['after_party:clone_service_for_transferred_procedures'] }
subject do
rake_task.invoke
end
after do
rake_task.reenable
end
context 'procedures from different admins share the same service' do
let(:admin1) { create(:administrateur) }
let(:admin2) { create(:administrateur) }
let(:service) { create(:service, administrateur: admin1) }
let!(:procedure1) { create(:procedure, service: service, administrateur: admin1) }
let!(:procedure2) { create(:procedure, service: service, administrateur: admin2) }
let!(:procedure3) { create(:procedure, service: service, administrateur: admin2) }
it 'clones service for procedure2 & procedure3' do
subject
expect(procedure1.reload.service).not_to eq(procedure2.reload.service)
expect(procedure1.reload.service).not_to eq(procedure3.reload.service)
expect(procedure2.reload.service).to eq(procedure3.reload.service)
end
it 'does nothing for procedure1' do
subject
expect(procedure1.reload.service).to eq(service)
end
end
end