Skip to content

Commit

Permalink
Merge pull request #223 from joneslee85/dropdb-postgres
Browse files Browse the repository at this point in the history
Use dropdb to drop DB with PostgreSQL
  • Loading branch information
Trung Lê committed Aug 5, 2015
2 parents f3bab56 + 1e3ebf5 commit 99f3146
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
26 changes: 18 additions & 8 deletions lib/lotus/model/migrator/postgres_adapter.rb
Expand Up @@ -32,15 +32,25 @@ def create
# @since 0.4.0
# @api private
def drop
new_connection.run %(DROP DATABASE "#{ database }")
rescue Sequel::DatabaseError => e
message = if e.message.match(/does not exist/)
"Cannot find database: #{ database }"
else
e.message
end
set_environment_variables

require 'open3'

Open3.popen3('dropdb', database) do |stdin, stdout, stderr, wait_thr|
exit_status = wait_thr.value

raise MigrationError.new(message)
unless exit_status.success?
error_message = stderr.read

message = if error_message.match(/does not exist/)
"Cannot find database: #{ database }"
else
message
end

raise MigrationError.new(message)
end
end
end

# @since 0.4.0
Expand Down
1 change: 0 additions & 1 deletion test/integration/migrations_test.rb
Expand Up @@ -390,7 +390,6 @@

it "raises error if database doesn't exist" do
Lotus::Model::Migrator.drop # remove the first time

exception = -> { Lotus::Model::Migrator.drop }.must_raise Lotus::Model::MigrationError
exception.message.must_equal "Cannot find database: #{ @database }"
end
Expand Down

0 comments on commit 99f3146

Please sign in to comment.