Commit 7103c4a7 authored by Grzegorz Bizon's avatar Grzegorz Bizon

Extend stages statuses migration

parent 9c7c95c7
...@@ -5,13 +5,17 @@ class MigrateStagesStatuses < ActiveRecord::Migration ...@@ -5,13 +5,17 @@ class MigrateStagesStatuses < ActiveRecord::Migration
disable_ddl_transaction! disable_ddl_transaction!
class Build < ActiveRecord::Base STATUSES = { created: 0, pending: 1, running: 2, success: 3,
self.table_name = 'ci_builds' failed: 4, canceled: 5, skipped: 6, manual: 7 }
scope :relevant, -> do class Stage < ActiveRecord::Base
where(status: %w[pending running success failed canceled skipped manual]) self.table_name = 'ci_stages'
end end
class Build < ActiveRecord::Base
self.table_name = 'ci_builds'
scope :latest, -> { where(retried: [false, nil]) }
scope :created, -> { where(status: 'created') } scope :created, -> { where(status: 'created') }
scope :running, -> { where(status: 'running') } scope :running, -> { where(status: 'running') }
scope :pending, -> { where(status: 'pending') } scope :pending, -> { where(status: 'pending') }
...@@ -27,12 +31,12 @@ class MigrateStagesStatuses < ActiveRecord::Migration ...@@ -27,12 +31,12 @@ class MigrateStagesStatuses < ActiveRecord::Migration
scope :exclude_ignored, -> do scope :exclude_ignored, -> do
where("allow_failure = ? OR status IN (?)", where("allow_failure = ? OR status IN (?)",
false, all_state_names - [:failed, :canceled, :manual]) false, %w[created pending running success skipped])
end end
def status_sql def self.status_sql
scope_relevant = relevant.exclude_ignored scope_relevant = latest.exclude_ignored
scope_warnings = relevant.failed_but_allowed scope_warnings = latest.failed_but_allowed
builds = scope_relevant.select('count(*)').to_sql builds = scope_relevant.select('count(*)').to_sql
created = scope_relevant.created.select('count(*)').to_sql created = scope_relevant.created.select('count(*)').to_sql
...@@ -45,25 +49,34 @@ class MigrateStagesStatuses < ActiveRecord::Migration ...@@ -45,25 +49,34 @@ class MigrateStagesStatuses < ActiveRecord::Migration
warnings = scope_warnings.select('count(*) > 0').to_sql warnings = scope_warnings.select('count(*) > 0').to_sql
"(CASE "(CASE
WHEN (#{builds})=(#{skipped}) AND (#{warnings}) THEN 'success' WHEN (#{builds})=(#{skipped}) AND (#{warnings}) THEN #{STATUSES[:success]}
WHEN (#{builds})=(#{skipped}) THEN 'skipped' WHEN (#{builds})=(#{skipped}) THEN #{STATUSES[:skipped]}
WHEN (#{builds})=(#{success}) THEN 'success' WHEN (#{builds})=(#{success}) THEN #{STATUSES[:success]}
WHEN (#{builds})=(#{created}) THEN 'created' WHEN (#{builds})=(#{created}) THEN #{STATUSES[:created]}
WHEN (#{builds})=(#{success})+(#{skipped}) THEN 'success' WHEN (#{builds})=(#{success})+(#{skipped}) THEN #{STATUSES[:success]}
WHEN (#{builds})=(#{success})+(#{skipped})+(#{canceled}) THEN 'canceled' WHEN (#{builds})=(#{success})+(#{skipped})+(#{canceled}) THEN #{STATUSES[:canceled]}
WHEN (#{builds})=(#{created})+(#{skipped})+(#{pending}) THEN 'pending' WHEN (#{builds})=(#{created})+(#{skipped})+(#{pending}) THEN #{STATUSES[:pending]}
WHEN (#{running})+(#{pending})>0 THEN 'running' WHEN (#{running})+(#{pending})>0 THEN '#{STATUSES[:running]}
WHEN (#{manual})>0 THEN 'manual' WHEN (#{manual})>0 THEN #{STATUSES[:manual]}
WHEN (#{created})>0 THEN 'running' WHEN (#{created})>0 THEN #{STATUSES[:running]}
ELSE 'failed' ELSE #{STATUSES[:failed]}
END)" END)"
end end
end end
def up def up
Stage.all.in_batches(of: 10000) do |relation|
status_sql = Build
.where('ci_builds.commit_id = ci_stages.pipeline_id')
.where('ci_builds.stage = ci_stages.name')
.status_sql
execute <<-SQL.strip_heredoc execute <<-SQL.strip_heredoc
UPDATE ci_stages SET status = #{status_sql}
WHERE id = (#{relation.select(:id).to_sql})
SQL SQL
end end
end
def down def down
execute <<-SQL.strip_heredoc execute <<-SQL.strip_heredoc
......
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