Commit 4c99d0e5 authored by Stan Hu's avatar Stan Hu

Merge branch '8584-move-ee-specific-code-from-gitbab-database-ee-gitlab-database' into 'master'

Move EE specific code from Gitbab::Database into ee

Closes #8584

See merge request gitlab-org/gitlab-ee!8597
parents bf2b72e0 d4518791
......@@ -3,6 +3,25 @@ module EE
module Database
extend ::Gitlab::Utils::Override
override :read_only?
def read_only?
::Gitlab::Geo.secondary?
end
def healthy?
return true unless postgresql?
!Postgresql::ReplicationSlot.lag_too_great?
end
# Disables prepared statements for the current database connection.
def disable_prepared_statements
config = ActiveRecord::Base.configurations[Rails.env]
config['prepared_statements'] = false
ActiveRecord::Base.establish_connection(config)
end
override :add_post_migrate_path_to_rails
def add_post_migrate_path_to_rails(force: false)
super
......
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Database do
describe '.read_only?' do
context 'with Geo enabled' do
before do
allow(Gitlab::Geo).to receive(:enabled?) { true }
allow(Gitlab::Geo).to receive(:current_node) { geo_node }
end
context 'is Geo secondary node' do
let(:geo_node) { create(:geo_node) }
it 'returns true' do
expect(described_class.read_only?).to be_truthy
end
end
context 'is Geo primary node' do
let(:geo_node) { create(:geo_node, :primary) }
it 'returns false when is Geo primary node' do
expect(described_class.read_only?).to be_falsey
end
end
end
context 'with Geo disabled' do
it 'returns false' do
expect(described_class.read_only?).to be_falsey
end
end
end
describe '.healthy?' do
it 'returns true when using MySQL' do
allow(described_class).to receive(:postgresql?).and_return(false)
expect(described_class.healthy?).to be_truthy
end
context 'when using PostgreSQL' do
before do
allow(described_class).to receive(:postgresql?).and_return(true)
end
it 'returns true when replication lag is not too great' do
allow(Postgresql::ReplicationSlot).to receive(:lag_too_great?).and_return(false)
expect(described_class.healthy?).to be_truthy
end
it 'returns false when replication lag is too great' do
allow(Postgresql::ReplicationSlot).to receive(:lag_too_great?).and_return(true)
expect(described_class.healthy?).to be_falsey
end
end
end
describe '.disable_prepared_statements' do
it 'disables prepared statements' do
config = {}
expect(ActiveRecord::Base.configurations).to receive(:[])
.with(Rails.env)
.and_return(config)
expect(ActiveRecord::Base).to receive(:establish_connection)
.with({ 'prepared_statements' => false })
described_class.disable_prepared_statements
expect(config['prepared_statements']).to eq(false)
end
end
end
......@@ -27,7 +27,7 @@ describe Geo::PruneEventLogWorker, :geo do
end
it 'does nothing when database is not feeling healthy' do
allow(Gitlab::Database).to receive(:healthy?).and_return(false)
allow(EE::Gitlab::Database).to receive(:healthy?).and_return(false)
expect(Geo::PruneEventLogService).not_to receive(:new)
......
......@@ -39,16 +39,15 @@ module Gitlab
adapter_name.casecmp('postgresql').zero?
end
# Overridden in EE
def self.read_only?
Gitlab::Geo.secondary?
false
end
def self.read_write?
!self.read_only?
end
# check whether the underlying database is in read-only mode
# Check whether the underlying database is in read-only mode
def self.db_read_only?
if postgresql?
pg_is_in_recovery =
......@@ -103,12 +102,6 @@ module Gitlab
Gitlab::Database.postgresql_9_or_less? ? 'pg_last_xlog_replay_location' : 'pg_last_wal_replay_lsn'
end
def self.healthy?
return true unless postgresql?
!Postgresql::ReplicationSlot.lag_too_great?
end
def self.nulls_last_order(field, direction = 'ASC')
order = "#{field} #{direction}"
......@@ -141,10 +134,6 @@ module Gitlab
postgresql? ? "RANDOM()" : "RAND()"
end
def self.minute_interval(value)
postgresql? ? "#{value} * '1 minute'::interval" : "INTERVAL #{value} MINUTE"
end
def self.true_value
if postgresql?
"'t'"
......@@ -238,14 +227,6 @@ module Gitlab
ActiveRecord::ConnectionAdapters::ConnectionPool.new(spec)
end
# Disables prepared statements for the current database connection.
def self.disable_prepared_statements
config = ActiveRecord::Base.configurations[Rails.env]
config['prepared_statements'] = false
ActiveRecord::Base.establish_connection(config)
end
def self.connection
ActiveRecord::Base.connection
end
......
......@@ -443,51 +443,9 @@ describe Gitlab::Database do
end
end
describe '#disable_prepared_statements' do
it 'disables prepared statements' do
config = {}
expect(ActiveRecord::Base.configurations).to receive(:[])
.with(Rails.env)
.and_return(config)
expect(ActiveRecord::Base).to receive(:establish_connection)
.with({ 'prepared_statements' => false })
described_class.disable_prepared_statements
expect(config['prepared_statements']).to eq(false)
end
end
describe '.read_only?' do
context 'with Geo enabled' do
before do
allow(Gitlab::Geo).to receive(:enabled?) { true }
allow(Gitlab::Geo).to receive(:current_node) { geo_node }
end
context 'is Geo secondary node' do
let(:geo_node) { create(:geo_node) }
it 'returns true' do
expect(described_class.read_only?).to be_truthy
end
end
context 'is Geo primary node' do
let(:geo_node) { create(:geo_node, :primary) }
it 'returns false when is Geo primary node' do
expect(described_class.read_only?).to be_falsey
end
end
end
context 'with Geo disabled' do
it 'returns false' do
expect(described_class.read_only?).to be_falsey
end
it 'returns false' do
expect(described_class.read_only?).to be_falsey
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