debian-mirror-gitlab/db/post_migrate/20180119121225_remove_redundant_pipeline_stages.rb

59 lines
1.5 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
class RemoveRedundantPipelineStages < ActiveRecord::Migration[4.2]
2018-03-17 18:26:18 +05:30
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up(attempts: 100)
remove_redundant_pipeline_stages!
remove_outdated_index!
add_unique_index!
rescue ActiveRecord::RecordNotUnique
retry if (attempts -= 1) > 0
raise StandardError, <<~EOS
Failed to add an unique index to ci_stages, despite retrying the
migration 100 times.
2019-12-04 20:38:33 +05:30
See https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/16580.
2018-03-17 18:26:18 +05:30
EOS
end
def down
remove_concurrent_index :ci_stages, [:pipeline_id, :name], unique: true
add_concurrent_index :ci_stages, [:pipeline_id, :name]
end
private
def remove_outdated_index!
return unless index_exists?(:ci_stages, [:pipeline_id, :name])
remove_concurrent_index :ci_stages, [:pipeline_id, :name]
end
def add_unique_index!
add_concurrent_index :ci_stages, [:pipeline_id, :name], unique: true
end
def remove_redundant_pipeline_stages!
2018-11-20 20:47:30 +05:30
disable_statement_timeout do
redundant_stages_ids = <<~SQL
SELECT id FROM ci_stages WHERE (pipeline_id, name) IN (
SELECT pipeline_id, name FROM ci_stages
GROUP BY pipeline_id, name HAVING COUNT(*) > 1
)
2018-03-17 18:26:18 +05:30
SQL
2018-11-20 20:47:30 +05:30
2018-03-17 18:26:18 +05:30
execute <<~SQL
2018-11-20 20:47:30 +05:30
UPDATE ci_builds SET stage_id = NULL WHERE stage_id IN (#{redundant_stages_ids})
2018-03-17 18:26:18 +05:30
SQL
2018-11-20 20:47:30 +05:30
2020-01-01 13:55:28 +05:30
execute <<~SQL
DELETE FROM ci_stages WHERE id IN (#{redundant_stages_ids})
SQL
2018-03-17 18:26:18 +05:30
end
end
end