Commit 27eab8a4 authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'update-retried-migration' into 'master'

Add temporary partial index to speed up the migration

Closes #32469

See merge request !11534
parents bcba281f 1aa67247
...@@ -2,28 +2,42 @@ class UpateRetriedForCiBuild < ActiveRecord::Migration ...@@ -2,28 +2,42 @@ class UpateRetriedForCiBuild < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
DOWNTIME = false DOWNTIME = false
disable_ddl_transaction! disable_ddl_transaction!
def up def up
disable_statement_timeout disable_statement_timeout
latest_id = <<-SQL.strip_heredoc with_temporary_partial_index do
SELECT MAX(ci_builds2.id) latest_id = <<-SQL.strip_heredoc
FROM ci_builds ci_builds2 SELECT MAX(ci_builds2.id)
WHERE ci_builds.commit_id=ci_builds2.commit_id FROM ci_builds ci_builds2
AND ci_builds.name=ci_builds2.name WHERE ci_builds.commit_id=ci_builds2.commit_id
SQL AND ci_builds.name=ci_builds2.name
SQL
# This is slow update as it does single-row query
# This is designed to be run as idle, or a post deployment migration # This is slow update as it does single-row query
is_retried = Arel.sql("((#{latest_id}) != ci_builds.id)") # This is designed to be run as idle, or a post deployment migration
is_retried = Arel.sql("((#{latest_id}) != ci_builds.id)")
update_column_in_batches(:ci_builds, :retried, is_retried) do |table, query|
query.where(table[:retried].eq(nil)) update_column_in_batches(:ci_builds, :retried, is_retried) do |table, query|
query.where(table[:retried].eq(nil))
end
end end
end end
def down def down
end end
def with_temporary_partial_index
if Gitlab::Database.postgresql?
execute 'CREATE INDEX CONCURRENTLY IF NOT EXISTS index_for_ci_builds_retried_migration ON ci_builds (id) WHERE retried IS NULL;'
end
yield
if Gitlab::Database.postgresql?
execute 'DROP INDEX CONCURRENTLY IF EXISTS index_for_ci_builds_retried_migration'
end
end
end end
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