From 91393be13cbb0f6f47033449d77ae6c55f211068 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 18 Jun 2019 13:09:05 +0000 Subject: [PATCH 1/2] tasks: extract rollback to a separate method --- ...to_champ_piece_jointe_migration_service.rb | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) 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..a31713eea 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,16 @@ 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 { |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 + end + def make_blob(pj) storage_service.make_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename) end From f8dda3ae45a33b14b8685533d39ee76b36e4475e Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Tue, 18 Jun 2019 13:28:26 +0000 Subject: [PATCH 2/2] tasks: don't abort rollback when a dossier fails Instead print a warning, and continue rolling back the other dossiers. --- ...to_champ_piece_jointe_migration_service.rb | 9 +++++++- ...amp_piece_jointe_migration_service_spec.rb | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) 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 a31713eea..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 @@ -139,7 +139,14 @@ class PieceJustificativeToChampPieceJointeMigrationService 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 { |c| destroy_champ_pj(c.dossier.reload, c) } + 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 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