debian-mirror-gitlab/spec/lib/gitlab/database/partitioning/monthly_strategy_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

277 lines
12 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Gitlab::Database::Partitioning::MonthlyStrategy do
2021-11-11 11:23:49 +05:30
let(:connection) { ActiveRecord::Base.connection }
2020-07-28 23:09:34 +05:30
describe '#current_partitions' do
subject { described_class.new(model, partitioning_key).current_partitions }
let(:model) { double('model', table_name: table_name) }
let(:partitioning_key) { double }
2021-12-11 22:18:48 +05:30
let(:table_name) { :_test_partitioned_test }
2020-07-28 23:09:34 +05:30
before do
2021-11-11 11:23:49 +05:30
connection.execute(<<~SQL)
2020-07-28 23:09:34 +05:30
CREATE TABLE #{table_name}
(id serial not null, created_at timestamptz not null, PRIMARY KEY (id, created_at))
PARTITION BY RANGE (created_at);
2021-12-11 22:18:48 +05:30
CREATE TABLE #{Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA}._test_partitioned_test_000000
2020-07-28 23:09:34 +05:30
PARTITION OF #{table_name}
FOR VALUES FROM (MINVALUE) TO ('2020-05-01');
2021-12-11 22:18:48 +05:30
CREATE TABLE #{Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA}._test_partitioned_test_202005
2020-07-28 23:09:34 +05:30
PARTITION OF #{table_name}
FOR VALUES FROM ('2020-05-01') TO ('2020-06-01');
SQL
end
it 'detects both partitions' do
2022-11-25 23:54:43 +05:30
expect(subject).to eq(
[
Gitlab::Database::Partitioning::TimePartition.new(table_name, nil, '2020-05-01', partition_name: '_test_partitioned_test_000000'),
Gitlab::Database::Partitioning::TimePartition.new(table_name, '2020-05-01', '2020-06-01', partition_name: '_test_partitioned_test_202005')
])
2020-07-28 23:09:34 +05:30
end
end
describe '#missing_partitions' do
subject { described_class.new(model, partitioning_key).missing_partitions }
let(:model) do
Class.new(ActiveRecord::Base) do
2021-12-11 22:18:48 +05:30
self.table_name = '_test_partitioned_test'
2020-07-28 23:09:34 +05:30
self.primary_key = :id
end
end
let(:partitioning_key) { :created_at }
around do |example|
2021-01-03 14:25:43 +05:30
travel_to(Date.parse('2020-08-22')) { example.run }
2020-07-28 23:09:34 +05:30
end
context 'with existing partitions' do
before do
2021-11-11 11:23:49 +05:30
connection.execute(<<~SQL)
2020-07-28 23:09:34 +05:30
CREATE TABLE #{model.table_name}
(id serial not null, created_at timestamptz not null, PRIMARY KEY (id, created_at))
PARTITION BY RANGE (created_at);
2021-12-11 22:18:48 +05:30
CREATE TABLE #{Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA}._test_partitioned_test_000000
2020-07-28 23:09:34 +05:30
PARTITION OF #{model.table_name}
FOR VALUES FROM (MINVALUE) TO ('2020-05-01');
2021-12-11 22:18:48 +05:30
CREATE TABLE #{Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA}._test_partitioned_test_202006
2020-07-28 23:09:34 +05:30
PARTITION OF #{model.table_name}
FOR VALUES FROM ('2020-06-01') TO ('2020-07-01');
SQL
# Insert some data, it doesn't make a difference
model.create!(created_at: Date.parse('2020-04-20'))
model.create!(created_at: Date.parse('2020-06-15'))
end
2021-09-30 23:02:18 +05:30
context 'when pruning partitions before June 2020' do
subject { described_class.new(model, partitioning_key, retain_for: 1.month).missing_partitions }
it 'does not include the missing partition from May 2020 because it would be dropped' do
expect(subject).not_to include(Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-05-01', '2020-06-01'))
end
it 'detects the missing partition for 1 month ago (July 2020)' do
expect(subject).to include(Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-07-01', '2020-08-01'))
end
end
2020-07-28 23:09:34 +05:30
it 'detects the gap and the missing partition in May 2020' do
expect(subject).to include(Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-05-01', '2020-06-01'))
end
it 'detects the missing partitions at the end of the range and expects a partition for July 2020' do
expect(subject).to include(Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-07-01', '2020-08-01'))
end
it 'detects the missing partitions at the end of the range and expects a partition for August 2020' do
expect(subject).to include(Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-08-01', '2020-09-01'))
end
it 'creates partitions 6 months out from now (Sep 2020 through Feb 2021)' do
expect(subject).to include(
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-09-01', '2020-10-01'),
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-10-01', '2020-11-01'),
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-11-01', '2020-12-01'),
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-12-01', '2021-01-01'),
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2021-01-01', '2021-02-01'),
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2021-02-01', '2021-03-01')
)
end
it 'detects all missing partitions' do
expect(subject.size).to eq(9)
end
end
context 'without existing partitions' do
before do
2021-11-11 11:23:49 +05:30
connection.execute(<<~SQL)
2020-07-28 23:09:34 +05:30
CREATE TABLE #{model.table_name}
(id serial not null, created_at timestamptz not null, PRIMARY KEY (id, created_at))
PARTITION BY RANGE (created_at);
SQL
end
2021-09-30 23:02:18 +05:30
context 'when pruning partitions before June 2020' do
subject { described_class.new(model, partitioning_key, retain_for: 1.month).missing_partitions }
it 'detects exactly the set of partitions from June 2020 to March 2021' do
months = %w[2020-07-01 2020-08-01 2020-09-01 2020-10-01 2020-11-01 2020-12-01 2021-01-01 2021-02-01 2021-03-01]
expected = months[..-2].zip(months.drop(1)).map do |(from, to)|
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, from, to)
end
expect(subject).to match_array(expected)
end
end
2020-07-28 23:09:34 +05:30
it 'detects the missing catch-all partition at the beginning' do
expect(subject).to include(Gitlab::Database::Partitioning::TimePartition.new(model.table_name, nil, '2020-08-01'))
end
it 'detects the missing partition for today and expects a partition for August 2020' do
expect(subject).to include(Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-08-01', '2020-09-01'))
end
it 'creates partitions 6 months out from now (Sep 2020 through Feb 2021' do
expect(subject).to include(
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-09-01', '2020-10-01'),
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-10-01', '2020-11-01'),
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-11-01', '2020-12-01'),
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-12-01', '2021-01-01'),
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2021-01-01', '2021-02-01'),
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2021-02-01', '2021-03-01')
)
end
it 'detects all missing partitions' do
expect(subject.size).to eq(8)
end
end
context 'with a regular partition but no catchall (MINVALUE, to) partition' do
before do
2021-11-11 11:23:49 +05:30
connection.execute(<<~SQL)
2020-07-28 23:09:34 +05:30
CREATE TABLE #{model.table_name}
(id serial not null, created_at timestamptz not null, PRIMARY KEY (id, created_at))
PARTITION BY RANGE (created_at);
2021-12-11 22:18:48 +05:30
CREATE TABLE #{Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA}._test_partitioned_test_202006
2020-07-28 23:09:34 +05:30
PARTITION OF #{model.table_name}
FOR VALUES FROM ('2020-06-01') TO ('2020-07-01');
SQL
end
it 'detects a missing catch-all partition to add before the existing partition' do
expect(subject).to include(Gitlab::Database::Partitioning::TimePartition.new(model.table_name, nil, '2020-06-01'))
end
end
end
2021-09-30 23:02:18 +05:30
describe '#extra_partitions' do
let(:model) do
Class.new(ActiveRecord::Base) do
2021-12-11 22:18:48 +05:30
self.table_name = '_test_partitioned_test'
2021-09-30 23:02:18 +05:30
self.primary_key = :id
end
end
let(:partitioning_key) { :created_at }
2021-12-11 22:18:48 +05:30
let(:table_name) { :_test_partitioned_test }
2021-09-30 23:02:18 +05:30
around do |example|
travel_to(Date.parse('2020-08-22')) { example.run }
end
describe 'with existing partitions' do
before do
ActiveRecord::Base.connection.execute(<<~SQL)
CREATE TABLE #{table_name}
(id serial not null, created_at timestamptz not null, PRIMARY KEY (id, created_at))
PARTITION BY RANGE (created_at);
2021-12-11 22:18:48 +05:30
CREATE TABLE #{Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA}._test_partitioned_test_000000
2021-09-30 23:02:18 +05:30
PARTITION OF #{table_name}
FOR VALUES FROM (MINVALUE) TO ('2020-05-01');
2021-12-11 22:18:48 +05:30
CREATE TABLE #{Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA}._test_partitioned_test_202005
2021-09-30 23:02:18 +05:30
PARTITION OF #{table_name}
FOR VALUES FROM ('2020-05-01') TO ('2020-06-01');
2021-12-11 22:18:48 +05:30
CREATE TABLE #{Gitlab::Database::DYNAMIC_PARTITIONS_SCHEMA}._test_partitioned_test_202006
2021-09-30 23:02:18 +05:30
PARTITION OF #{table_name}
FOR VALUES FROM ('2020-06-01') TO ('2020-07-01')
SQL
end
context 'without a time retention policy' do
subject { described_class.new(model, partitioning_key).extra_partitions }
it 'has no extra partitions to prune' do
expect(subject).to eq([])
end
end
context 'with a time retention policy that excludes no partitions' do
subject { described_class.new(model, partitioning_key, retain_for: 4.months).extra_partitions }
it 'has no extra partitions to prune' do
expect(subject).to eq([])
end
end
context 'with a time retention policy of 3 months' do
subject { described_class.new(model, partitioning_key, retain_for: 3.months).extra_partitions }
it 'prunes the unbounded partition ending 2020-05-01' do
min_value_to_may = Gitlab::Database::Partitioning::TimePartition.new(model.table_name, nil, '2020-05-01',
2021-12-11 22:18:48 +05:30
partition_name: '_test_partitioned_test_000000')
2021-09-30 23:02:18 +05:30
expect(subject).to contain_exactly(min_value_to_may)
end
end
context 'with a time retention policy of 2 months' do
subject { described_class.new(model, partitioning_key, retain_for: 2.months).extra_partitions }
it 'prunes the unbounded partition and the partition for May-June' do
expect(subject).to contain_exactly(
2021-12-11 22:18:48 +05:30
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, nil, '2020-05-01', partition_name: '_test_partitioned_test_000000'),
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-05-01', '2020-06-01', partition_name: '_test_partitioned_test_202005')
2021-09-30 23:02:18 +05:30
)
end
2021-11-11 11:23:49 +05:30
context 'when the retain_non_empty_partitions is true' do
subject { described_class.new(model, partitioning_key, retain_for: 2.months, retain_non_empty_partitions: true).extra_partitions }
it 'prunes empty partitions' do
expect(subject).to contain_exactly(
2021-12-11 22:18:48 +05:30
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, nil, '2020-05-01', partition_name: '_test_partitioned_test_000000'),
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, '2020-05-01', '2020-06-01', partition_name: '_test_partitioned_test_202005')
2021-11-11 11:23:49 +05:30
)
end
it 'does not prune non-empty partitions' do
2021-12-11 22:18:48 +05:30
connection.execute("INSERT INTO #{table_name} (created_at) VALUES (('2020-05-15'))") # inserting one record into _test_partitioned_test_202005
2021-11-11 11:23:49 +05:30
expect(subject).to contain_exactly(
2021-12-11 22:18:48 +05:30
Gitlab::Database::Partitioning::TimePartition.new(model.table_name, nil, '2020-05-01', partition_name: '_test_partitioned_test_000000')
2021-11-11 11:23:49 +05:30
)
end
end
2021-09-30 23:02:18 +05:30
end
end
end
2020-07-28 23:09:34 +05:30
end