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 0000000000000000000000000000000000000000..d497a61a9591a6dd5b775f6de4dba314f06e571b --- /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 0000000000000000000000000000000000000000..b4b76893595a3e625d5838c7747c025d2acfc114 --- /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 8c7440ee6102f035b7db57f52d331a69b08dcc33..f34dd32fb7436401671ceee654908ee87cb395d2 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 0000000000000000000000000000000000000000..dc54f4acbf4140da948f2b48c4ca19e523b36e6f --- /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