lib/sequel_rails/storage/postgres.rb in sequel-rails-0.6.1 vs lib/sequel_rails/storage/postgres.rb in sequel-rails-0.7.0
- old
+ new
@@ -1,57 +1,57 @@
module SequelRails
module Storage
class Postgres < Abstract
+
def _create
- ENV["PGPASSWORD"] = password unless password.blank?
- commands = ["createdb", "--encoding", charset]
- commands << "--username" << username unless username.blank?
- commands << "--owner" << owner unless owner.blank?
- commands << "--port" << port.to_s unless port.blank?
- commands << "--host" << host unless host.blank?
- commands << database
- res = system(*commands)
- ENV["PGPASSWORD"] = nil unless password.blank?
- res
+ with_pgpassword do
+ commands = ["createdb"]
+ add_connection_settings commands
+ add_option commands, "--maintenance-db", maintenance_db
+ add_option commands, "--encoding", encoding
+ add_option commands, "--locale", locale
+ add_option commands, "--lc-collate", collation
+ add_option commands, "--lc-ctype", ctype
+ add_option commands, "--template", template
+ add_option commands, "--tablespace", tablespace
+ add_option commands, "--owner", owner
+ commands << database
+ safe_exec commands
+ end
end
def _drop
- ENV["PGPASSWORD"] = password unless password.blank?
- commands = ["dropdb"]
- commands << "-U" << username unless username.blank?
- commands << "--port" << port.to_s unless port.blank?
- commands << "--host" << host unless host.blank?
- commands << database
- res = system(*commands)
- ENV["PGPASSWORD"] = nil unless password.blank?
- res
+ with_pgpassword do
+ commands = ["dropdb"]
+ add_connection_settings commands
+ commands << database
+ safe_exec commands
+ end
end
def _dump(filename)
- ENV["PGPASSWORD"] = password unless password.blank?
- commands = %w(pg_dump -i -s -x -O)
- commands << "-f" << filename
- commands << "-U" << username unless username.blank?
- commands << "--port" << port.to_s unless port.blank?
- commands << "--host" << host unless host.blank?
- commands << database
- res = system(*commands)
- ENV["PGPASSWORD"] = nil unless password.blank?
- res
+ with_pgpassword do
+ commands = ["pg_dump"]
+ add_connection_settings commands
+ add_flag commands, "-i"
+ add_flag commands, "-s"
+ add_flag commands, "-x"
+ add_flag commands, "-O"
+ add_option commands, "--file", filename
+ commands << database
+ safe_exec commands
+ end
end
def _load(filename)
- ENV["PGPASSWORD"] = password unless password.blank?
- commands = %w(psql)
- commands << "-f" << filename
- commands << "-U" << username unless username.blank?
- commands << "--port" << port.to_s unless port.blank?
- commands << "--host" << host unless host.blank?
- commands << database
- res = system(*commands)
- ENV["PGPASSWORD"] = nil unless password.blank?
- res
+ with_pgpassword do
+ commands = ["psql"]
+ add_connection_settings commands
+ add_option commands, "--file", filename
+ commands << database
+ safe_exec commands
+ end
end
def close_connections
begin
db = ::Sequel.connect(config)
@@ -66,8 +66,48 @@
# Will raise an error as it kills existing process running this
# command. Seems to be only way to ensure *all* test connections
# are closed
end
end
+
+ def encoding
+ @encoding ||= config["encoding"] || charset
+ end
+
+ def locale
+ @locale ||= config["locale"] || ""
+ end
+
+ def template
+ @template ||= config["template"] || ""
+ end
+
+ def ctype
+ @ctype ||= config["ctype"] || ""
+ end
+
+ def tablespace
+ @tablespace ||= config["tablespace"] || ""
+ end
+
+ def maintenance_db
+ @maintenance_db ||= config["maintenance_db"] || ""
+ end
+
+ private
+
+ def with_pgpassword
+ ENV["PGPASSWORD"] = password unless password.blank?
+ yield
+ ensure
+ ENV["PGPASSWORD"] = nil unless password.blank?
+ end
+
+ def add_connection_settings(commands)
+ add_option commands, "--username", username
+ add_option commands, "--host", host
+ add_option commands, "--port", port.to_s
+ end
+
end
end
end