Skip to content

Commit

Permalink
Showing 4 changed files with 14 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/models/build.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/travis-backup.rb
Original file line number Diff line number Diff line change
@@ -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

6 changes: 3 additions & 3 deletions spec/backup_spec.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 9 additions & 1 deletion spec/support/factories.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e075805

Please sign in to comment.