Commit c2fbd867 authored by Matija Čupić's avatar Matija Čupić

Add background migration to fill pipeline source

parent 97fbc2e5
# frozen_string_literal: true
# rubocop:disable Style/Documentation
module Gitlab
module BackgroundMigration
class PopulateExternalPipelineSource
module Migratable
class Pipeline < ActiveRecord::Base
self.table_name = 'ci_pipelines'
def self.sources
{
unknown: nil,
push: 1,
web: 2,
trigger: 3,
schedule: 4,
api: 5,
external: 6
}
end
end
class CommitStatus < ActiveRecord::Base
self.table_name = 'ci_builds'
self.inheritance_column = :_type_disabled
end
class Build < CommitStatus
end
class GenericCommitStatus < CommitStatus
end
end
def perform(start_id, stop_id)
external_pipelines(start_id, stop_id).each do |pipeline|
pipeline.update_attribute(:source, Migratable::Pipeline.sources[:external])
end
end
private
def external_pipelines(start_id, stop_id)
Migratable::Pipeline.where(id: (start_id..stop_id))
.where(
'EXISTS (?) AND NOT EXISTS (?)',
Migratable::GenericCommitStatus.where("type='CommitStatus'").where('ci_builds.commit_id=ci_pipelines.id').select(1),
Migratable::Build.where("type='Ci::Build'").where('ci_builds.commit_id=ci_pipelines.id').select(1)
)
end
end
end
end
require 'spec_helper'
describe Gitlab::BackgroundMigration::PopulateExternalPipelineSource, :migration, schema: 20180916011959 do
let(:migration) { described_class.new }
let(:projects) { table(:projects) }
let(:pipelines) { table(:ci_pipelines) }
let(:statuses) { table(:ci_builds) }
let(:builds) { table(:ci_builds) }
let!(:internal_pipeline) { pipelines.create(id: 1, source: described_class::Migratable::Pipeline.sources[:web]) }
let!(:external_pipeline) { pipelines.create(id: 2, source: nil) }
let!(:second_external_pipeline) { pipelines.create(id: 3, source: nil) }
let!(:status) { statuses.create(id: 1, commit_id: 2, type: 'CommitStatus') }
let!(:build) { builds.create(id: 2, commit_id: 1, type: 'Ci::Build') }
subject { migration.perform(1, 2) }
it 'populates the pipeline source' do
subject
expect(external_pipeline.reload.source).to eq(described_class::Migratable::Pipeline.sources[:external])
end
it 'only processes a single batch of links at a time' do
subject
expect(second_external_pipeline.reload.source).to eq(nil)
end
it 'can be repeated without effect' do
subject
expect { subject }.not_to change { external_pipeline.reload.source }
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