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

cleanup_concurrent_schema_change.rb « background_migration « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 91b50c1a493b05db52fd5b115ae92326eaace130 (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
55
56
# frozen_string_literal: true

module Gitlab
  module BackgroundMigration
    # Base class for background migration for rename/type changes.
    class CleanupConcurrentSchemaChange
      include Database::MigrationHelpers

      # 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?(table, new_column) && column_exists?(table, old_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,
            self.class.name,
            [table, old_column, new_column]
          )
        else
          cleanup_concurrent_schema_change(table, old_column, new_column)
        end
      end

      def cleanup_concurrent_schema_change(_table, _old_column, _new_column)
        raise NotImplementedError
      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