From 2dc2cfac13c5d158d35a6c2ef73be46bda7dc18e Mon Sep 17 00:00:00 2001 From: Steve Hoeksema Date: Thu, 22 Jun 2017 15:07:53 +1200 Subject: [PATCH 1/3] Don't try and drop a database that doesn't exist --- lib/sequel_rails/storage/postgres.rb | 1 + spec/lib/sequel_rails/storage/postgres_spec.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/sequel_rails/storage/postgres.rb b/lib/sequel_rails/storage/postgres.rb index ae3c378..5f915a5 100644 --- a/lib/sequel_rails/storage/postgres.rb +++ b/lib/sequel_rails/storage/postgres.rb @@ -22,6 +22,7 @@ def _drop with_pgpassword do commands = ['dropdb'] add_connection_settings commands + add_flag commands, '--if-exists' commands << database safe_exec commands end diff --git a/spec/lib/sequel_rails/storage/postgres_spec.rb b/spec/lib/sequel_rails/storage/postgres_spec.rb index aa928da..7a676f1 100644 --- a/spec/lib/sequel_rails/storage/postgres_spec.rb +++ b/spec/lib/sequel_rails/storage/postgres_spec.rb @@ -69,7 +69,7 @@ describe '#_drop' do it 'uses the dropdb command' do expect(subject).to receive(:`).with( - "dropdb --username\\=#{username} --host\\=#{host} --port\\=#{port} #{database}" + "dropdb --username\\=#{username} --host\\=#{host} --port\\=#{port} --if-exists #{database}" ) subject._drop end From 723be90555b900f77daae5a45e3efeebfda25053 Mon Sep 17 00:00:00 2001 From: Steve Hoeksema Date: Thu, 22 Jun 2017 15:08:35 +1200 Subject: [PATCH 2/3] Ignore errors trying to connect to a database that doesn't exist when dropping it --- lib/sequel_rails/storage/postgres.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/sequel_rails/storage/postgres.rb b/lib/sequel_rails/storage/postgres.rb index 5f915a5..0d13b80 100644 --- a/lib/sequel_rails/storage/postgres.rb +++ b/lib/sequel_rails/storage/postgres.rb @@ -65,6 +65,9 @@ def close_connections # command. Seems to be only way to ensure *all* test connections # are closed nil + rescue Sequel::DatabaseConnectionError + # Will raise an error if the database doesn't exist. + nil end def encoding From 05b332e09013d6dd556f05b1cfec0ee8e82dc7a7 Mon Sep 17 00:00:00 2001 From: Steve Hoeksema Date: Thu, 22 Jun 2017 15:08:53 +1200 Subject: [PATCH 3/3] Don't specify a port when it's blank or zero --- lib/sequel_rails/storage/postgres.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/sequel_rails/storage/postgres.rb b/lib/sequel_rails/storage/postgres.rb index 0d13b80..80df1fa 100644 --- a/lib/sequel_rails/storage/postgres.rb +++ b/lib/sequel_rails/storage/postgres.rb @@ -108,9 +108,9 @@ def with_pgpassword end def add_connection_settings(commands) - add_option commands, '--username', username - add_option commands, '--host', host - add_option commands, '--port', port.to_s + add_option commands, '--username', username unless username.blank? + add_option commands, '--host', host unless host.blank? + add_option commands, '--port', port.to_s unless port.to_s.blank? || port.to_s == "0" end end end