Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatija Čupić <matteeyah@gmail.com>2018-09-19 19:54:03 +0300
committerMatija Čupić <matteeyah@gmail.com>2018-09-25 22:09:14 +0300
commitc2fbd8677c27b28d68efab3bfd097a246e083b98 (patch)
tree20884426839dd40aa8c5dc30f00768538948bbb6
parent97fbc2e5cd2dece8575a0d26f2db4fc502e5a9ba (diff)
Add background migration to fill pipeline source
-rw-r--r--lib/gitlab/background_migration/populate_external_pipeline_source.rb54
-rw-r--r--spec/lib/gitlab/background_migration/populate_external_pipeline_source_spec.rb35
2 files changed, 89 insertions, 0 deletions
diff --git a/lib/gitlab/background_migration/populate_external_pipeline_source.rb b/lib/gitlab/background_migration/populate_external_pipeline_source.rb
new file mode 100644
index 00000000000..c2686d5fd27
--- /dev/null
+++ b/lib/gitlab/background_migration/populate_external_pipeline_source.rb
@@ -0,0 +1,54 @@
+# 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
diff --git a/spec/lib/gitlab/background_migration/populate_external_pipeline_source_spec.rb b/spec/lib/gitlab/background_migration/populate_external_pipeline_source_spec.rb
new file mode 100644
index 00000000000..71cdc5e364c
--- /dev/null
+++ b/spec/lib/gitlab/background_migration/populate_external_pipeline_source_spec.rb
@@ -0,0 +1,35 @@
+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