summaryrefslogtreecommitdiff
path: root/spec
diff options
context:
space:
mode:
authorRobert Speicher <robert@gitlab.com>2018-05-29 17:47:47 +0000
committerRobert Speicher <robert@gitlab.com>2018-05-29 17:47:47 +0000
commitefcadd2d3c3eaa1e8d25275d8aef357a2ffb6fcb (patch)
tree2ef38d817a39be3bf53f973404dc84536a3fc6c8 /spec
parent71b2eb7d935ab4f5ee3d901dad259d9fc02b1a22 (diff)
parent760fdd1dd31d30d5ab407a0c42e864040d79504c (diff)
downloadgitlab-ce-efcadd2d3c3eaa1e8d25275d8aef357a2ffb6fcb.tar.gz
Merge branch 'sh-batch-dependent-destroys' into 'master'
Fix project destruction failing due to idle in transaction timeouts Closes #44610 See merge request gitlab-org/gitlab-ce!18609
Diffstat (limited to 'spec')
-rw-r--r--spec/models/concerns/batch_destroy_dependent_associations_spec.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/spec/models/concerns/batch_destroy_dependent_associations_spec.rb b/spec/models/concerns/batch_destroy_dependent_associations_spec.rb
new file mode 100644
index 00000000000..c16b245bea8
--- /dev/null
+++ b/spec/models/concerns/batch_destroy_dependent_associations_spec.rb
@@ -0,0 +1,60 @@
+require 'spec_helper'
+
+describe BatchDestroyDependentAssociations do
+ class TestProject < ActiveRecord::Base
+ self.table_name = 'projects'
+
+ has_many :builds, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
+ has_many :notification_settings, as: :source, dependent: :delete_all # rubocop:disable Cop/ActiveRecordDependent
+ has_many :pages_domains
+ has_many :todos
+
+ include BatchDestroyDependentAssociations
+ end
+
+ describe '#dependent_associations_to_destroy' do
+ set(:project) { TestProject.new }
+
+ it 'returns the right associations' do
+ expect(project.dependent_associations_to_destroy.map(&:name)).to match_array([:builds])
+ end
+ end
+
+ describe '#destroy_dependent_associations_in_batches' do
+ set(:project) { create(:project) }
+ set(:build) { create(:ci_build, project: project) }
+ set(:notification_setting) { create(:notification_setting, project: project) }
+ let!(:todos) { create(:todo, project: project) }
+
+ it 'destroys multiple builds' do
+ create(:ci_build, project: project)
+
+ expect(Ci::Build.count).to eq(2)
+
+ project.destroy_dependent_associations_in_batches
+
+ expect(Ci::Build.count).to eq(0)
+ end
+
+ it 'destroys builds in batches' do
+ expect(project).to receive_message_chain(:builds, :find_each).and_yield(build)
+ expect(build).to receive(:destroy).and_call_original
+
+ project.destroy_dependent_associations_in_batches
+
+ expect(Ci::Build.count).to eq(0)
+ expect(Todo.count).to eq(1)
+ expect(User.count).to be > 0
+ expect(NotificationSetting.count).to eq(User.count)
+ end
+
+ it 'excludes associations' do
+ project.destroy_dependent_associations_in_batches(exclude: [:builds])
+
+ expect(Ci::Build.count).to eq(1)
+ expect(Todo.count).to eq(1)
+ expect(User.count).to be > 0
+ expect(NotificationSetting.count).to eq(User.count)
+ end
+ end
+end