From b700c1bef89a67a64f1040fb6bb03c0320eefe91 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 29 Apr 2017 21:55:48 +0200 Subject: Rename database client files to put them in a `Client` module Will be modifying the class names in the next commit. Here we change the file names & locations of the database client files. The format changes thusly: _client.rb -> client/.rb This organisation feels cleaner to me. Wasn't liking the previous structure. --- lib/dbshell/client/database.rb | 24 ++++++++++++++++++++++++ lib/dbshell/client/postgres.rb | 23 +++++++++++++++++++++++ lib/dbshell/client/sqlite3.rb | 15 +++++++++++++++ lib/dbshell/database_client.rb | 24 ------------------------ lib/dbshell/postgres_client.rb | 23 ----------------------- lib/dbshell/sqlite3_client.rb | 15 --------------- spec/dbshell/client/database_spec.rb | 26 ++++++++++++++++++++++++++ spec/dbshell/client/postgres_spec.rb | 26 ++++++++++++++++++++++++++ spec/dbshell/client/sqlite3_spec.rb | 16 ++++++++++++++++ spec/dbshell/database_client_spec.rb | 26 -------------------------- spec/dbshell/postgres_client_spec.rb | 26 -------------------------- spec/dbshell/sqlite3_client_spec.rb | 16 ---------------- 12 files changed, 130 insertions(+), 130 deletions(-) create mode 100644 lib/dbshell/client/database.rb create mode 100644 lib/dbshell/client/postgres.rb create mode 100644 lib/dbshell/client/sqlite3.rb delete mode 100644 lib/dbshell/database_client.rb delete mode 100644 lib/dbshell/postgres_client.rb delete mode 100644 lib/dbshell/sqlite3_client.rb create mode 100644 spec/dbshell/client/database_spec.rb create mode 100644 spec/dbshell/client/postgres_spec.rb create mode 100644 spec/dbshell/client/sqlite3_spec.rb delete mode 100644 spec/dbshell/database_client_spec.rb delete mode 100644 spec/dbshell/postgres_client_spec.rb delete mode 100644 spec/dbshell/sqlite3_client_spec.rb diff --git a/lib/dbshell/client/database.rb b/lib/dbshell/client/database.rb new file mode 100644 index 0000000..edea777 --- /dev/null +++ b/lib/dbshell/client/database.rb @@ -0,0 +1,24 @@ +require 'dbshell/sqlite3_client' +require 'dbshell/postgres_client' + +class DBShell::DatabaseClient + def self.handler(connection_params) + case connection_params['adapter'] + when 'sqlite3' + DBShell::Sqlite3Client + when 'postgresql' + DBShell::PostgresClient + else + raise DBShell::InvalidDatabaseAdapter + end + end + + def self.runshell(connection_params) + self + .handler(connection_params) + .runshell(connection_params) + end +end + + +class DBShell::InvalidDatabaseAdapter < StandardError; end diff --git a/lib/dbshell/client/postgres.rb b/lib/dbshell/client/postgres.rb new file mode 100644 index 0000000..1c1c663 --- /dev/null +++ b/lib/dbshell/client/postgres.rb @@ -0,0 +1,23 @@ +class DBShell::PostgresClient + EXECUTABLE_NAME = 'psql' + + def self.runshell(db_info) + args = self.build_command(db_info) + exec(*args) + end + + def self.build_command(db_info) + args = [EXECUTABLE_NAME] + + host = db_info['host'] + port = db_info['port'] + db_name = db_info['database'] + user = db_info['username'] + password = db_info['password'] + + args.push('-U', user) if user + args.push('-h', host) if host + args.push('-p', port) if port + args.push(db_name) + end +end diff --git a/lib/dbshell/client/sqlite3.rb b/lib/dbshell/client/sqlite3.rb new file mode 100644 index 0000000..adc71ef --- /dev/null +++ b/lib/dbshell/client/sqlite3.rb @@ -0,0 +1,15 @@ +class DBShell::Sqlite3Client + EXECUTABLE_NAME = 'sqlite3' + + def self.runshell(db_info) + args = self.build_command(db_info) + exec(*args) + end + + def self.build_command(db_info) + args = [ + EXECUTABLE_NAME, + db_info['database'] + ] + end +end diff --git a/lib/dbshell/database_client.rb b/lib/dbshell/database_client.rb deleted file mode 100644 index edea777..0000000 --- a/lib/dbshell/database_client.rb +++ /dev/null @@ -1,24 +0,0 @@ -require 'dbshell/sqlite3_client' -require 'dbshell/postgres_client' - -class DBShell::DatabaseClient - def self.handler(connection_params) - case connection_params['adapter'] - when 'sqlite3' - DBShell::Sqlite3Client - when 'postgresql' - DBShell::PostgresClient - else - raise DBShell::InvalidDatabaseAdapter - end - end - - def self.runshell(connection_params) - self - .handler(connection_params) - .runshell(connection_params) - end -end - - -class DBShell::InvalidDatabaseAdapter < StandardError; end diff --git a/lib/dbshell/postgres_client.rb b/lib/dbshell/postgres_client.rb deleted file mode 100644 index 1c1c663..0000000 --- a/lib/dbshell/postgres_client.rb +++ /dev/null @@ -1,23 +0,0 @@ -class DBShell::PostgresClient - EXECUTABLE_NAME = 'psql' - - def self.runshell(db_info) - args = self.build_command(db_info) - exec(*args) - end - - def self.build_command(db_info) - args = [EXECUTABLE_NAME] - - host = db_info['host'] - port = db_info['port'] - db_name = db_info['database'] - user = db_info['username'] - password = db_info['password'] - - args.push('-U', user) if user - args.push('-h', host) if host - args.push('-p', port) if port - args.push(db_name) - end -end diff --git a/lib/dbshell/sqlite3_client.rb b/lib/dbshell/sqlite3_client.rb deleted file mode 100644 index adc71ef..0000000 --- a/lib/dbshell/sqlite3_client.rb +++ /dev/null @@ -1,15 +0,0 @@ -class DBShell::Sqlite3Client - EXECUTABLE_NAME = 'sqlite3' - - def self.runshell(db_info) - args = self.build_command(db_info) - exec(*args) - end - - def self.build_command(db_info) - args = [ - EXECUTABLE_NAME, - db_info['database'] - ] - end -end diff --git a/spec/dbshell/client/database_spec.rb b/spec/dbshell/client/database_spec.rb new file mode 100644 index 0000000..1659fa3 --- /dev/null +++ b/spec/dbshell/client/database_spec.rb @@ -0,0 +1,26 @@ +require 'minitest/autorun' +require 'dbshell/database_client' + +describe DBShell::DatabaseClient do + describe ".handler" do + it "handles SQLite3" do + DBShell::DatabaseClient.handler({ + 'adapter' => 'sqlite3' + }).must_equal(DBShell::Sqlite3Client) + end + + it "handles Postgres" do + DBShell::DatabaseClient.handler({ + 'adapter' => 'postgresql' + }).must_equal(DBShell::PostgresClient) + end + + it "raises an error if no suitable adapter is found" do + proc do + DBShell::DatabaseClient.handler({ + 'adapter' => 'sybil_system' + }) + end.must_raise(DBShell::InvalidDatabaseAdapter) + end + end +end diff --git a/spec/dbshell/client/postgres_spec.rb b/spec/dbshell/client/postgres_spec.rb new file mode 100644 index 0000000..2f767f1 --- /dev/null +++ b/spec/dbshell/client/postgres_spec.rb @@ -0,0 +1,26 @@ +require 'minitest/autorun' +require 'dbshell/postgres_client' + +describe DBShell::PostgresClient do + describe ".build_command" do + it "builds basic arguments" do + DBShell::PostgresClient.build_command({ + 'adapter' => 'postgresql', + 'host' => 'mailmarehost', + 'port' => 6027, + 'username' => 'derpyhooves', + 'password' => 'somepassword', + 'database' => 'dbname' + }).must_equal([ + 'psql', + '-U', + 'derpyhooves', + '-h', + 'mailmarehost', + '-p', + 6027, + 'dbname' + ]) + end + end +end diff --git a/spec/dbshell/client/sqlite3_spec.rb b/spec/dbshell/client/sqlite3_spec.rb new file mode 100644 index 0000000..4922993 --- /dev/null +++ b/spec/dbshell/client/sqlite3_spec.rb @@ -0,0 +1,16 @@ +require 'minitest/autorun' +require 'dbshell/sqlite3_client' + +describe DBShell::Sqlite3Client do + describe ".build_command" do + it "builds basic arguments" do + DBShell::Sqlite3Client.build_command({ + 'adapter' => 'sqlite3', + 'database' => 'db/development.sqlite3' + }).must_equal([ + 'sqlite3', + 'db/development.sqlite3' + ]) + end + end +end diff --git a/spec/dbshell/database_client_spec.rb b/spec/dbshell/database_client_spec.rb deleted file mode 100644 index 1659fa3..0000000 --- a/spec/dbshell/database_client_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'minitest/autorun' -require 'dbshell/database_client' - -describe DBShell::DatabaseClient do - describe ".handler" do - it "handles SQLite3" do - DBShell::DatabaseClient.handler({ - 'adapter' => 'sqlite3' - }).must_equal(DBShell::Sqlite3Client) - end - - it "handles Postgres" do - DBShell::DatabaseClient.handler({ - 'adapter' => 'postgresql' - }).must_equal(DBShell::PostgresClient) - end - - it "raises an error if no suitable adapter is found" do - proc do - DBShell::DatabaseClient.handler({ - 'adapter' => 'sybil_system' - }) - end.must_raise(DBShell::InvalidDatabaseAdapter) - end - end -end diff --git a/spec/dbshell/postgres_client_spec.rb b/spec/dbshell/postgres_client_spec.rb deleted file mode 100644 index 2f767f1..0000000 --- a/spec/dbshell/postgres_client_spec.rb +++ /dev/null @@ -1,26 +0,0 @@ -require 'minitest/autorun' -require 'dbshell/postgres_client' - -describe DBShell::PostgresClient do - describe ".build_command" do - it "builds basic arguments" do - DBShell::PostgresClient.build_command({ - 'adapter' => 'postgresql', - 'host' => 'mailmarehost', - 'port' => 6027, - 'username' => 'derpyhooves', - 'password' => 'somepassword', - 'database' => 'dbname' - }).must_equal([ - 'psql', - '-U', - 'derpyhooves', - '-h', - 'mailmarehost', - '-p', - 6027, - 'dbname' - ]) - end - end -end diff --git a/spec/dbshell/sqlite3_client_spec.rb b/spec/dbshell/sqlite3_client_spec.rb deleted file mode 100644 index 4922993..0000000 --- a/spec/dbshell/sqlite3_client_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'minitest/autorun' -require 'dbshell/sqlite3_client' - -describe DBShell::Sqlite3Client do - describe ".build_command" do - it "builds basic arguments" do - DBShell::Sqlite3Client.build_command({ - 'adapter' => 'sqlite3', - 'database' => 'db/development.sqlite3' - }).must_equal([ - 'sqlite3', - 'db/development.sqlite3' - ]) - end - end -end -- cgit v1.2.3