Commit 0e39ae3f authored by Grzegorz Bizon's avatar Grzegorz Bizon

Remove pending builds data migration

parent 4661cf85
# frozen_string_literal: true
class CopyPendingBuildsToPendingBuildsTable < ActiveRecord::Migration[6.0]
include Gitlab::Database::MigrationHelpers
BATCH_SIZE = 10000
disable_ddl_transaction!
class Build < ActiveRecord::Base
include EachBatch
self.table_name = 'ci_builds'
self.inheritance_column = :_type_disabled
scope :pending, -> do
where(status: 'pending')
.where(type: 'Ci::Build')
.where('updated_at > ?', 24.hours.ago)
.order('id ASC')
end
end
def up
Build.pending.limit(1).pluck(:id).first.try do |id|
Build.where('id >= ?', id).each_batch(of: BATCH_SIZE) do |batch|
min_id, max_id = batch.pluck('MIN(id), MAX(id)').first
execute <<~SQL
WITH pending_builds AS (
SELECT id,
project_id
FROM ci_builds
WHERE status = 'pending'
AND type = 'Ci::Build'
AND NOT EXISTS (
SELECT 1 FROM ci_pending_builds
WHERE ci_pending_builds.build_id = ci_builds.id
)
AND id BETWEEN #{min_id} AND #{max_id}
)
INSERT INTO ci_pending_builds (build_id, project_id)
SELECT id,
project_id
FROM pending_builds
ON CONFLICT DO NOTHING
SQL
end
end
end
def down
# noop
end
end
8eade0cf920391deb0ee1d6c7eee765b8ffc6e08c7c37ef34ef6062c9049609e
\ No newline at end of file
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'post_migrate', '20210520105029_copy_pending_builds_to_pending_builds_table.rb')
RSpec.describe CopyPendingBuildsToPendingBuildsTable do
let(:namespaces) { table(:namespaces) }
let(:projects) { table(:projects) }
let(:queue) { table(:ci_pending_builds) }
let(:builds) { table(:ci_builds) }
before do
namespaces.create!(id: 123, name: 'sample', path: 'sample')
projects.create!(id: 123, name: 'sample', path: 'sample', namespace_id: 123)
builds.create!(id: 1, project_id: 123, status: 'pending', type: 'Ci::Build', created_at: 4.days.ago, updated_at: 4.days.ago)
builds.create!(id: 2, project_id: 123, status: 'pending', type: 'Ci::Build')
builds.create!(id: 3, project_id: 123, status: 'pending', type: 'GenericCommitStatus')
builds.create!(id: 4, project_id: 123, status: 'pending', type: 'GenericCommitStatus')
builds.create!(id: 5, project_id: 123, status: 'pending', type: 'Ci::Bridge')
builds.create!(id: 6, project_id: 123, status: 'pending', type: 'Ci::Build')
builds.create!(id: 7, project_id: 123, status: 'running', type: 'Ci::Build')
builds.create!(id: 8, project_id: 123, status: 'created', type: 'Ci::Build')
end
context 'when there are new pending builds present' do
it 'migrates data' do
migrate!
expect(queue.all.count).to eq 2
expect(queue.all.pluck(:build_id)).to match_array([2, 6])
end
end
context 'when there are pending builds already migrated present' do
before do
queue.create!(id: 1, build_id: 2, project_id: 123)
end
it 'does not copy entries that have already been copied' do
expect(queue.all.count).to eq 1
migrate!
expect(queue.all.count).to eq 2
expect(queue.all.pluck(:build_id)).to match_array([2, 6])
end
end
context 'when there is more than one batch of pending builds to migrate' do
before do
stub_const("#{described_class}::BATCH_SIZE", 2)
end
it 'correctly migrates data and exits after doing that' do
migrate!
expect(queue.all.count).to eq 2
expect(queue.all.pluck(:build_id)).to match_array([2, 6])
end
end
context 'when an old build has been recently updated' do
before do
builds.find(1).touch
end
it 'migrates it as well' do
migrate!
expect(queue.all.count).to eq 3
expect(queue.all.pluck(:build_id)).to match_array([1, 2, 6])
end
end
context 'when there is no data to migrate' do
before do
builds.all.delete_all
end
it 'does not raise an exception' do
expect(builds.all.count).to eq 0
expect { migrate! }.not_to raise_error
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