debian-mirror-gitlab/spec/lib/gitlab/database_spec.rb

485 lines
16 KiB
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2015-10-24 18:46:33 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe Gitlab::Database do
2017-08-17 22:00:37 +05:30
before do
stub_const('MigrationTest', Class.new { include Gitlab::Database })
end
2020-07-28 23:09:34 +05:30
describe 'EXTRA_SCHEMAS' do
it 'contains only schemas starting with gitlab_ prefix' do
described_class::EXTRA_SCHEMAS.each do |schema|
expect(schema.to_s).to start_with('gitlab_')
end
end
end
2021-11-11 11:23:49 +05:30
describe '.default_pool_size' do
before do
allow(Gitlab::Runtime).to receive(:max_threads).and_return(7)
end
it 'returns the max thread size plus a fixed headroom of 10' do
expect(described_class.default_pool_size).to eq(17)
end
it 'returns the max thread size plus a DB_POOL_HEADROOM if this env var is present' do
stub_env('DB_POOL_HEADROOM', '7')
expect(described_class.default_pool_size).to eq(14)
end
end
2021-09-30 23:02:18 +05:30
describe '.has_config?' do
context 'two tier database config' do
before do
allow(Gitlab::Application).to receive_message_chain(:config, :database_configuration, :[]).with(Rails.env)
.and_return({ "adapter" => "postgresql", "database" => "gitlabhq_test" })
end
it 'returns false for primary' do
expect(described_class.has_config?(:primary)).to eq(false)
end
it 'returns false for ci' do
expect(described_class.has_config?(:ci)).to eq(false)
end
end
context 'three tier database config' do
before do
allow(Gitlab::Application).to receive_message_chain(:config, :database_configuration, :[]).with(Rails.env)
.and_return({
"primary" => { "adapter" => "postgresql", "database" => "gitlabhq_test" },
"ci" => { "adapter" => "postgresql", "database" => "gitlabhq_test_ci" }
})
end
it 'returns true for primary' do
expect(described_class.has_config?(:primary)).to eq(true)
end
it 'returns true for ci' do
expect(described_class.has_config?(:ci)).to eq(true)
end
it 'returns false for non-existent' do
expect(described_class.has_config?(:nonexistent)).to eq(false)
end
end
end
2022-04-04 11:22:00 +05:30
describe '.check_for_non_superuser' do
subject { described_class.check_for_non_superuser }
let(:non_superuser) { Gitlab::Database::PgUser.new(usename: 'foo', usesuper: false ) }
let(:superuser) { Gitlab::Database::PgUser.new(usename: 'bar', usesuper: true) }
it 'prints user details if not superuser' do
allow(Gitlab::Database::PgUser).to receive(:find_by).with('usename = CURRENT_USER').and_return(non_superuser)
expect(Gitlab::AppLogger).to receive(:info).with("Account details: User: \"foo\", UseSuper: (false)")
subject
end
it 'raises an exception if superuser' do
allow(Gitlab::Database::PgUser).to receive(:find_by).with('usename = CURRENT_USER').and_return(superuser)
expect(Gitlab::AppLogger).to receive(:info).with("Account details: User: \"bar\", UseSuper: (true)")
expect { subject }.to raise_error('Error: detected superuser')
end
it 'catches exception if find_by fails' do
allow(Gitlab::Database::PgUser).to receive(:find_by).with('usename = CURRENT_USER').and_raise(ActiveRecord::StatementInvalid)
expect { subject }.to raise_error('User CURRENT_USER not found')
end
end
2020-07-28 23:09:34 +05:30
describe '.check_postgres_version_and_print_warning' do
2021-12-11 22:18:48 +05:30
let(:reflect) { instance_spy(Gitlab::Database::Reflection) }
2020-07-28 23:09:34 +05:30
subject { described_class.check_postgres_version_and_print_warning }
2021-12-11 22:18:48 +05:30
before do
allow(Gitlab::Database::Reflection)
.to receive(:new)
.and_return(reflect)
end
2020-07-28 23:09:34 +05:30
it 'prints a warning if not compliant with minimum postgres version' do
2021-12-11 22:18:48 +05:30
allow(reflect).to receive(:postgresql_minimum_supported_version?).and_return(false)
2020-07-28 23:09:34 +05:30
2021-12-11 22:18:48 +05:30
expect(Kernel)
.to receive(:warn)
.with(/You are using PostgreSQL/)
.exactly(Gitlab::Database.database_base_models.length)
.times
2020-07-28 23:09:34 +05:30
subject
end
it 'doesnt print a warning if compliant with minimum postgres version' do
2021-12-11 22:18:48 +05:30
allow(reflect).to receive(:postgresql_minimum_supported_version?).and_return(true)
2020-07-28 23:09:34 +05:30
expect(Kernel).not_to receive(:warn).with(/You are using PostgreSQL/)
subject
end
it 'doesnt print a warning in Rails runner environment' do
2021-12-11 22:18:48 +05:30
allow(reflect).to receive(:postgresql_minimum_supported_version?).and_return(false)
2020-07-28 23:09:34 +05:30
allow(Gitlab::Runtime).to receive(:rails_runner?).and_return(true)
expect(Kernel).not_to receive(:warn).with(/You are using PostgreSQL/)
2018-03-17 18:26:18 +05:30
2020-07-28 23:09:34 +05:30
subject
2018-03-17 18:26:18 +05:30
end
2020-07-28 23:09:34 +05:30
it 'ignores ActiveRecord errors' do
2021-12-11 22:18:48 +05:30
allow(reflect).to receive(:postgresql_minimum_supported_version?).and_raise(ActiveRecord::ActiveRecordError)
2018-03-17 18:26:18 +05:30
2020-07-28 23:09:34 +05:30
expect { subject }.not_to raise_error
end
it 'ignores Postgres errors' do
2021-12-11 22:18:48 +05:30
allow(reflect).to receive(:postgresql_minimum_supported_version?).and_raise(PG::Error)
2020-07-28 23:09:34 +05:30
expect { subject }.not_to raise_error
2018-03-17 18:26:18 +05:30
end
end
2021-11-11 11:23:49 +05:30
describe '.db_config_for_connection' do
context 'when the regular connection is used' do
it 'returns db_config' do
connection = ActiveRecord::Base.retrieve_connection
2021-01-03 14:25:43 +05:30
2021-11-11 11:23:49 +05:30
expect(described_class.db_config_for_connection(connection)).to eq(connection.pool.db_config)
end
end
2022-07-23 23:45:48 +05:30
context 'when the connection is LoadBalancing::ConnectionProxy', :database_replica do
it 'returns primary db config even if ambiguous queries default to replica' do
Gitlab::Database::LoadBalancing::Session.current.use_primary!
primary_config = described_class.db_config_for_connection(ActiveRecord::Base.connection)
Gitlab::Database::LoadBalancing::Session.clear_session
Gitlab::Database::LoadBalancing::Session.current.fallback_to_replicas_for_ambiguous_queries do
expect(described_class.db_config_for_connection(ActiveRecord::Base.connection)).to eq(primary_config)
end
2021-11-11 11:23:49 +05:30
end
2021-09-30 23:02:18 +05:30
end
context 'when the pool is a NullPool' do
2021-11-11 11:23:49 +05:30
it 'returns nil' do
2021-09-30 23:02:18 +05:30
connection = double(:active_record_connection, pool: ActiveRecord::ConnectionAdapters::NullPool.new)
2021-11-11 11:23:49 +05:30
expect(described_class.db_config_for_connection(connection)).to be_nil
2021-09-30 23:02:18 +05:30
end
end
end
2021-11-11 11:23:49 +05:30
describe '.db_config_name' do
it 'returns the db_config name for the connection' do
2021-11-18 22:05:49 +05:30
model = ActiveRecord::Base
# This is a ConnectionProxy
expect(described_class.db_config_name(model.connection))
2022-05-07 20:08:51 +05:30
.to eq('main')
2021-11-11 11:23:49 +05:30
2021-11-18 22:05:49 +05:30
# This is an actual connection
expect(described_class.db_config_name(model.retrieve_connection))
.to eq('main')
end
context 'when replicas are configured', :database_replica do
it 'returns the name for a replica' do
2021-12-11 22:18:48 +05:30
replica = ActiveRecord::Base.load_balancer.host
2021-11-18 22:05:49 +05:30
expect(described_class.db_config_name(replica)).to eq('main_replica')
end
2021-11-11 11:23:49 +05:30
end
end
2022-06-21 17:19:12 +05:30
describe '.db_config_names' do
let(:expected) { %w[foo bar] }
it 'includes only main by default' do
allow(::ActiveRecord::Base).to receive(:configurations).and_return(
double(configs_for: %w[foo bar].map { |x| double(name: x) })
)
expect(described_class.db_config_names).to eq(expected)
end
it 'excludes geo when that is included' do
allow(::ActiveRecord::Base).to receive(:configurations).and_return(
double(configs_for: %w[foo bar geo].map { |x| double(name: x) })
)
expect(described_class.db_config_names).to eq(expected)
end
end
2022-05-07 20:08:51 +05:30
describe '.gitlab_schemas_for_connection' do
it 'does return a valid schema depending on a base model used', :request_store do
expect(described_class.gitlab_schemas_for_connection(Project.connection)).to include(:gitlab_main, :gitlab_shared)
expect(described_class.gitlab_schemas_for_connection(Ci::Build.connection)).to include(:gitlab_ci, :gitlab_shared)
end
it 'does return gitlab_ci when a ActiveRecord::Base is using CI connection' do
with_reestablished_active_record_base do
reconfigure_db_connection(model: ActiveRecord::Base, config_model: Ci::Build)
expect(described_class.gitlab_schemas_for_connection(ActiveRecord::Base.connection)).to include(:gitlab_ci, :gitlab_shared)
end
end
2022-06-21 17:19:12 +05:30
context "when there's CI connection", :request_store do
before do
skip_if_multiple_databases_not_setup
# FF due to lib/gitlab/database/load_balancing/configuration.rb:92
# Requires usage of `:request_store`
stub_feature_flags(force_no_sharing_primary_model: true)
end
context 'when CI uses database_tasks: false does indicate that ci: is subset of main:' do
before do
allow(Ci::ApplicationRecord.connection_db_config).to receive(:database_tasks?).and_return(false)
end
it 'does return gitlab_ci when accessing via main: connection' do
expect(described_class.gitlab_schemas_for_connection(Project.connection)).to include(:gitlab_ci, :gitlab_main, :gitlab_shared)
end
it 'does not return gitlab_main when accessing via ci: connection' do
expect(described_class.gitlab_schemas_for_connection(Ci::Build.connection)).to include(:gitlab_ci, :gitlab_shared)
expect(described_class.gitlab_schemas_for_connection(Ci::Build.connection)).not_to include(:gitlab_main)
end
end
context 'when CI uses database_tasks: true does indicate that ci: has own database' do
before do
allow(Ci::ApplicationRecord.connection_db_config).to receive(:database_tasks?).and_return(true)
end
it 'does not return gitlab_ci when accessing via main: connection' do
expect(described_class.gitlab_schemas_for_connection(Project.connection)).to include(:gitlab_main, :gitlab_shared)
expect(described_class.gitlab_schemas_for_connection(Project.connection)).not_to include(:gitlab_ci)
end
it 'does not return gitlab_main when accessing via ci: connection' do
expect(described_class.gitlab_schemas_for_connection(Ci::Build.connection)).to include(:gitlab_ci, :gitlab_shared)
expect(described_class.gitlab_schemas_for_connection(Ci::Build.connection)).not_to include(:gitlab_main)
end
end
end
2022-07-23 23:45:48 +05:30
it 'does return empty for non-adopted connections' do
new_connection = ActiveRecord::Base.postgresql_connection(
ActiveRecord::Base.connection_db_config.configuration_hash)
expect(described_class.gitlab_schemas_for_connection(new_connection)).to be_nil
ensure
new_connection&.disconnect!
end
2022-05-07 20:08:51 +05:30
end
2016-04-02 18:10:28 +05:30
describe '#true_value' do
2019-10-12 21:52:04 +05:30
it 'returns correct value' do
2017-08-17 22:00:37 +05:30
expect(described_class.true_value).to eq "'t'"
2016-04-02 18:10:28 +05:30
end
end
describe '#false_value' do
2019-10-12 21:52:04 +05:30
it 'returns correct value' do
2017-08-17 22:00:37 +05:30
expect(described_class.false_value).to eq "'f'"
2016-04-02 18:10:28 +05:30
end
end
2018-03-17 18:26:18 +05:30
describe '#sanitize_timestamp' do
let(:max_timestamp) { Time.at((1 << 31) - 1) }
subject { described_class.sanitize_timestamp(timestamp) }
context 'with a timestamp smaller than MAX_TIMESTAMP_VALUE' do
let(:timestamp) { max_timestamp - 10.years }
it 'returns the given timestamp' do
expect(subject).to eq(timestamp)
end
end
context 'with a timestamp larger than MAX_TIMESTAMP_VALUE' do
let(:timestamp) { max_timestamp + 1.second }
it 'returns MAX_TIMESTAMP_VALUE' do
expect(subject).to eq(max_timestamp)
end
end
end
2021-04-17 20:07:23 +05:30
2022-05-07 20:08:51 +05:30
describe '.all_uncached' do
let(:base_model) do
Class.new do
def self.uncached
@uncached = true
yield
end
end
end
let(:model1) { Class.new(base_model) }
let(:model2) { Class.new(base_model) }
before do
2022-08-13 15:12:31 +05:30
allow(described_class).to receive(:database_base_models_using_load_balancing)
2022-05-07 20:08:51 +05:30
.and_return({ model1: model1, model2: model2 }.with_indifferent_access)
end
it 'wraps the given block in uncached calls for each primary connection', :aggregate_failures do
expect(model1.instance_variable_get(:@uncached)).to be_nil
expect(model2.instance_variable_get(:@uncached)).to be_nil
expect(Gitlab::Database::LoadBalancing::Session.current).to receive(:use_primary).and_yield
expect(model2).to receive(:uncached).and_call_original
expect(model1).to receive(:uncached).and_call_original
yielded_to_block = false
described_class.all_uncached do
expect(model1.instance_variable_get(:@uncached)).to be(true)
expect(model2.instance_variable_get(:@uncached)).to be(true)
yielded_to_block = true
end
expect(yielded_to_block).to be(true)
end
end
2021-10-27 15:23:28 +05:30
describe '.read_only?' do
it 'returns false' do
expect(described_class.read_only?).to eq(false)
end
end
describe '.read_write' do
it 'returns true' do
expect(described_class.read_write?).to eq(true)
end
end
2021-04-17 20:07:23 +05:30
describe 'ActiveRecordBaseTransactionMetrics' do
def subscribe_events
events = []
begin
subscriber = ActiveSupport::Notifications.subscribe('transaction.active_record') do |e|
events << e
end
yield
ensure
ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end
events
end
context 'without a transaction block' do
it 'does not publish a transaction event' do
events = subscribe_events do
User.first
end
expect(events).to be_empty
end
end
context 'within a transaction block' do
it 'publishes a transaction event' do
events = subscribe_events do
ActiveRecord::Base.transaction do
User.first
end
end
expect(events.length).to be(1)
event = events.first
expect(event).not_to be_nil
expect(event.duration).to be > 0.0
expect(event.payload).to a_hash_including(
2021-11-18 22:05:49 +05:30
connection: be_a(Gitlab::Database::LoadBalancing::ConnectionProxy)
2021-04-17 20:07:23 +05:30
)
end
end
context 'within an empty transaction block' do
it 'publishes a transaction event' do
events = subscribe_events do
ActiveRecord::Base.transaction {}
end
expect(events.length).to be(1)
event = events.first
expect(event).not_to be_nil
expect(event.duration).to be > 0.0
expect(event.payload).to a_hash_including(
2021-11-18 22:05:49 +05:30
connection: be_a(Gitlab::Database::LoadBalancing::ConnectionProxy)
2021-04-17 20:07:23 +05:30
)
end
end
context 'within a nested transaction block' do
it 'publishes multiple transaction events' do
events = subscribe_events do
ActiveRecord::Base.transaction do
ActiveRecord::Base.transaction do
ActiveRecord::Base.transaction do
User.first
end
end
end
end
expect(events.length).to be(3)
events.each do |event|
expect(event).not_to be_nil
expect(event.duration).to be > 0.0
expect(event.payload).to a_hash_including(
2021-11-18 22:05:49 +05:30
connection: be_a(Gitlab::Database::LoadBalancing::ConnectionProxy)
2021-04-17 20:07:23 +05:30
)
end
end
end
context 'within a cancelled transaction block' do
it 'publishes multiple transaction events' do
events = subscribe_events do
ActiveRecord::Base.transaction do
User.first
raise ActiveRecord::Rollback
end
end
expect(events.length).to be(1)
event = events.first
expect(event).not_to be_nil
expect(event.duration).to be > 0.0
expect(event.payload).to a_hash_including(
2021-11-18 22:05:49 +05:30
connection: be_a(Gitlab::Database::LoadBalancing::ConnectionProxy)
2021-04-17 20:07:23 +05:30
)
end
end
end
2015-10-24 18:46:33 +05:30
end