Commit 6fd95fae authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch 'ab/partitioning-pk' into 'master'

Change primary key definition to bigint

See merge request gitlab-org/gitlab!34372
parents aef157b4 e6660ba3
...@@ -109,9 +109,18 @@ module Gitlab ...@@ -109,9 +109,18 @@ module Gitlab
remove_column(table_name, partition_column.name) remove_column(table_name, partition_column.name)
rename_column(table_name, tmp_column_name, partition_column.name) rename_column(table_name, tmp_column_name, partition_column.name)
change_column_default(table_name, primary_key, nil) change_column_default(table_name, primary_key, nil)
if column_of_type?(table_name, primary_key, :integer)
# Default to int8 primary keys to prevent overflow
change_column(table_name, primary_key, :bigint)
end
end end
end end
def column_of_type?(table_name, column, type)
find_column_definition(table_name, column).type == type
end
def create_daterange_partitions(table_name, column_name, min_date, max_date) def create_daterange_partitions(table_name, column_name, min_date, max_date)
min_date = min_date.beginning_of_month.to_date min_date = min_date.beginning_of_month.to_date
max_date = max_date.next_month.beginning_of_month.to_date max_date = max_date.next_month.beginning_of_month.to_date
......
...@@ -111,6 +111,36 @@ describe Gitlab::Database::PartitioningMigrationHelpers::TableManagementHelpers ...@@ -111,6 +111,36 @@ describe Gitlab::Database::PartitioningMigrationHelpers::TableManagementHelpers
expect_table_partitioned_by(partitioned_table, [partition_column]) expect_table_partitioned_by(partitioned_table, [partition_column])
end end
it 'changes the primary key datatype to bigint' do
migration.partition_table_by_date template_table, partition_column, min_date: min_date, max_date: max_date
pk_column = connection.columns(partitioned_table).find { |c| c.name == old_primary_key }
expect(pk_column.sql_type).to eq('bigint')
end
context 'with a non-integer primary key datatype' do
before do
connection.create_table :another_example, id: false do |t|
t.string :identifier, primary_key: true
t.timestamp :created_at
end
end
let(:template_table) { :another_example }
let(:old_primary_key) { 'identifier' }
it 'does not change the primary key datatype' do
migration.partition_table_by_date template_table, partition_column, min_date: min_date, max_date: max_date
original_pk_column = connection.columns(template_table).find { |c| c.name == old_primary_key }
pk_column = connection.columns(partitioned_table).find { |c| c.name == old_primary_key }
expect(pk_column).not_to be_nil
expect(pk_column).to eq(original_pk_column)
end
end
it 'removes the default from the primary key column' do it 'removes the default from the primary key column' do
migration.partition_table_by_date template_table, partition_column, min_date: min_date, max_date: max_date migration.partition_table_by_date template_table, partition_column, min_date: min_date, max_date: max_date
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment