debian-mirror-gitlab/lib/gitlab/database/migrations/lock_retry_mixin.rb

49 lines
1.5 KiB
Ruby
Raw Normal View History

2021-11-11 11:23:49 +05:30
# frozen_string_literal: true
module Gitlab
module Database
module Migrations
module LockRetryMixin
module ActiveRecordMigrationProxyLockRetries
def migration_class
migration.class
end
2022-04-04 11:22:00 +05:30
def migration_connection
migration.connection
end
2021-11-11 11:23:49 +05:30
def enable_lock_retries?
# regular AR migrations don't have this,
# only ones inheriting from Gitlab::Database::Migration have
return false unless migration.respond_to?(:enable_lock_retries?)
migration.enable_lock_retries?
end
end
module ActiveRecordMigratorLockRetries
# We patch the original method to start a transaction
# using the WithLockRetries methodology for the whole migration.
def ddl_transaction(migration, &block)
if use_transaction?(migration) && migration.enable_lock_retries?
Gitlab::Database::WithLockRetries.new(
2022-04-04 11:22:00 +05:30
connection: migration.migration_connection,
2021-11-11 11:23:49 +05:30
klass: migration.migration_class,
logger: Gitlab::BackgroundMigration::Logger
).run(raise_on_exhaustion: false, &block)
else
super
end
end
end
def self.patch!
ActiveRecord::MigrationProxy.prepend(ActiveRecordMigrationProxyLockRetries)
ActiveRecord::Migrator.prepend(ActiveRecordMigratorLockRetries)
end
end
end
end
end