From a8847e40ea3585cbc5af3e4aecde7b10aa856a38 Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Wed, 29 May 2019 13:06:57 +0200 Subject: [PATCH] pj migration: add comments and notices --- ...ustificative_to_champ_piece_jointe_migration_service.rb | 7 +++++++ 1 file changed, 7 insertions(+) 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 ed6597e94..18d37251e 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 @@ -40,9 +40,12 @@ class PieceJustificativeToChampPieceJointeMigrationService # Unscope to make sure all dossiers are migrated, even the soft-deleted ones procedure.dossiers.unscope(where: :hidden_at).includes(:champs).find_each do |dossier| + # Add the new pieces justificatives champs to the dossier champs_pj = types_de_champ_pj.map(&:build_champ) dossier.champs += champs_pj + # Copy the dossier old pieces jointes to the new champs + # (even if the champs already existed, so that we ensure a clean state) champs_pj.each do |champ| type_pj_id = champ.type_de_champ.old_pj&.fetch('stable_id', nil) pj = dossier.retrieve_last_piece_justificative_by_type(type_pj_id) @@ -68,11 +71,15 @@ class PieceJustificativeToChampPieceJointeMigrationService rescue StandardError, SignalException # 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| type_champ.champ.each { |c| c.piece_justificative_file.purge } type_champ.destroy end + rake_puts "Migration of procedure #{procedure.id} rolled back." + # Reraise the exception to abort the migration. raise end