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:
Nicolas Bouilleaud 2019-06-20 10:42:50 +02:00 committed by GitHub
commit d71b3ade80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 9 deletions

View file

@ -54,15 +54,7 @@ class PieceJustificativeToChampPieceJointeMigrationService
# If anything goes wrong, we roll back the migration by destroying the newly created # If anything goes wrong, we roll back the migration by destroying the newly created
# types de champ, champs blobs and attachments. # types de champ, champs blobs and attachments.
rake_puts "Error received. Rolling back migration of procedure #{procedure.id}" rake_puts "Error received. Rolling back migration of procedure #{procedure.id}"
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
rake_puts "Migration of procedure #{procedure.id} rolled back." rake_puts "Migration of procedure #{procedure.id} rolled back."
# Reraise the exception to abort the migration. # Reraise the exception to abort the migration.
@ -144,6 +136,23 @@ class PieceJustificativeToChampPieceJointeMigrationService
raise raise
end 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) def make_blob(pj)
storage_service.make_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename) storage_service.make_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename)
end end

View file

@ -276,5 +276,26 @@ describe PieceJustificativeToChampPieceJointeMigrationService do
expect { try_convert(procedure) }.not_to change { dossier.champs.count } expect { try_convert(procedure) }.not_to change { dossier.champs.count }
end end
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
end end