Commit 41d9ac9e authored by Andreas Brandl's avatar Andreas Brandl

Merge branch '343955-improve-database-partitioning-tests' into 'master'

Improve Gitlab::Database::Partitioning tests

See merge request gitlab-org/gitlab!74036
parents e5a6cc3a 01fca948
...@@ -20,7 +20,11 @@ module Gitlab ...@@ -20,7 +20,11 @@ module Gitlab
class << self class << self
def register_models(models) def register_models(models)
registered_models.merge(models) models.each do |model|
raise "#{model} should have partitioning strategy defined" unless model.respond_to?(:partitioning_strategy)
registered_models << model
end
end end
def register_tables(tables) def register_tables(tables)
...@@ -55,8 +59,6 @@ module Gitlab ...@@ -55,8 +59,6 @@ module Gitlab
Gitlab::AppLogger.info(message: 'Finished dropping detached postgres partitions') Gitlab::AppLogger.info(message: 'Finished dropping detached postgres partitions')
end end
private
def registered_models def registered_models
@registered_models ||= Set.new @registered_models ||= Set.new
end end
...@@ -65,6 +67,8 @@ module Gitlab ...@@ -65,6 +67,8 @@ module Gitlab
@registered_tables ||= Set.new @registered_tables ||= Set.new
end end
private
def registered_for_sync def registered_for_sync
registered_models + registered_tables.map do |table| registered_models + registered_tables.map do |table|
TableWithoutModel.new(**table) TableWithoutModel.new(**table)
......
...@@ -8,6 +8,28 @@ RSpec.describe Gitlab::Database::Partitioning do ...@@ -8,6 +8,28 @@ RSpec.describe Gitlab::Database::Partitioning do
let(:connection) { ApplicationRecord.connection } let(:connection) { ApplicationRecord.connection }
around do |example|
previously_registered_models = described_class.registered_models.dup
described_class.instance_variable_set('@registered_models', Set.new)
previously_registered_tables = described_class.registered_tables.dup
described_class.instance_variable_set('@registered_tables', Set.new)
example.run
described_class.instance_variable_set('@registered_models', previously_registered_models)
described_class.instance_variable_set('@registered_tables', previously_registered_tables)
end
describe '.register_models' do
context 'ensure that the registered models have partitioning strategy' do
it 'fails when partitioning_strategy is not specified for the model' do
model = Class.new(ApplicationRecord)
expect { described_class.register_models([model]) }.to raise_error /should have partitioning strategy defined/
end
end
end
describe '.sync_partitions' do describe '.sync_partitions' do
let(:table_names) { %w[partitioning_test1 partitioning_test2] } let(:table_names) { %w[partitioning_test1 partitioning_test2] }
let(:models) do let(:models) do
...@@ -40,24 +62,18 @@ RSpec.describe Gitlab::Database::Partitioning do ...@@ -40,24 +62,18 @@ RSpec.describe Gitlab::Database::Partitioning do
end end
context 'when no partitioned models are given' do context 'when no partitioned models are given' do
let(:partition_manager_class) { described_class::PartitionManager }
let(:partition_manager) { double('partition manager') }
let(:model) { double('model') }
it 'manages partitions for each registered model' do it 'manages partitions for each registered model' do
registered_for_sync = described_class.__send__(:registered_for_sync) described_class.register_models([models.first])
described_class.register_tables([
allow(described_class).to receive(:registered_for_sync) {
.and_return(registered_for_sync) table_name: table_names.last,
partitioned_column: :created_at, strategy: :monthly
expect(Gitlab::Database::EachDatabase).to receive(:each_model_connection) }
.with(registered_for_sync) ])
.and_yield(model)
expect { described_class.sync_partitions }
expect(partition_manager_class).to receive(:new).with(model).and_return(partition_manager) .to change { find_partitions(table_names.first).size }.from(0)
expect(partition_manager).to receive(:sync_partitions) .and change { find_partitions(table_names.last).size }.from(0)
described_class.sync_partitions
end end
end end
end end
...@@ -125,10 +141,4 @@ RSpec.describe Gitlab::Database::Partitioning do ...@@ -125,10 +141,4 @@ RSpec.describe Gitlab::Database::Partitioning do
table_oid(table_name).present? table_oid(table_name).present?
end end
end end
context 'ensure that the registered models have partitioning strategy' do
it 'fails when partitioning_strategy is not specified for the model' do
expect(described_class.__send__(:registered_models)).to all(respond_to(:partitioning_strategy))
end
end
end end
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