debian-mirror-gitlab/db/post_migrate/20180604123514_cleanup_stages_position_migration.rb

46 lines
1.2 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
class CleanupStagesPositionMigration < ActiveRecord::Migration[4.2]
2018-11-08 19:23:39 +05:30
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
TMP_INDEX_NAME = 'tmp_id_stage_position_partial_null_index'.freeze
disable_ddl_transaction!
class Stages < ActiveRecord::Base
include EachBatch
self.table_name = 'ci_stages'
end
def up
2018-11-20 20:47:30 +05:30
disable_statement_timeout do
Gitlab::BackgroundMigration.steal('MigrateStageIndex')
2018-11-08 19:23:39 +05:30
2018-11-20 20:47:30 +05:30
unless index_exists_by_name?(:ci_stages, TMP_INDEX_NAME)
add_concurrent_index(:ci_stages, :id, where: 'position IS NULL', name: TMP_INDEX_NAME)
end
2018-11-08 19:23:39 +05:30
2018-11-20 20:47:30 +05:30
migratable = <<~SQL
position IS NULL AND EXISTS (
SELECT 1 FROM ci_builds WHERE stage_id = ci_stages.id AND stage_idx IS NOT NULL
)
SQL
2018-11-08 19:23:39 +05:30
2018-11-20 20:47:30 +05:30
Stages.where(migratable).each_batch(of: 1000) do |batch|
batch.pluck(:id).each do |stage|
Gitlab::BackgroundMigration::MigrateStageIndex.new.perform(stage, stage)
end
2018-11-08 19:23:39 +05:30
end
2018-11-20 20:47:30 +05:30
remove_concurrent_index_by_name(:ci_stages, TMP_INDEX_NAME)
end
2018-11-08 19:23:39 +05:30
end
def down
if index_exists_by_name?(:ci_stages, TMP_INDEX_NAME)
2018-11-20 20:47:30 +05:30
disable_statement_timeout do
remove_concurrent_index_by_name(:ci_stages, TMP_INDEX_NAME)
end
2018-11-08 19:23:39 +05:30
end
end
end