Skip to content

Commit

Permalink
Fixing passwords with the postgresql database adaptor
Browse files Browse the repository at this point in the history
Updated a bit of verbosity in the specs
  • Loading branch information
David Kowis committed Jun 8, 2011
1 parent 5d80880 commit dea9d31
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
15 changes: 12 additions & 3 deletions lib/backup/database/postgresql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,19 @@ def tables_to_dump
##
# Builds the credentials PostgreSQL syntax to authenticate the user
# to perform the database dumping process
def credential_options
def username_options
return '' unless username.is_a?(String) and not username.empty?
"--username='#{username}'"
end

##
# Builds the password syntax PostgreSQL uses to authenticate the user
# to perform database dumping
def password_options
return '' unless password.is_a?(String) and not username.empty?
"PGPASSWORD='#{password}'"
end

##
# Builds the PostgreSQL connectivity options syntax to connect the user
# to perform the database dumping process, socket gets gsub'd to host since
Expand All @@ -92,8 +100,9 @@ def options
##
# Builds the full pgdump string based on all attributes
def pgdump
"#{ utility(:pg_dump) } #{ credential_options } #{ connectivity_options } " +
"#{ options } #{ tables_to_dump } #{ tables_to_skip } #{ name }"
("#{password_options} " +
"#{ utility(:pg_dump) } #{ username_options } #{ connectivity_options } " +
"#{ options } #{ tables_to_dump } #{ tables_to_skip } #{ name }").strip
end

##
Expand Down
41 changes: 31 additions & 10 deletions spec/database/postgresql_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,20 @@
db.additional_options.should == []
end

it do
it 'handles an empty username' do
db = Backup::Database::PostgreSQL.new {}
db.username = ''

db.credential_options.should == ''
db.username_options.should == ''
db.password_options.should == ''
end

it do
it 'handles a nil username' do
db = Backup::Database::PostgreSQL.new {}
db.username = nil

db.credential_options.should == ''
db.username_options.should == ''
db.password_options.should == ''
end

it 'should ensure the directory is available' do
Expand Down Expand Up @@ -91,17 +93,23 @@
end
end

describe '#credential_options' do
it 'should return the postgresql syntax for the credential options' do
db.credential_options.should == "--username='someuser'"
describe '#username_options' do
it 'should return the postgresql syntax for the username options' do
db.username_options.should == "--username='someuser'"
end

it 'should only return the postgresql syntax for the user' do
db = Backup::Database::PostgreSQL.new do |db|
db.username = 'someuser'
end

db.credential_options.should == "--username='someuser'"
db.username_options.should == "--username='someuser'"
end
end

describe '#password_options' do
it 'returns the environment variable set for the password' do
db.password_options.should == "PGPASSWORD='secret'"
end
end

Expand Down Expand Up @@ -133,14 +141,27 @@
end

describe '#pg_dump_string' do
it 'should return the full pg_dump string' do
before do
db.expects(:utility).with(:pg_dump).returns('pg_dump')
end

it 'should return the full pg_dump string' do
db.pgdump.should ==
"pg_dump --username='someuser' " +
"PGPASSWORD='secret' pg_dump --username='someuser' " +
"--host='localhost' --port='123' --host='/pg.sock' " +
"--single-transaction --quick --table='users' --table='pirates' " +
"--exclude-table='logs' --exclude-table='profiles' mydatabase"
end

it 'returns the full pg_dump string when a password is not specified' do
db.password = nil
db.pgdump.should ==
"pg_dump --username='someuser' " +
"--host='localhost' --port='123' --host='/pg.sock' " +
"--single-transaction --quick --table='users' --table='pirates' " +
"--exclude-table='logs' --exclude-table='profiles' mydatabase"

end
end

describe '#perform!' do
Expand Down

0 comments on commit dea9d31

Please sign in to comment.