diff --git a/lib/models/build.rb b/lib/models/build.rb index dc4f04f..b5aeb35 100644 --- a/lib/models/build.rb +++ b/lib/models/build.rb @@ -8,6 +8,7 @@ class Build < Model belongs_to :repository has_many :jobs, -> { order('id') }, foreign_key: :source_id, dependent: :destroy, class_name: 'Job' + has_one :repo_for_that_this_build_is_current, foreign_key: :current_build_id, dependent: :destroy, class_name: 'Repository' self.table_name = 'builds' end diff --git a/lib/travis-backup.rb b/lib/travis-backup.rb index eb82ce7..1175741 100644 --- a/lib/travis-backup.rb +++ b/lib/travis-backup.rb @@ -184,7 +184,7 @@ def remove_orphans_for_table(model_class, table_a_name, table_b_name, fk_name) dry_run_report[key].concat(for_delete.map(&:id)) dry_run_report[key].uniq! else - model_class.where(id: for_delete.map(&:id)).delete_all + model_class.where(id: for_delete.map(&:id)).destroy_all end end diff --git a/spec/backup_spec.rb b/spec/backup_spec.rb index 8772bac..7f48010 100644 --- a/spec/backup_spec.rb +++ b/spec/backup_spec.rb @@ -104,7 +104,7 @@ def destination_logs_size FactoryBot.create_list(:repository_with_last_build_id, 2) FactoryBot.create_list(:build_orphaned_on_repository_id, 2) FactoryBot.create_list(:build_with_repository_id, 2) - FactoryBot.create_list(:build_orphaned_on_commit_id, 2) + FactoryBot.create_list(:build_orphaned_on_commit_id_with_repo, 2) FactoryBot.create_list(:build_with_commit_id, 2) FactoryBot.create_list(:build_orphaned_on_request_id, 2) FactoryBot.create_list(:build_with_request_id, 2) @@ -149,10 +149,10 @@ def destination_logs_size FactoryBot.create_list(:stage_with_build_id, 2) ActiveRecord::Base.connection.execute('alter table repositories enable trigger all;') } - it 'removes orphaned repositories' do + it 'removes orphaned repositories (with these dependent on orphaned builds)' do expect { backup.remove_orphans - }.to change { Repository.all.size }.by -4 + }.to change { Repository.all.size }.by -6 end it 'removes orphaned builds' do diff --git a/spec/support/factories.rb b/spec/support/factories.rb index 87447f8..17b2bdb 100644 --- a/spec/support/factories.rb +++ b/spec/support/factories.rb @@ -124,8 +124,16 @@ repository_id { Repository.first.id } end - factory :build_orphaned_on_commit_id do + factory :build_orphaned_on_commit_id_with_repo do commit_id { 2_000_000_000 } + after(:create) do |build| + create( + :repository, + current_build_id: build.id, + created_at: build.created_at, + updated_at: build.updated_at + ) + end end factory :build_with_commit_id do