Skip to content
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

Postgres rake fixes #52

Merged
merged 4 commits into from
Oct 6, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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