Commit 119c4917 authored by Gabriel Mazetto's avatar Gabriel Mazetto

Fix specs to use `disable_statement_timeout` correctly

parent 09e7c75d
......@@ -106,14 +106,14 @@ class ProjectForeignKeysWithCascadingDeletes < ActiveRecord::Migration
# Disables statement timeouts for the current connection. This is
# necessary as removing of orphaned data might otherwise exceed the
# statement timeout.
disable_statement_timeout
disable_statement_timeout(transaction: false) do
remove_orphans(*queue.pop) until queue.empty?
steal_from_queues(queues - [queue])
end
end
end
end
threads.each(&:join)
end
......
......@@ -25,8 +25,9 @@ class AddLowerPathIndexToRedirectRoutes < ActiveRecord::Migration
# trivial to write a query that checks for an index. BUT there is a
# convenient `IF EXISTS` parameter for `DROP INDEX`.
if supports_drop_index_concurrently?
disable_statement_timeout
disable_statement_timeout(transaction: false) do
execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME};"
end
else
execute "DROP INDEX IF EXISTS #{INDEX_NAME};"
end
......
......@@ -8,8 +8,7 @@ class AddIndexOnNamespacesLowerName < ActiveRecord::Migration
def up
return unless Gitlab::Database.postgresql?
disable_statement_timeout
disable_statement_timeout(transaction: false) do
if Gitlab::Database.version.to_f >= 9.5
# Allow us to hot-patch the index manually ahead of the migration
execute "CREATE INDEX CONCURRENTLY IF NOT EXISTS #{INDEX_NAME} ON namespaces (lower(name));"
......@@ -17,16 +16,17 @@ class AddIndexOnNamespacesLowerName < ActiveRecord::Migration
execute "CREATE INDEX CONCURRENTLY #{INDEX_NAME} ON namespaces (lower(name));"
end
end
end
def down
return unless Gitlab::Database.postgresql?
disable_statement_timeout
disable_statement_timeout(transaction: false) do
if Gitlab::Database.version.to_f >= 9.2
execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME};"
else
execute "DROP INDEX IF EXISTS #{INDEX_NAME};"
end
end
end
end
......@@ -18,8 +18,7 @@ class ReworkRedirectRoutesIndexes < ActiveRecord::Migration
OLD_INDEX_NAME_PATH_LOWER = "index_on_redirect_routes_lower_path"
def up
disable_statement_timeout
disable_statement_timeout(transaction: false) do
# this is a plain btree on a single boolean column. It'll never be
# selective enough to be valuable. This class is called by
# setup_postgresql.rake so it needs to be able to handle this
......@@ -50,10 +49,10 @@ class ReworkRedirectRoutesIndexes < ActiveRecord::Migration
# column so the varchar_pattern_ops index is sufficient
execute "DROP INDEX CONCURRENTLY IF EXISTS #{OLD_INDEX_NAME_PATH_LOWER};"
end
end
def down
disable_statement_timeout
disable_statement_timeout(transaction: false) do
add_concurrent_index(:redirect_routes, :permanent)
return unless Gitlab::Database.postgresql?
......@@ -65,4 +64,5 @@ class ReworkRedirectRoutesIndexes < ActiveRecord::Migration
execute("DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME_PERM};")
execute("DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME_TEMP};")
end
end
end
......@@ -13,8 +13,7 @@ class CreateProjectCiCdSettings < ActiveRecord::Migration
end
end
disable_statement_timeout
disable_statement_timeout(transaction: false) do
# This particular INSERT will take between 10 and 20 seconds.
execute 'INSERT INTO project_ci_cd_settings (project_id) SELECT id FROM projects'
......@@ -24,6 +23,7 @@ class CreateProjectCiCdSettings < ActiveRecord::Migration
add_foreign_key_with_retry
end
end
def down
drop_table :project_ci_cd_settings
......
......@@ -14,8 +14,7 @@ class CleanupBuildStageMigration < ActiveRecord::Migration
end
def up
disable_statement_timeout
disable_statement_timeout(transaction: false) do
##
# We steal from the background migrations queue to catch up with the
# scheduled migrations set.
......@@ -52,10 +51,13 @@ class CleanupBuildStageMigration < ActiveRecord::Migration
#
remove_concurrent_index_by_name(:ci_builds, TMP_INDEX)
end
end
def down
if index_exists_by_name?(:ci_builds, TMP_INDEX)
disable_statement_timeout(transaction: false) do
remove_concurrent_index_by_name(:ci_builds, TMP_INDEX)
end
end
end
end
......@@ -13,20 +13,20 @@ class ProjectNameLowerIndex < ActiveRecord::Migration
def up
return unless Gitlab::Database.postgresql?
disable_statement_timeout
disable_statement_timeout(transaction: false) do
execute "CREATE INDEX CONCURRENTLY #{INDEX_NAME} ON projects (LOWER(name))"
end
end
def down
return unless Gitlab::Database.postgresql?
disable_statement_timeout
disable_statement_timeout(transaction: false) do
if supports_drop_index_concurrently?
execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME}"
else
execute "DROP INDEX IF EXISTS #{INDEX_NAME}"
end
end
end
end
......@@ -29,20 +29,22 @@ class CompositePrimaryKeysMigration < ActiveRecord::Migration
def up
return unless Gitlab::Database.postgresql?
disable_statement_timeout
disable_statement_timeout(transaction: false) do
TABLES.each do |index|
add_primary_key(index)
end
end
end
def down
return unless Gitlab::Database.postgresql?
disable_statement_timeout
disable_statement_timeout(transaction: false) do
TABLES.each do |index|
remove_primary_key(index)
end
end
end
private
......
......@@ -8,10 +8,10 @@ class EnableAutoCancelPendingPipelinesForAll < ActiveRecord::Migration
DOWNTIME = false
def up
disable_statement_timeout
disable_statement_timeout(transaction: false) do
update_column_in_batches(:projects, :auto_cancel_pending_pipelines, 1)
end
end
def down
# Nothing we can do!
......
......@@ -7,14 +7,14 @@ class UpdateRetriedForCiBuild < ActiveRecord::Migration
disable_ddl_transaction!
def up
disable_statement_timeout
if Gitlab::Database.mysql?
up_mysql
else
disable_statement_timeout(transaction: false) do
up_postgres
end
end
end
def down
end
......
......@@ -7,11 +7,10 @@ class AddHeadPipelineForEachMergeRequest < ActiveRecord::Migration
disable_ddl_transaction!
def up
disable_statement_timeout
pipelines = Arel::Table.new(:ci_pipelines)
merge_requests = Arel::Table.new(:merge_requests)
disable_statement_timeout(transaction: false) do
head_id = pipelines
.project(Arel::Nodes::NamedFunction.new('max', [pipelines[:id]]))
.from(pipelines)
......@@ -22,6 +21,7 @@ class AddHeadPipelineForEachMergeRequest < ActiveRecord::Migration
update_column_in_batches(:merge_requests, :head_pipeline_id, sub_query)
end
end
def down
end
......
......@@ -87,16 +87,16 @@ class RenameAllReservedPathsAgain < ActiveRecord::Migration
].freeze
def up
disable_statement_timeout
disable_statement_timeout(transaction: false) do
TOP_LEVEL_ROUTES.each { |route| rename_root_paths(route) }
PROJECT_WILDCARD_ROUTES.each { |route| rename_wildcard_paths(route) }
GROUP_ROUTES.each { |route| rename_child_paths(route) }
end
end
def down
disable_statement_timeout
disable_statement_timeout(transaction: false) do
revert_renames
end
end
end
......@@ -6,8 +6,7 @@ class MigratePipelineStages < ActiveRecord::Migration
disable_ddl_transaction!
def up
disable_statement_timeout
disable_statement_timeout(transaction: false) do
execute <<-SQL.strip_heredoc
INSERT INTO ci_stages (project_id, pipeline_id, name)
SELECT project_id, commit_id, stage FROM ci_builds
......@@ -19,4 +18,5 @@ class MigratePipelineStages < ActiveRecord::Migration
ORDER BY MAX(stage_idx)
SQL
end
end
end
......@@ -7,22 +7,22 @@ class MigrateBuildStageReferenceAgain < ActiveRecord::Migration
disable_ddl_transaction!
def up
disable_statement_timeout
stage_id = Arel.sql <<-SQL.strip_heredoc
(SELECT id FROM ci_stages
WHERE ci_stages.pipeline_id = ci_builds.commit_id
AND ci_stages.name = ci_builds.stage)
SQL
disable_statement_timeout(transaction: false) do
update_column_in_batches(:ci_builds, :stage_id, stage_id) do |table, query|
query.where(table[:stage_id].eq(nil))
end
end
end
def down
disable_statement_timeout
disable_statement_timeout(transaction: false) do
update_column_in_batches(:ci_builds, :stage_id, nil)
end
end
end
......@@ -26,9 +26,10 @@ class MigrateStagesStatuses < ActiveRecord::Migration
end
def down
disable_statement_timeout
disable_statement_timeout(transaction: false) do
# rubocop:disable Migration/UpdateLargeTable
update_column_in_batches(:ci_stages, :status, nil)
end
end
end
......@@ -78,13 +78,13 @@ class RemoveSoftRemovedObjects < ActiveRecord::Migration
MODELS = [Issue, MergeRequest, CiPipelineSchedule, CiTrigger].freeze
def up
disable_statement_timeout
disable_statement_timeout(transaction: false) do
remove_personal_routes
remove_personal_namespaces
remove_group_namespaces
remove_simple_soft_removed_rows
end
end
def down
# The data removed by this migration can't be restored in an automated way.
......
......@@ -38,8 +38,7 @@ class RemoveRedundantPipelineStages < ActiveRecord::Migration
end
def remove_redundant_pipeline_stages!
disable_statement_timeout
disable_statement_timeout(transaction: false) do
redundant_stages_ids = <<~SQL
SELECT id FROM ci_stages WHERE (pipeline_id, name) IN (
SELECT pipeline_id, name FROM ci_stages
......@@ -63,4 +62,5 @@ class RemoveRedundantPipelineStages < ActiveRecord::Migration
SQL
end
end
end
end
......@@ -15,11 +15,11 @@ class RemovePermanentFromRedirectRoutes < ActiveRecord::Migration
# ReworkRedirectRoutesIndexes:
# https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/16211
if Gitlab::Database.postgresql?
disable_statement_timeout
disable_statement_timeout(transaction: false) do
execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME_PERM};"
execute "DROP INDEX CONCURRENTLY IF EXISTS #{INDEX_NAME_TEMP};"
end
end
remove_column(:redirect_routes, :permanent)
end
......@@ -28,10 +28,10 @@ class RemovePermanentFromRedirectRoutes < ActiveRecord::Migration
add_column(:redirect_routes, :permanent, :boolean)
if Gitlab::Database.postgresql?
disable_statement_timeout
disable_statement_timeout(transaction: false) do
execute("CREATE INDEX CONCURRENTLY #{INDEX_NAME_PERM} ON redirect_routes (lower(path) varchar_pattern_ops) where (permanent);")
execute("CREATE INDEX CONCURRENTLY #{INDEX_NAME_TEMP} ON redirect_routes (lower(path) varchar_pattern_ops) where (not permanent or permanent is null) ;")
end
end
end
end
......@@ -20,12 +20,12 @@ class AddPathIndexToRedirectRoutes < ActiveRecord::Migration
def up
return unless Gitlab::Database.postgresql?
disable_statement_timeout
disable_statement_timeout(transaction: false) do
unless index_exists_by_name?(:redirect_routes, INDEX_NAME)
execute("CREATE UNIQUE INDEX CONCURRENTLY #{INDEX_NAME} ON redirect_routes (lower(path) varchar_pattern_ops);")
end
end
end
def down
# Do nothing in the DOWN. Since the index above is originally created in the
......
......@@ -17,8 +17,7 @@ class RescheduleBuildsStagesMigration < ActiveRecord::Migration
end
def up
disable_statement_timeout
disable_statement_timeout(transaction: false) do
Build.where('stage_id IS NULL').tap do |relation|
queue_background_migration_jobs_by_range_at_intervals(relation,
MIGRATION,
......@@ -26,6 +25,7 @@ class RescheduleBuildsStagesMigration < ActiveRecord::Migration
batch_size: BATCH_SIZE)
end
end
end
def down
# noop
......
......@@ -13,8 +13,7 @@ class ScheduleStagesIndexMigration < ActiveRecord::Migration
end
def up
disable_statement_timeout
disable_statement_timeout(transaction: false) do
Stage.all.tap do |relation|
queue_background_migration_jobs_by_range_at_intervals(relation,
MIGRATION,
......@@ -22,6 +21,7 @@ class ScheduleStagesIndexMigration < ActiveRecord::Migration
batch_size: BATCH_SIZE)
end
end
end
def down
# noop
......
......@@ -12,8 +12,7 @@ class CleanupStagesPositionMigration < ActiveRecord::Migration
end
def up
disable_statement_timeout
disable_statement_timeout(transaction: false) do
Gitlab::BackgroundMigration.steal('MigrateStageIndex')
unless index_exists_by_name?(:ci_stages, TMP_INDEX_NAME)
......@@ -34,10 +33,13 @@ class CleanupStagesPositionMigration < ActiveRecord::Migration
remove_concurrent_index_by_name(:ci_stages, TMP_INDEX_NAME)
end
end
def down
if index_exists_by_name?(:ci_stages, TMP_INDEX_NAME)
disable_statement_timeout(transaction: false) do
remove_concurrent_index_by_name(:ci_stages, TMP_INDEX_NAME)
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