summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-06-30 14:22:23 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-06-30 14:22:23 +0200
commit9c7c95c768cd5294dd085c2fc2425fae91c4c689 (patch)
treef05de41bee3d35f451bfe0ef22fd2cc929d7ce1a
parent81dba76b9d7d120cd22e3619a4058bd4885be9bc (diff)
downloadgitlab-ce-9c7c95c768cd5294dd085c2fc2425fae91c4c689.tar.gz
Add initial changes for stages statuses migration
-rw-r--r--db/migrate/20170630105320_add_status_to_ci_stages.rb9
-rw-r--r--db/post_migrate/20170630111158_migrate_stages_statuses.rb76
-rw-r--r--db/schema.rb3
-rw-r--r--spec/migrations/migrate_stages_statuses_spec.rb50
4 files changed, 137 insertions, 1 deletions
diff --git a/db/migrate/20170630105320_add_status_to_ci_stages.rb b/db/migrate/20170630105320_add_status_to_ci_stages.rb
new file mode 100644
index 00000000000..d497a61a959
--- /dev/null
+++ b/db/migrate/20170630105320_add_status_to_ci_stages.rb
@@ -0,0 +1,9 @@
+class AddStatusToCiStages < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ def change
+ add_column :ci_stages, :status, :integer
+ end
+end
diff --git a/db/post_migrate/20170630111158_migrate_stages_statuses.rb b/db/post_migrate/20170630111158_migrate_stages_statuses.rb
new file mode 100644
index 00000000000..b4b76893595
--- /dev/null
+++ b/db/post_migrate/20170630111158_migrate_stages_statuses.rb
@@ -0,0 +1,76 @@
+class MigrateStagesStatuses < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ disable_ddl_transaction!
+
+ class Build < ActiveRecord::Base
+ self.table_name = 'ci_builds'
+
+ scope :relevant, -> do
+ where(status: %w[pending running success failed canceled skipped manual])
+ end
+
+ scope :created, -> { where(status: 'created') }
+ scope :running, -> { where(status: 'running') }
+ scope :pending, -> { where(status: 'pending') }
+ scope :success, -> { where(status: 'success') }
+ scope :failed, -> { where(status: 'failed') }
+ scope :canceled, -> { where(status: 'canceled') }
+ scope :skipped, -> { where(status: 'skipped') }
+ scope :manual, -> { where(status: 'manual') }
+
+ scope :failed_but_allowed, -> do
+ where(allow_failure: true, status: [:failed, :canceled])
+ end
+
+ scope :exclude_ignored, -> do
+ where("allow_failure = ? OR status IN (?)",
+ false, all_state_names - [:failed, :canceled, :manual])
+ end
+
+ def status_sql
+ scope_relevant = relevant.exclude_ignored
+ scope_warnings = relevant.failed_but_allowed
+
+ builds = scope_relevant.select('count(*)').to_sql
+ created = scope_relevant.created.select('count(*)').to_sql
+ success = scope_relevant.success.select('count(*)').to_sql
+ manual = scope_relevant.manual.select('count(*)').to_sql
+ pending = scope_relevant.pending.select('count(*)').to_sql
+ running = scope_relevant.running.select('count(*)').to_sql
+ skipped = scope_relevant.skipped.select('count(*)').to_sql
+ canceled = scope_relevant.canceled.select('count(*)').to_sql
+ warnings = scope_warnings.select('count(*) > 0').to_sql
+
+ "(CASE
+ WHEN (#{builds})=(#{skipped}) AND (#{warnings}) THEN 'success'
+ WHEN (#{builds})=(#{skipped}) THEN 'skipped'
+ WHEN (#{builds})=(#{success}) THEN 'success'
+ WHEN (#{builds})=(#{created}) THEN 'created'
+ WHEN (#{builds})=(#{success})+(#{skipped}) THEN 'success'
+ WHEN (#{builds})=(#{success})+(#{skipped})+(#{canceled}) THEN 'canceled'
+ WHEN (#{builds})=(#{created})+(#{skipped})+(#{pending}) THEN 'pending'
+ WHEN (#{running})+(#{pending})>0 THEN 'running'
+ WHEN (#{manual})>0 THEN 'manual'
+ WHEN (#{created})>0 THEN 'running'
+ ELSE 'failed'
+ END)"
+ end
+ end
+
+ def up
+ execute <<-SQL.strip_heredoc
+ SQL
+ end
+
+ def down
+ execute <<-SQL.strip_heredoc
+ UPDATE ci_stages SET status = null
+ SQL
+ end
+
+ private
+
+end
diff --git a/db/schema.rb b/db/schema.rb
index 8c7440ee610..f34dd32fb74 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20170622162730) do
+ActiveRecord::Schema.define(version: 20170630111158) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -337,6 +337,7 @@ ActiveRecord::Schema.define(version: 20170622162730) do
t.datetime "created_at"
t.datetime "updated_at"
t.string "name"
+ t.integer "status"
end
add_index "ci_stages", ["pipeline_id", "name"], name: "index_ci_stages_on_pipeline_id_and_name", using: :btree
diff --git a/spec/migrations/migrate_stages_statuses_spec.rb b/spec/migrations/migrate_stages_statuses_spec.rb
new file mode 100644
index 00000000000..dc54f4acbf4
--- /dev/null
+++ b/spec/migrations/migrate_stages_statuses_spec.rb
@@ -0,0 +1,50 @@
+require 'spec_helper'
+require Rails.root.join('db', 'post_migrate', '20170630111158_migrate_stages_statuses.rb')
+
+describe MigrateStagesStatuses, :migration do
+ let(:jobs) { table(:ci_builds) }
+ let(:stages) { table(:ci_stages) }
+ let(:pipelines) { table(:ci_pipelines) }
+ let(:projects) { table(:projects) }
+
+ STATUSES = { created: 0, pending: 1, running: 2, success: 3,
+ failed: 4, canceled: 5, skipped: 6, manual: 7 }
+ STAGES = { test: 1, build: 2, deploy: 3}
+
+ before do
+ projects.create!(id: 1, name: 'gitlab1', path: 'gitlab1')
+ projects.create!(id: 2, name: 'gitlab2', path: 'gitlab2')
+
+ pipelines.create!(id: 1, project_id: 123, ref: 'master', sha: 'adf43c3a')
+ pipelines.create!(id: 2, project_id: 456, ref: 'feature', sha: '21a3deb')
+
+ create_job(project: 1, pipeline: 1, stage: 'test', status: 'success')
+ create_job(project: 1, pipeline: 1, stage: 'test', status: 'running')
+ create_job(project: 1, pipeline: 1, stage: 'build', status: 'success')
+ create_job(project: 1, pipeline: 1, stage: 'build', status: 'failed')
+ create_job(project: 2, pipeline: 2, stage: 'test', status: 'success')
+ create_job(project: 2, pipeline: 2, stage: 'test', status: 'succcss')
+
+ stages.create!(id: 1, pipeline_id: 1, project_id: 1, status: nil)
+ stages.create!(id: 2, pipeline_id: 1, project_id: 1, status: nil)
+ stages.create!(id: 3, pipeline_id: 2, project_id: 2, status: nil)
+ end
+
+ pending 'correctly migrates stages statuses' do
+ expect(stages.where(status: nil).count).to eq 3
+
+ migrate!
+
+ expect(stages.where(status: nil)).to be_empty
+ expect(stages.all.order(:id, :asc).pluck(:stage))
+ .to eq %w[running success failed]
+ end
+
+ def create_job(project:, pipeline:, stage:, status:)
+ stage_idx = STAGES[stage.to_sym]
+ status_id = STATUSES[status.to_sym]
+
+ jobs.create!(project_id: project, commit_id: pipeline,
+ stage_idx: stage_idx, stage: stage, status: status_id)
+ end
+end