Commit 95f15dc5 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Clean up pending builds on GitLab.com in batches

parent 3c4178a8
# frozen_string_literal: true
class CleanUpPendingBuildsTable < ActiveRecord::Migration[6.0]
BATCH_SIZE = 1000
disable_ddl_transaction!
def up
execute <<~SQL
DELETE FROM ci_pending_builds
USING ci_builds
WHERE ci_builds.id = ci_pending_builds.build_id
AND ci_builds.status != 'pending'
AND ci_builds.type = 'Ci::Build'
SQL
return unless Gitlab.com?
each_batch('ci_pending_builds', of: BATCH_SIZE) do |min, max|
execute <<~SQL
DELETE FROM ci_pending_builds
USING ci_builds
WHERE ci_builds.id = ci_pending_builds.build_id
AND ci_builds.status != 'pending'
AND ci_builds.type = 'Ci::Build'
AND ci_pending_builds.id BETWEEN #{min} AND #{max}
SQL
end
end
def down
# noop
end
private
def each_batch(table_name, scope: ->(table) { table.all }, of: 1000)
table = Class.new(ActiveRecord::Base) do
include EachBatch
self.table_name = table_name
self.inheritance_column = :_type_disabled
end
scope.call(table).each_batch(of: of) do |batch|
yield batch.pluck('MIN(id), MAX(id)').first
end
end
end
......@@ -10,6 +10,8 @@ RSpec.describe CleanUpPendingBuildsTable do
let(:builds) { table(:ci_builds) }
before do
allow(Gitlab).to receive(:com?).and_return(true)
namespaces.create!(id: 123, name: 'sample', path: 'sample')
projects.create!(id: 123, name: 'sample', path: 'sample', namespace_id: 123)
......@@ -32,4 +34,16 @@ RSpec.describe CleanUpPendingBuildsTable do
expect(queue.first.id).to eq 1
expect(builds.all.count).to eq 6
end
context 'when there are multiple batches' do
before do
stub_const("#{described_class}::BATCH_SIZE", 1)
end
it 'iterates the data correctly' do
migrate!
expect(queue.all.count).to eq 1
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment