Skip to content

Commit 26050cf

Browse files
committed
Use postgres for local connections in all DBs
1 parent 0674a3c commit 26050cf

10 files changed

Lines changed: 80 additions & 6 deletions

File tree

definitions/checks/foreman/validate_external_db_version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ValidateExternalDbVersion < ForemanMaintain::Check
1111
end
1212

1313
def run
14-
current_db_version = feature(:foreman_database).db_version(feature(:foreman_database).local?)
14+
current_db_version = feature(:foreman_database).db_version
1515
fail!(db_upgrade_message(current_db_version)) if current_db_version.major < 13
1616
end
1717

definitions/features/candlepin_database.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ def load_configuration
4747
'driver_class' => full_config['jpa.config.hibernate.connection.driver_class'],
4848
'url' => url,
4949
}
50+
51+
# Build connection string for pg_dump (only used for local databases)
52+
@configuration['connection_string'] = "postgres:///#{@configuration['database']}"
53+
@configuration
5054
end
5155

5256
def extend_with_db_options

definitions/features/foreman_database.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ def load_configuration
4343

4444
@configuration = config['production']
4545
@configuration['host'] ||= 'localhost'
46+
47+
# Build connection string for pg_dump (only used for local databases)
48+
@configuration['connection_string'] = "postgres:///#{@configuration['database']}"
4649
@configuration
4750
end
4851
end

definitions/features/pulpcore_database.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ def load_configuration
4141
@configuration['database'] = db_config['NAME']
4242
@configuration['username'] = db_config['USER']
4343
@configuration['password'] = db_config['PASSWORD']
44+
45+
# Build connection string for pg_dump (only used for local databases)
46+
@configuration['connection_string'] = "postgres:///#{db_config['NAME']}"
4447
@configuration
4548
end
4649
end

definitions/scenarios/restore.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def compose
5151
add_step_with_context(Procedures::Crond::Start)
5252
end
5353

54-
# rubocop:disable Metrics/MethodLength,Metrics/AbcSize,Metrics/CyclomaticComplexity
54+
# rubocop:disable Metrics/CyclomaticComplexity
5555
def restore_sql_dumps(backup)
5656
if feature(:instance).postgresql_local?
5757
add_step(Procedures::Service::Start.new(:only => ['postgresql']))

lib/foreman_maintain/concerns/base_database.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def db_version
143143

144144
query = 'SHOW server_version'
145145
server_version_cmd = 'psql --tuples-only --no-align'
146-
version_string = if localdb
146+
version_string = if local?
147147
execute!("runuser - postgres -c '#{server_version_cmd} -c \"#{query}\"'")
148148
else
149149
execute!(server_version_cmd, :stdin => query, :env => base_env)

test/definitions/features/candlepin_database_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,21 @@ def stub_without_ssl_config(&block)
3636
refute_includes url, 'sslrootcert=/usr/share/foreman/root.crt'
3737
end
3838
end
39+
40+
it 'Sets connection_string for local database backups with SSL' do
41+
stub_with_ssl_config do
42+
config = subject.configuration
43+
assert_equal 'candlepin1db', config['database']
44+
assert_equal 'postgres:///candlepin1db', config['connection_string']
45+
end
46+
end
47+
48+
it 'Sets connection_string for local database backups without SSL' do
49+
stub_without_ssl_config do
50+
config = subject.configuration
51+
assert_equal 'candlepin', config['database']
52+
assert_equal 'postgres:///candlepin', config['connection_string']
53+
end
54+
end
3955
end
4056
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require 'test_helper'
2+
3+
describe Features::ForemanDatabase do
4+
include DefinitionsTestHelper
5+
6+
subject { Features::ForemanDatabase.new }
7+
8+
describe '.configuration' do
9+
it 'returns hash with DB config and connection_string' do
10+
database_yml_content = {
11+
'production' => {
12+
'adapter' => 'postgresql',
13+
'host' => 'localhost',
14+
'port' => 5432,
15+
'database' => 'foreman',
16+
'username' => 'foreman',
17+
'password' => 'password',
18+
},
19+
}
20+
21+
File.expects(:exist?).with('/etc/foreman/database.yml').returns(true)
22+
File.expects(:read).with('/etc/foreman/database.yml').returns('mocked_yaml_content')
23+
YAML.expects(:load).with('mocked_yaml_content').returns(database_yml_content)
24+
25+
expected = {
26+
'adapter' => 'postgresql',
27+
'host' => 'localhost',
28+
'port' => 5432,
29+
'database' => 'foreman',
30+
'username' => 'foreman',
31+
'password' => 'password',
32+
'connection_string' => 'postgres:///foreman',
33+
}
34+
assert_equal expected, subject.configuration
35+
end
36+
end
37+
end

test/definitions/features/pulpcore_database_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
JSON
1616
subject.expects(:execute!).with(expected_command, merge_stderr: false).returns(manager_return)
1717
expected = { "adapter" => "postgresql", "host" => "remotedb", "port" => "5432",
18-
"database" => "pulpcore", "username" => "pulp", "password" => "password" }
18+
"database" => "pulpcore", "username" => "pulp", "password" => "password",
19+
"connection_string" => "postgres:///pulpcore" }
1920
assert_equal expected, subject.configuration
2021
end
2122
end

test/lib/concerns/base_database_test.rb

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,24 @@ module ForemanMaintain
3737
assert db.local?
3838
end
3939

40-
it 'fetches server version' do
40+
it 'fetches server version remotely' do
41+
db.expects(:local?).returns(false)
4142
db.expects(:ping!)
4243
db.expects(:execute!).with(
4344
'psql --tuples-only --no-align',
4445
stdin: 'SHOW server_version',
4546
env: expected_env
4647
).returns('13.16')
47-
db.expects(:localdb).returns(false)
48+
49+
assert db.db_version
50+
end
51+
52+
it 'fetches server version locally' do
53+
db.expects(:local?).returns(true)
54+
db.expects(:ping!)
55+
db.expects(:execute!).with(
56+
"runuser - postgres -c 'psql --tuples-only --no-align -c \"SHOW server_version\"'"
57+
).returns('13.16')
4858

4959
assert db.db_version
5060
end

0 commit comments

Comments
 (0)