summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/cleanup_concurrent_type_change.rb
blob: de622f657b22b6b7cf61c61e610eb328d9b6dd9b (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
44
45
46
47
48
49
50
51
52
53
54
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # Background migration for cleaning up a concurrent column rename.
    class CleanupConcurrentTypeChange
      include Database::MigrationHelpers

      RESCHEDULE_DELAY = 10.minutes

      # table - The name of the table the migration is performed for.
      # old_column - The name of the old (to drop) column.
      # new_column - The name of the new column.
      def perform(table, old_column, new_column)
        return unless column_exists?(:issues, new_column)

        rows_to_migrate = define_model_for(table)
          .where(new_column => nil)
          .where
          .not(old_column => nil)

        if rows_to_migrate.any?
          BackgroundMigrationWorker.perform_in(
            RESCHEDULE_DELAY,
            'CleanupConcurrentTypeChange',
            [table, old_column, new_column]
          )
        else
          cleanup_concurrent_column_type_change(table, old_column)
        end
      end

      # These methods are necessary so we can re-use the migration helpers in
      # this class.
      def connection
        ActiveRecord::Base.connection
      end

      def method_missing(name, *args, &block)
        connection.__send__(name, *args, &block) # rubocop: disable GitlabSecurity/PublicSend
      end

      def respond_to_missing?(*args)
        connection.respond_to?(*args) || super
      end

      def define_model_for(table)
        Class.new(ActiveRecord::Base) do
          self.table_name = table
        end
      end
    end
  end
end