Skip to content

Commit e075805

Browse files
committed
bug with fk constraint fixed
1 parent 0c9c3f7 commit e075805

4 files changed

Lines changed: 14 additions & 5 deletions

File tree

lib/models/build.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
class Build < Model
99
belongs_to :repository
1010
has_many :jobs, -> { order('id') }, foreign_key: :source_id, dependent: :destroy, class_name: 'Job'
11+
has_one :repo_for_that_this_build_is_current, foreign_key: :current_build_id, dependent: :destroy, class_name: 'Repository'
1112

1213
self.table_name = 'builds'
1314
end

lib/travis-backup.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ def remove_orphans_for_table(model_class, table_a_name, table_b_name, fk_name)
184184
dry_run_report[key].concat(for_delete.map(&:id))
185185
dry_run_report[key].uniq!
186186
else
187-
model_class.where(id: for_delete.map(&:id)).delete_all
187+
model_class.where(id: for_delete.map(&:id)).destroy_all
188188
end
189189
end
190190

spec/backup_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def destination_logs_size
104104
FactoryBot.create_list(:repository_with_last_build_id, 2)
105105
FactoryBot.create_list(:build_orphaned_on_repository_id, 2)
106106
FactoryBot.create_list(:build_with_repository_id, 2)
107-
FactoryBot.create_list(:build_orphaned_on_commit_id, 2)
107+
FactoryBot.create_list(:build_orphaned_on_commit_id_with_repo, 2)
108108
FactoryBot.create_list(:build_with_commit_id, 2)
109109
FactoryBot.create_list(:build_orphaned_on_request_id, 2)
110110
FactoryBot.create_list(:build_with_request_id, 2)
@@ -149,10 +149,10 @@ def destination_logs_size
149149
FactoryBot.create_list(:stage_with_build_id, 2)
150150
ActiveRecord::Base.connection.execute('alter table repositories enable trigger all;')
151151
}
152-
it 'removes orphaned repositories' do
152+
it 'removes orphaned repositories (with these dependent on orphaned builds)' do
153153
expect {
154154
backup.remove_orphans
155-
}.to change { Repository.all.size }.by -4
155+
}.to change { Repository.all.size }.by -6
156156
end
157157

158158
it 'removes orphaned builds' do

spec/support/factories.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,16 @@
124124
repository_id { Repository.first.id }
125125
end
126126

127-
factory :build_orphaned_on_commit_id do
127+
factory :build_orphaned_on_commit_id_with_repo do
128128
commit_id { 2_000_000_000 }
129+
after(:create) do |build|
130+
create(
131+
:repository,
132+
current_build_id: build.id,
133+
created_at: build.created_at,
134+
updated_at: build.updated_at
135+
)
136+
end
129137
end
130138

131139
factory :build_with_commit_id do

0 commit comments

Comments
 (0)