2018-11-13 07:27:31 +00:00
|
|
|
class RemoveRedundantPipelineStages < ActiveRecord::Migration[4.2]
|
2018-01-19 13:25:30 +00:00
|
|
|
include Gitlab::Database::MigrationHelpers
|
|
|
|
|
|
|
|
DOWNTIME = false
|
|
|
|
|
2018-02-05 15:22:19 +00:00
|
|
|
disable_ddl_transaction!
|
2018-02-05 14:32:52 +00:00
|
|
|
|
2018-02-05 15:22:19 +00:00
|
|
|
def up(attempts: 100)
|
2018-02-05 14:32:52 +00:00
|
|
|
remove_redundant_pipeline_stages!
|
2018-02-07 08:18:11 +00:00
|
|
|
remove_outdated_index!
|
2018-02-05 15:22:19 +00:00
|
|
|
add_unique_index!
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
retry if (attempts -= 1) > 0
|
2018-02-06 13:16:12 +00:00
|
|
|
|
2018-02-06 08:58:50 +00:00
|
|
|
raise StandardError, <<~EOS
|
|
|
|
Failed to add an unique index to ci_stages, despite retrying the
|
2018-02-06 13:16:12 +00:00
|
|
|
migration 100 times.
|
|
|
|
|
2019-09-18 14:02:45 +00:00
|
|
|
See https://gitlab.com/gitlab-org/gitlab-foss/merge_requests/16580.
|
2018-02-06 08:58:50 +00:00
|
|
|
EOS
|
2018-02-05 14:32:52 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
remove_concurrent_index :ci_stages, [:pipeline_id, :name], unique: true
|
|
|
|
add_concurrent_index :ci_stages, [:pipeline_id, :name]
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-02-05 15:22:19 +00:00
|
|
|
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
|
|
|
|
|
2018-02-05 14:32:52 +00:00
|
|
|
def remove_redundant_pipeline_stages!
|
2018-08-10 23:45:46 +00:00
|
|
|
disable_statement_timeout do
|
2018-07-09 21:31:45 +00:00
|
|
|
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-01-24 14:21:20 +00:00
|
|
|
SQL
|
2018-07-09 21:31:45 +00:00
|
|
|
|
2018-01-24 14:21:20 +00:00
|
|
|
execute <<~SQL
|
2018-07-09 21:31:45 +00:00
|
|
|
UPDATE ci_builds SET stage_id = NULL WHERE stage_id IN (#{redundant_stages_ids})
|
2018-01-24 14:21:20 +00:00
|
|
|
SQL
|
2018-07-09 21:31:45 +00:00
|
|
|
|
2019-11-19 15:06:24 +00:00
|
|
|
execute <<~SQL
|
|
|
|
DELETE FROM ci_stages WHERE id IN (#{redundant_stages_ids})
|
|
|
|
SQL
|
2018-01-24 14:21:20 +00:00
|
|
|
end
|
2018-01-19 13:25:30 +00:00
|
|
|
end
|
|
|
|
end
|