Welcome to mirror list, hosted at ThFree Co, Russian Federation.

lock_retry_mixin.rb « migrations « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fff0f35e33cdcbcac58af98a18a7bcee92f9c32d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# frozen_string_literal: true

module Gitlab
  module Database
    module Migrations
      module LockRetryMixin
        module ActiveRecordMigrationProxyLockRetries
          def migration_class
            migration.class
          end

          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(
                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