From f1bcb2b9759d89a22fc254e8779a26cf69b8a45f Mon Sep 17 00:00:00 2001 From: Kevin Menard Date: Fri, 4 Oct 2013 23:01:33 -0400 Subject: [PATCH 1/4] Using system() instead of backticks creates an environment issue on Ubuntu where pg_wrapper doesn't work properly. --- lib/sequel_rails/storage/postgres.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/sequel_rails/storage/postgres.rb b/lib/sequel_rails/storage/postgres.rb index e857ac4..801932b 100644 --- a/lib/sequel_rails/storage/postgres.rb +++ b/lib/sequel_rails/storage/postgres.rb @@ -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 = `#{commands.join(' ')}` ENV["PGPASSWORD"] = nil unless password.blank? res end @@ -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 = `#{commands.join(' ')}` ENV["PGPASSWORD"] = nil unless password.blank? res end @@ -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 = `#{commands.join(' ')}` ENV["PGPASSWORD"] = nil unless password.blank? res end @@ -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 = `#{commands.join(' ')}` ENV["PGPASSWORD"] = nil unless password.blank? res end From 5ab72afda67d76aa23711e51bdd262b4547e9623 Mon Sep 17 00:00:00 2001 From: Kevin Menard Date: Fri, 4 Oct 2013 23:02:52 -0400 Subject: [PATCH 2/4] Since we're using system commands anyway, delegate up to the Postgres handler so we invoke the commands with the correct arguments. --- lib/sequel_rails/storage/jdbc.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/sequel_rails/storage/jdbc.rb b/lib/sequel_rails/storage/jdbc.rb index a4e653a..3eb3d90 100644 --- a/lib/sequel_rails/storage/jdbc.rb +++ b/lib/sequel_rails/storage/jdbc.rb @@ -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 @@ -48,7 +49,8 @@ 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 From 854632376e5adba2f083c9b0c7ad596ddac70387 Mon Sep 17 00:00:00 2001 From: Kevin Menard Date: Fri, 4 Oct 2013 23:03:27 -0400 Subject: [PATCH 3/4] Implement JDBC Postgres schema dumping and loading by delegating to the non-JDBC Postgres adapter. --- lib/sequel_rails/storage/jdbc.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/lib/sequel_rails/storage/jdbc.rb b/lib/sequel_rails/storage/jdbc.rb index 3eb3d90..fc2e242 100644 --- a/lib/sequel_rails/storage/jdbc.rb +++ b/lib/sequel_rails/storage/jdbc.rb @@ -54,6 +54,24 @@ def _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 + private def collation From e5f4d731d8aaed3bc3ee0567140d2b8fb279a6a9 Mon Sep 17 00:00:00 2001 From: Kevin Menard Date: Sat, 5 Oct 2013 15:38:33 -0400 Subject: [PATCH 4/4] Escape command arguments when using backticks. Also, normalize evaluated value between backticks and :system calls. --- lib/sequel_rails/storage/postgres.rb | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/sequel_rails/storage/postgres.rb b/lib/sequel_rails/storage/postgres.rb index 801932b..04e5e8f 100644 --- a/lib/sequel_rails/storage/postgres.rb +++ b/lib/sequel_rails/storage/postgres.rb @@ -9,7 +9,7 @@ def _create commands << "--port" << port.to_s unless port.blank? commands << "--host" << host unless host.blank? commands << database - res = `#{commands.join(' ')}` + res = safe_exec(commands) ENV["PGPASSWORD"] = nil unless password.blank? res end @@ -21,7 +21,7 @@ def _drop commands << "--port" << port.to_s unless port.blank? commands << "--host" << host unless host.blank? commands << database - res = `#{commands.join(' ')}` + res = safe_exec(commands) ENV["PGPASSWORD"] = nil unless password.blank? res end @@ -34,7 +34,7 @@ def _dump(filename) commands << "--port" << port.to_s unless port.blank? commands << "--host" << host unless host.blank? commands << database - res = `#{commands.join(' ')}` + res = safe_exec(commands) ENV["PGPASSWORD"] = nil unless password.blank? res end @@ -47,7 +47,7 @@ def _load(filename) commands << "--port" << port.to_s unless port.blank? commands << "--host" << host unless host.blank? commands << database - res = `#{commands.join(' ')}` + res = safe_exec(commands) ENV["PGPASSWORD"] = nil unless password.blank? res end @@ -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