Commit 423c0fb4 authored by Toon Claes's avatar Toon Claes

Make it possible to enable/disable PostgreSQL FDW for Geo

User are able to set `fdw: true` in their `config/database_geo.yml` to
use PostgreSQL FDW for Geo.

By default it is disabled, and if FDW isn't configured properly, it
isn't used anyway.

Closes gitlab-org/gitlab-ee#4558.
parent 20ce2ec6
---
title: Make it possible to enable/disable PostgreSQL FDW for Geo
merge_request: 4020
author:
type: added
......@@ -9,6 +9,7 @@ production:
username: git
password: "secure password"
host: localhost
# fdw: false
#
# Development specific
......@@ -21,6 +22,7 @@ development:
username: postgres
password: "secure password"
host: localhost
# fdw: false
#
# Staging specific
......@@ -33,6 +35,7 @@ staging:
username: git
password: "secure password"
host: localhost
# fdw: false
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
......@@ -45,3 +48,4 @@ test: &test
username: postgres
password:
host: localhost
# fdw: false
......@@ -65,14 +65,22 @@ module Gitlab
::License.feature_available?(:geo)
end
def self.fdw?
self.cache_value(:geo_fdw?) do
::Geo::BaseRegistry.connection.execute(
def self.fdw_capable?
self.cache_value(:geo_fdw_capable?) do
::Geo::TrackingBase.connection.execute(
"SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '#{FDW_SCHEMA}' AND table_type = 'FOREIGN TABLE'"
).first.fetch('count').to_i.positive?
end
end
def self.fdw?
return false unless self.fdw_capable?
# FDW is disabled by default: https://gitlab.com/gitlab-org/gitlab-ee/issues/4558
# Enable it by setting `fdw: true` in config/database_geo.yml
Rails.configuration.geo_database.fetch('fdw', false)
end
def self.fdw_table(table_name)
FDW_SCHEMA + ".#{table_name}"
end
......
......@@ -18,24 +18,57 @@ describe Gitlab::Geo, :geo do
end
end
describe 'fdw?' do
describe 'fdw_capable?' do
let(:fdw_check) { "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'gitlab_secondary' AND table_type = 'FOREIGN TABLE'" }
before do
allow(::Geo::BaseRegistry.connection).to receive(:execute).with(anything).and_call_original
allow(::Geo::TrackingBase.connection).to receive(:execute).with(anything).and_call_original
end
it 'returns true when PostgreSQL FDW is enabled' do
expect(::Geo::BaseRegistry.connection).to receive(:execute).with(fdw_check).and_return([{ 'count' => 1 }])
expect(::Geo::TrackingBase.connection).to receive(:execute).with(fdw_check).and_return([{ 'count' => 1 }])
expect(described_class.fdw_capable?).to be_truthy
end
it 'returns false when PostgreSQL FDW is not enabled' do
expect(::Geo::TrackingBase.connection).to receive(:execute).with(fdw_check).and_return([{ 'count' => 0 }])
expect(described_class.fdw?).to be_truthy
expect(described_class.fdw_capable?).to be_falsey
end
end
describe 'fdw?' do
it 'returns false when PostgreSQL FDW is not enabled' do
expect(::Geo::BaseRegistry.connection).to receive(:execute).with(fdw_check).and_return([{ 'count' => 0 }])
allow(::Geo::TrackingBase.connection).to receive(:execute).and_return([{ 'count' => 0 }])
allow(Rails.configuration).to receive(:geo_database).and_return('fdw' => true)
expect(described_class.fdw?).to be_falsey
end
context 'fdw capable' do
before do
allow(described_class).to receive(:fdw_capable?).and_return(true)
end
it 'returns false by default' do
allow(Rails.configuration).to receive(:geo_database).and_return('fdw' => nil)
expect(described_class.fdw?).to be_falsey
end
it 'returns false if configured in `config/database_geo.yml`' do
allow(Rails.configuration).to receive(:geo_database).and_return('fdw' => false)
expect(described_class.fdw?).to be_falsey
end
it 'returns true if configured in `config/database_geo.yml`' do
allow(Rails.configuration).to receive(:geo_database).and_return('fdw' => true)
expect(described_class.fdw?).to be_truthy
end
end
end
describe 'primary?' 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