From 1156010222a51643c7921aa953aad052e91b666c Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 29 Apr 2017 20:14:51 +0200 Subject: Add `DBShell::DatabaseClient` A more generic interface to specific database adapter clients. This class will act as a frontend to the others. Calling its `.runshell()` will call the appropriate `.runshell()` for the adapter provided in `connection_params`. This way, we have a single entry point to start a database shell from the Rake `dbshell` task. --- lib/dbshell/database_client.rb | 15 +++++++++++++++ spec/dbshell/database_client_spec.rb | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 lib/dbshell/database_client.rb create mode 100644 spec/dbshell/database_client_spec.rb diff --git a/lib/dbshell/database_client.rb b/lib/dbshell/database_client.rb new file mode 100644 index 0000000..8d55095 --- /dev/null +++ b/lib/dbshell/database_client.rb @@ -0,0 +1,15 @@ +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 +end + + +class DBShell::InvalidDatabaseAdapter < StandardError; end diff --git a/spec/dbshell/database_client_spec.rb b/spec/dbshell/database_client_spec.rb new file mode 100644 index 0000000..1659fa3 --- /dev/null +++ b/spec/dbshell/database_client_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 -- cgit v1.2.3