-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor BaseDatabase class for simplicity #945
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
470a5ea
allow passing env to CommandRunner
evgeni 747e89e
Drop config parameter on database instance methods
ekohl d75f89b
Remove dump_command helper method that's only used once
ekohl 66cf3f7
Use -f parameter for pg_dump
ekohl f701d88
Pass PostgreSQL password via environment variables
ekohl 4325b10
Pass in PostgreSQL credentials via environment variables
ekohl bb1681f
Also pass in the database name via an environment variable
ekohl de3110b
Pass in the version query via stdin
ekohl 1e34fba
Use long options to make a command self documenting
ekohl 4aa6f95
Introduce a ping! helper that raises an exception
ekohl d3020de
Completely cache the Debian PostgreSQL versions
ekohl 31ac520
drop hidden_patterns
evgeni 5e89e8c
Use <<~ for more predictable indenting
ekohl 0ea3d4e
add tests for base database concern
evgeni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
require 'test_helper' | ||
|
||
class FakeDatabase | ||
include ForemanMaintain::Concerns::BaseDatabase | ||
include ForemanMaintain::Concerns::SystemHelpers | ||
|
||
attr_reader :configuration | ||
|
||
def initialize(config) | ||
@configuration = config | ||
end | ||
end | ||
|
||
module ForemanMaintain | ||
describe Concerns::BaseDatabase do | ||
describe 'with local db' do | ||
let(:db) { FakeDatabase.new(config) } | ||
let(:config) do | ||
{ | ||
'host' => 'localhost', | ||
'database' => 'fakedb', | ||
'username' => 'fakeuser', | ||
'password' => 'fakepassword', | ||
} | ||
end | ||
let(:expected_env) do | ||
{ | ||
'PGHOST' => 'localhost', | ||
'PGPORT' => nil, | ||
'PGDATABASE' => 'fakedb', | ||
'PGUSER' => 'fakeuser', | ||
'PGPASSWORD' => 'fakepassword', | ||
} | ||
end | ||
|
||
it 'accepts localhost as local' do | ||
assert db.local? | ||
end | ||
|
||
it 'fetches server version' do | ||
db.expects(:ping!) | ||
db.expects(:execute!).with( | ||
'psql --tuples-only --no-align', | ||
stdin: 'SHOW server_version', | ||
env: expected_env | ||
).returns('13.16') | ||
|
||
assert db.db_version | ||
end | ||
|
||
it 'drops db' do | ||
db.expects(:execute!).with("runuser - postgres -c 'dropdb fakedb'").returns('') | ||
|
||
assert db.dropdb | ||
end | ||
|
||
it 'restores db' do | ||
file = '/backup/fake.dump' | ||
|
||
db.expects(:execute!).with("runuser - postgres -c 'pg_restore -C -d postgres #{file}'"). | ||
returns('') | ||
|
||
assert db.restore_dump(file, true) | ||
end | ||
|
||
it 'dumps db' do | ||
file = '/backup/fake.dump' | ||
|
||
db.expects(:execute!).with( | ||
"pg_dump -Fc -f /backup/fake.dump", | ||
env: expected_env | ||
).returns('') | ||
|
||
assert db.dump_db(file) | ||
end | ||
|
||
it 'pings db' do | ||
db.expects(:execute?).with("psql", | ||
stdin: "SELECT 1 as ping", env: expected_env).returns(true) | ||
|
||
assert db.ping | ||
end | ||
|
||
it 'runs db queries' do | ||
psql_return = <<~PSQL | ||
test | ||
------ | ||
42 | ||
(1 row) | ||
PSQL | ||
|
||
db.expects(:ping!) | ||
db.expects(:execute).with("psql", | ||
stdin: "SELECT 42 as test", env: expected_env).returns(psql_return) | ||
|
||
assert db.psql('SELECT 42 as test') | ||
end | ||
end | ||
|
||
describe 'with remote db' do | ||
let(:db) { FakeDatabase.new(config) } | ||
let(:config) do | ||
{ | ||
'host' => 'db.example.com', | ||
'database' => 'fakedb', | ||
'username' => 'fakeuser', | ||
'password' => 'fakepassword', | ||
} | ||
end | ||
let(:expected_env) do | ||
{ | ||
'PGHOST' => 'db.example.com', | ||
'PGPORT' => nil, | ||
'PGDATABASE' => 'fakedb', | ||
'PGUSER' => 'fakeuser', | ||
'PGPASSWORD' => 'fakepassword', | ||
} | ||
end | ||
|
||
it 'accepts db.example.com as remote' do | ||
refute db.local? | ||
end | ||
|
||
it 'drops db' do | ||
select_statement = <<~SQL | ||
select string_agg('drop table if exists \"' || tablename || '\" cascade;', '') | ||
from pg_tables | ||
where schemaname = 'public'; | ||
SQL | ||
delete_statement = 'drop table if exists \"faketable\"' | ||
db.expects(:psql).with(select_statement).returns(delete_statement) | ||
db.expects(:psql).with(delete_statement).returns('') | ||
assert db.dropdb | ||
end | ||
|
||
it 'restores db' do | ||
file = '/backup/fake.dump' | ||
restore_cmd = <<~CMD.strip | ||
pg_restore --no-privileges --clean --disable-triggers -n public -d fakedb #{file} | ||
CMD | ||
|
||
db.expects(:execute!).with( | ||
restore_cmd, | ||
valid_exit_statuses: [0, 1], | ||
env: expected_env | ||
).returns('') | ||
|
||
assert db.restore_dump(file, false) | ||
end | ||
|
||
it 'dumps remote db' do | ||
file = '/backup/fake.dump' | ||
|
||
db.expects(:execute!).with( | ||
"pg_dump -Fc -f /backup/fake.dump", | ||
env: expected_env | ||
).returns('') | ||
|
||
assert db.dump_db(file) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for my own sanity, I checked and https://rubyapi.org/3.3/o/process states: