Refactor external_pipelines query

This commit is contained in:
Matija Čupić 2018-09-27 19:28:24 +02:00
parent 6075a9323a
commit 4de93b6a3e
No known key found for this signature in database
GPG key ID: 4BAF84FFACD2E5DE
2 changed files with 6 additions and 9 deletions

View file

@ -24,18 +24,15 @@ module Gitlab
class CommitStatus < ActiveRecord::Base
self.table_name = 'ci_builds'
self.inheritance_column = :_type_disabled
end
class Build < CommitStatus
end
class GenericCommitStatus < CommitStatus
scope :has_pipeline, -> { where('ci_builds.commit_id=ci_pipelines.id') }
scope :of_type, -> (type) { where('type=?', type) }
end
end
def perform(start_id, stop_id)
external_pipelines(start_id, stop_id)
.update_all(:source, Migratable::Pipeline.sources[:external])
.update_all(source: Migratable::Pipeline.sources[:external])
end
private
@ -44,8 +41,8 @@ module Gitlab
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)
Migratable::CommitStatus.of_type('GenericCommitStatus').has_pipeline.select(1),
Migratable::CommitStatus.of_type('Ci::Build').has_pipeline.select(1),
)
end
end

View file

@ -12,7 +12,7 @@ describe Gitlab::BackgroundMigration::PopulateExternalPipelineSource, :migration
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!(:status) { statuses.create(id: 1, commit_id: 2, type: 'GenericCommitStatus') }
let!(:build) { builds.create(id: 2, commit_id: 1, type: 'Ci::Build') }
subject { migration.perform(1, 2) }