lib/gitlab/database/partitioning_migration_helpers/table_management_helpers.rb
# Creates a partitioned copy of an existing table, using a RANGE partitioning strategy on a timestamp column.
# One partition is created per month between the given `min_date` and `max_date`.
#
# A copy of the original table is required as PG currently does not support partitioning existing tables.
#
# Example:
#
#   partition_table_by_date :audit_events, :created_at, min_date: Date.new(2020, 1), max_date: Date.new(2020, 6)
#
# Options are:
#   :min_date - a date specifying the lower bounds of the partition range
#   :max_date - a date specifying the upper bounds of the partitioning range, defaults to today + 1 month
#
# Unless min_date is specified explicitly, we default to
# 1. The minimum value for the partitioning column in the table
# 2. If no data is present yet, the current month
Creates a partitioned copy of an existing table, using a RANGE partitioning strategy on a timestamp column.
One partition is created per month between the given `min_date` and `max_date`.

A copy of the original table is required as PG currently does not support partitioning existing tables.





Options are:
:min_date - a date specifying the lower bounds of the partition range
:max_date - a date specifying the upper bounds of the partitioning range, defaults to today + 1 month

Unless min_date is specified explicitly, we default to
1. The minimum value for the partitioning column in the table
2. If no data is present yet, the current month
Creates a partitioned copy of an existing table using a RANGE partitioning strategy on a timestamp column . One partition is created per month between the given ` min_date ` and ` max_date ` . A copy of the original table is required as PG currently does not support partitioning existing tables . Options are : : min_date - a date specifying the lower bounds of the partition range : max_date - a date specifying the upper bounds of the partitioning range defaults to today + 1 month Unless min_date is specified explicitly we default to 1 . The minimum value for the partitioning column in the table 2 . If no data is present yet the current month
def partition_table_by_date(table_name, column_name, min_date: nil, max_date: nil)
          assert_table_is_allowed(table_name)
          assert_not_in_transaction_block(scope: ERROR_SCOPE)
          max_date ||= Date.today + 1.month
          min_date ||= connection.select_one(<<~SQL)['minimum'] || max_date - 1.month
            SELECT date_trunc('MONTH', MIN(#{column_name})) AS minimum
            FROM #{table_name}
          SQL
          raise "max_date #{max_date} must be greater than min_date #{min_date}" if min_date >= max_date
          primary_key = connection.primary_key(table_name)
          raise "primary key not defined for #{table_name}" if primary_key.nil?
          partition_column = find_column_definition(table_name, column_name)
          raise "partition column #{column_name} does not exist on #{table_name}" if partition_column.nil?
          partitioned_table_name = make_partitioned_table_name(table_name)
          create_range_partitioned_copy(table_name, partitioned_table_name, partition_column, primary_key)
          create_daterange_partitions(partitioned_table_name, partition_column.name, min_date, max_date)
          create_trigger_to_sync_tables(table_name, partitioned_table_name, primary_key)
          enqueue_background_migration(table_name, partitioned_table_name, primary_key)
        end
def partition_table_by_date ( table_name , column_name , min_date : nil , max_date : nil ) assert_table_is_allowed ( table_name ) assert_not_in_transaction_block ( scope : ERROR_SCOPE ) max_date ||= Date . today + 1 . month min_date ||= connection . select_one ( <<~SQL ) [ 'minimum' ] || max_date - 1 . month 
            SELECT date_trunc('MONTH', MIN( #{ column_name } )) AS minimum
            FROM  #{ table_name } SQL raise "max_date #{max_date} must be greater than min_date #{min_date}" if min_date >= max_date primary_key = connection . primary_key ( table_name ) raise "primary key not defined for #{table_name}" if primary_key . nil? partition_column = find_column_definition ( table_name , column_name ) raise "partition column #{column_name} does not exist on #{table_name}" if partition_column . nil? partitioned_table_name = make_partitioned_table_name ( table_name ) create_range_partitioned_copy ( table_name , partitioned_table_name , partition_column , primary_key ) create_daterange_partitions ( partitioned_table_name , partition_column . name , min_date , max_date ) create_trigger_to_sync_tables ( table_name , partitioned_table_name , primary_key ) enqueue_background_migration ( table_name , partitioned_table_name , primary_key ) end
Creates a partitioned copy of an existing table, using a RANGE partitioning strategy on a timestamp column.
Creates a partitioned copy of an existing table using a RANGE partitioning strategy on a timestamp column .
table_name
<not_specific>
column_name
<not_specific>
min_date
<not_specific>
max_date
<not_specific>