Commit 9c7c95c7 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Add initial changes for stages statuses migration

parent 81dba76b
class AddStatusToCiStages < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def change
add_column :ci_stages, :status, :integer
end
end
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
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # 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 # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
...@@ -337,6 +337,7 @@ ActiveRecord::Schema.define(version: 20170622162730) do ...@@ -337,6 +337,7 @@ ActiveRecord::Schema.define(version: 20170622162730) do
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.string "name" t.string "name"
t.integer "status"
end end
add_index "ci_stages", ["pipeline_id", "name"], name: "index_ci_stages_on_pipeline_id_and_name", using: :btree add_index "ci_stages", ["pipeline_id", "name"], name: "index_ci_stages_on_pipeline_id_and_name", using: :btree
......
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
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