Skip to content

Commit

Permalink
Merge pull request #52 from mogotest/postgres_rake_fixes
Browse files Browse the repository at this point in the history
Postgres rake fixes
  • Loading branch information
JonathanTron committed Oct 6, 2013
2 parents bbe7e47 + e5f4d73 commit 1e86e26
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
24 changes: 22 additions & 2 deletions lib/sequel_rails/storage/jdbc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def _create
db.execute("CREATE DATABASE IF NOT EXISTS `#{db_name}` DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}")
end
elsif _is_postgres?
system("createdb #{db_name}")
adapter = ::SequelRails::Storage::Postgres.new(config)
adapter._create
end
end

Expand All @@ -48,7 +49,26 @@ def _drop
db.execute("DROP DATABASE IF EXISTS `#{db_name}`")
end
elsif _is_postgres?
system("dropdb #{db_name}")
adapter = ::SequelRails::Storage::Postgres.new(config)
adapter._drop
end
end

def _dump(filename)
if _is_postgres?
adapter = ::SequelRails::Storage::Postgres.new(config)
adapter._dump(filename)
else
raise NotImplementedError
end
end

def _load(filename)
if _is_postgres?
adapter = ::SequelRails::Storage::Postgres.new(config)
adapter._load(filename)
else
raise NotImplementedError
end
end

Expand Down
21 changes: 17 additions & 4 deletions lib/sequel_rails/storage/postgres.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def _create
commands << "--port" << port.to_s unless port.blank?
commands << "--host" << host unless host.blank?
commands << database
res = system(*commands)
res = safe_exec(commands)
ENV["PGPASSWORD"] = nil unless password.blank?
res
end
Expand All @@ -21,7 +21,7 @@ def _drop
commands << "--port" << port.to_s unless port.blank?
commands << "--host" << host unless host.blank?
commands << database
res = system(*commands)
res = safe_exec(commands)
ENV["PGPASSWORD"] = nil unless password.blank?
res
end
Expand All @@ -34,7 +34,7 @@ def _dump(filename)
commands << "--port" << port.to_s unless port.blank?
commands << "--host" << host unless host.blank?
commands << database
res = system(*commands)
res = safe_exec(commands)
ENV["PGPASSWORD"] = nil unless password.blank?
res
end
Expand All @@ -47,7 +47,7 @@ def _load(filename)
commands << "--port" << port.to_s unless port.blank?
commands << "--host" << host unless host.blank?
commands << database
res = system(*commands)
res = safe_exec(commands)
ENV["PGPASSWORD"] = nil unless password.blank?
res
end
Expand All @@ -68,6 +68,19 @@ def close_connections
# are closed
end
end

def safe_exec(args)
begin
require 'shellwords'

`#{Shellwords.join(args)}`

# Evaluate command status as a boolean like `system` does.
$?.exitstatus == 0
rescue LoadError
system(args)
end
end
end
end
end

0 comments on commit 1e86e26

Please sign in to comment.