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

backfill_partitioned_table.rb « background_migration « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6479d40a93018b4cedf8d40048aadd38c8c959fd (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 BackgroundMigration
    # Background migration to generically copy data from the given table into its corresponding partitioned table
    class BackfillPartitionedTable < BatchedMigrationJob
      operation_name :upsert_partitioned_table
      feature_category :database
      job_arguments :partitioned_table

      def perform
        validate_paritition_table!

        bulk_copy = Gitlab::Database::PartitioningMigrationHelpers::BulkCopy.new(
          batch_table,
          partitioned_table,
          batch_column,
          connection: connection
        )

        each_sub_batch do |relation|
          sub_start_id, sub_stop_id = relation.pick(Arel.sql("MIN(#{batch_column}), MAX(#{batch_column})"))
          bulk_copy.copy_between(sub_start_id, sub_stop_id)
        end
      end

      private

      def validate_paritition_table!
        unless connection.table_exists?(partitioned_table)
          raise "exiting backfill migration because partitioned table #{partitioned_table} does not exist. " \
                "This could be due to rollback of the migration which created the partitioned table."
        end

        # rubocop: disable Style/GuardClause
        unless Gitlab::Database::PostgresPartitionedTable.find_by_name_in_current_schema(partitioned_table).present?
          raise "exiting backfill migration because the given destination table is not partitioned."
        end
        # rubocop: enable Style/GuardClause
      end
    end
  end
end