diff --git a/lib/cleanup/destroyable_records.rb b/lib/cleanup/destroyable_records.rb index 47746e7ba6b..a329b1a3cd9 100644 --- a/lib/cleanup/destroyable_records.rb +++ b/lib/cleanup/destroyable_records.rb @@ -26,7 +26,7 @@ def print_data stdout.puts '********' stdout.puts 'Integration:' - if integration.blank? + if integration.nil? stdout.puts 'No associated integration' else stdout.puts integration.attributes.to_yaml @@ -58,15 +58,14 @@ def print_data end def destroy_records - stdout.puts 'Destroying integration usages' - integration_usages.each do |integration_usage| - integration_usage.destroy! + if integration.present? + stdout.puts 'Destroying integration usages' + integration_usages.destroy_all + integration.reload + stdout.puts "Destroying integration with issuer #{integration.issuer}" + integration.destroy! + service_provider.reload end - integration.reload - - stdout.puts "Destroying integration with issuer #{integration.issuer}" - integration.destroy! - service_provider.reload stdout.puts "Destroying service provider issuer #{service_provider.issuer}" service_provider.destroy! @@ -79,11 +78,11 @@ def destroy_records private def integration_usages - integration&.integration_usages || [] + integration&.integration_usages end def iaa_orders - integration&.iaa_orders || [] + integration&.iaa_orders end def in_person_enrollments diff --git a/spec/lib/cleanup/destroyable_records_spec.rb b/spec/lib/cleanup/destroyable_records_spec.rb index 269379bf495..752430df2ef 100644 --- a/spec/lib/cleanup/destroyable_records_spec.rb +++ b/spec/lib/cleanup/destroyable_records_spec.rb @@ -106,16 +106,27 @@ expect(iaa_order.integrations.include? integration).to be false end - describe 'integration without usages or iaa_orders' do - let!(:empty_integration) { create(:integration) } - let!(:service_provider) { empty_integration.service_provider } + context 'integration without integration usages' do + # int factory has no usages by default + let!(:integration) { create(:integration) } + let!(:service_provider) { integration.service_provider } it 'destroys the integration' do - deleted_int = Agreements::Integration.find_by(id: empty_integration.id) + deleted_int = Agreements::Integration.find_by(id: integration.id) expect(deleted_int).to be nil end end + context 'no integration' do + # sp factory has no integrations by default + let!(:service_provider) { create(:service_provider) } + + it 'destroys the service provider' do + deleted_sp = ServiceProvider.find_by(id: service_provider.id) + expect(deleted_sp).to be nil + end + end + it 'does not delete unrelated objects' do iu2.reload iaa_order.reload