Commit a0548a22 authored by Stan Hu's avatar Stan Hu

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

CE port of 'Move EE specific code from Gitbab::Database into ee'

See merge request gitlab-org/gitlab-ce!23361
parents 5d82035f 5f8423dd
...@@ -35,7 +35,6 @@ module Gitlab ...@@ -35,7 +35,6 @@ module Gitlab
adapter_name.casecmp('postgresql').zero? adapter_name.casecmp('postgresql').zero?
end end
# Overridden in EE
def self.read_only? def self.read_only?
false false
end end
...@@ -44,12 +43,14 @@ module Gitlab ...@@ -44,12 +43,14 @@ module Gitlab
!self.read_only? !self.read_only?
end 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? def self.db_read_only?
if postgresql? if postgresql?
pg_is_in_recovery =
ActiveRecord::Base.connection.execute('SELECT pg_is_in_recovery()') ActiveRecord::Base.connection.execute('SELECT pg_is_in_recovery()')
.first .first.fetch('pg_is_in_recovery')
.fetch('pg_is_in_recovery') == 't'
Gitlab::Utils.to_boolean(pg_is_in_recovery)
else else
false false
end end
......
...@@ -443,11 +443,17 @@ describe Gitlab::Database do ...@@ -443,11 +443,17 @@ describe Gitlab::Database do
end end
end end
describe '.read_only?' do
it 'returns false' do
expect(described_class.read_only?).to be_falsey
end
end
describe '.db_read_only?' do describe '.db_read_only?' do
context 'when using PostgreSQL' do context 'when using PostgreSQL' do
before do before do
allow(ActiveRecord::Base.connection).to receive(:execute).and_call_original allow(ActiveRecord::Base.connection).to receive(:execute).and_call_original
expect(described_class).to receive(:postgresql?).and_return(true) allow(described_class).to receive(:postgresql?).and_return(true)
end end
it 'detects a read only database' do it 'detects a read only database' do
...@@ -456,11 +462,25 @@ describe Gitlab::Database do ...@@ -456,11 +462,25 @@ describe Gitlab::Database do
expect(described_class.db_read_only?).to be_truthy expect(described_class.db_read_only?).to be_truthy
end end
# TODO: remove rails5-only tag after removing rails4 tests
it 'detects a read only database', :rails5 do
allow(ActiveRecord::Base.connection).to receive(:execute).with('SELECT pg_is_in_recovery()').and_return([{ "pg_is_in_recovery" => true }])
expect(described_class.db_read_only?).to be_truthy
end
it 'detects a read write database' do it 'detects a read write database' do
allow(ActiveRecord::Base.connection).to receive(:execute).with('SELECT pg_is_in_recovery()').and_return([{ "pg_is_in_recovery" => "f" }]) allow(ActiveRecord::Base.connection).to receive(:execute).with('SELECT pg_is_in_recovery()').and_return([{ "pg_is_in_recovery" => "f" }])
expect(described_class.db_read_only?).to be_falsey expect(described_class.db_read_only?).to be_falsey
end end
# TODO: remove rails5-only tag after removing rails4 tests
it 'detects a read write database', :rails5 do
allow(ActiveRecord::Base.connection).to receive(:execute).with('SELECT pg_is_in_recovery()').and_return([{ "pg_is_in_recovery" => false }])
expect(described_class.db_read_only?).to be_falsey
end
end end
context 'when using MySQL' do context 'when using MySQL' do
......
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