Merge pull request #3970 from betagouv/fix-pj-migration-rollback
Migration des PJ : gestion des erreurs qui peuvent se produire pendant le rollback
This commit is contained in:
commit
d71b3ade80
2 changed files with 39 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue