diff --git a/app/services/piece_justificative_to_champ_piece_jointe_migration_service.rb b/app/services/piece_justificative_to_champ_piece_jointe_migration_service.rb index dc5a02c29..b59a93c86 100644 --- a/app/services/piece_justificative_to_champ_piece_jointe_migration_service.rb +++ b/app/services/piece_justificative_to_champ_piece_jointe_migration_service.rb @@ -54,15 +54,7 @@ class PieceJustificativeToChampPieceJointeMigrationService # If anything goes wrong, we roll back the migration by destroying the newly created # types de champ, champs blobs and attachments. rake_puts "Error received. Rolling back migration of procedure #{procedure.id}…" - - types_de_champ_pj.each do |type_champ| - # First destroy all the individual champs on dossiers - type_champ.champ.each { |c| destroy_champ_pj(c.dossier.reload, c) } - # Now we can destroy the type de champ itself, - # without cascading the timestamp update on all attached dossiers. - type_champ.reload.destroy - end - + rollback_migration!(types_de_champ_pj) rake_puts "Migration of procedure #{procedure.id} rolled back." # Reraise the exception to abort the migration. @@ -144,6 +136,23 @@ class PieceJustificativeToChampPieceJointeMigrationService raise end + def rollback_migration!(types_de_champ_pj) + types_de_champ_pj.each do |type_champ| + # First destroy all the individual champs on dossiers + type_champ.champ.each do |champ| + begin + destroy_champ_pj(champ.dossier.reload, champ) + rescue => e + rake_puts e + rake_puts "Rolling back of champ #{champ.id} failed. Continuing to roll back…" + end + end + # Now we can destroy the type de champ itself, + # without cascading the timestamp update on all attached dossiers. + type_champ.reload.destroy + end + end + def make_blob(pj) storage_service.make_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename) end diff --git a/spec/services/piece_justificative_to_champ_piece_jointe_migration_service_spec.rb b/spec/services/piece_justificative_to_champ_piece_jointe_migration_service_spec.rb index b1e96df09..d9641c47a 100644 --- a/spec/services/piece_justificative_to_champ_piece_jointe_migration_service_spec.rb +++ b/spec/services/piece_justificative_to_champ_piece_jointe_migration_service_spec.rb @@ -276,5 +276,26 @@ describe PieceJustificativeToChampPieceJointeMigrationService do expect { try_convert(procedure) }.not_to change { dossier.champs.count } end end + + context 'when rolling back a dossier fails' do + before do + allow(service).to receive(:destroy_champ_pj) + .with(having_attributes(id: dossier.id), anything) + .and_raise(StandardError) + allow(service).to receive(:destroy_champ_pj) + .with(any_args) + .and_call_original + end + + it 'continues to roll back the other dossiers' do + expect { try_convert(procedure) } + .not_to change { failing_dossier.champs.count } + end + + it 'does not creates types de champ on the procedure' do + expect { try_convert(procedure) } + .not_to change { procedure.types_de_champ.count } + end + end end end