diff options
| -rw-r--r-- | lib/dbshell/client/database.rb | 34 | ||||
| -rw-r--r-- | lib/dbshell/client/postgres.rb | 38 | ||||
| -rw-r--r-- | lib/dbshell/client/sqlite3.rb | 26 | ||||
| -rw-r--r-- | lib/dbshell/rails/tasks/dbshell.rake | 2 | ||||
| -rw-r--r-- | spec/dbshell/client/database_spec.rb | 12 | ||||
| -rw-r--r-- | spec/dbshell/client/postgres_spec.rb | 4 | ||||
| -rw-r--r-- | spec/dbshell/client/sqlite3_spec.rb | 4 | 
7 files changed, 66 insertions, 54 deletions
| diff --git a/lib/dbshell/client/database.rb b/lib/dbshell/client/database.rb index d77efce..35cb9ee 100644 --- a/lib/dbshell/client/database.rb +++ b/lib/dbshell/client/database.rb @@ -1,22 +1,26 @@  require 'dbshell/client/sqlite3'  require 'dbshell/client/postgres' -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 +module DBShell +  module Client +    class Database +      def self.handler(connection_params) +        case connection_params['adapter'] +        when 'sqlite3' +          DBShell::Client::Sqlite3 +        when 'postgresql' +          DBShell::Client::Postgres +        else +          raise DBShell::InvalidDatabaseAdapter +        end +      end -  def self.runshell(connection_params) -    self -      .handler(connection_params) -      .runshell(connection_params) +      def self.runshell(connection_params) +        self +          .handler(connection_params) +          .runshell(connection_params) +      end +    end    end  end diff --git a/lib/dbshell/client/postgres.rb b/lib/dbshell/client/postgres.rb index 1c1c663..0e33843 100644 --- a/lib/dbshell/client/postgres.rb +++ b/lib/dbshell/client/postgres.rb @@ -1,23 +1,27 @@ -class DBShell::PostgresClient -  EXECUTABLE_NAME = 'psql' +module DBShell +  module Client +    class Postgres +      EXECUTABLE_NAME = 'psql' -  def self.runshell(db_info) -    args = self.build_command(db_info) -    exec(*args) -  end +      def self.runshell(db_info) +        args = self.build_command(db_info) +        exec(*args) +      end -  def self.build_command(db_info) -    args = [EXECUTABLE_NAME] +      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'] +        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) +        args.push('-U', user) if user +        args.push('-h', host) if host +        args.push('-p', port) if port +        args.push(db_name) +      end +    end    end  end diff --git a/lib/dbshell/client/sqlite3.rb b/lib/dbshell/client/sqlite3.rb index adc71ef..09b44f9 100644 --- a/lib/dbshell/client/sqlite3.rb +++ b/lib/dbshell/client/sqlite3.rb @@ -1,15 +1,19 @@ -class DBShell::Sqlite3Client -  EXECUTABLE_NAME = 'sqlite3' +module DBShell +  module Client +    class Sqlite3 +      EXECUTABLE_NAME = 'sqlite3' -  def self.runshell(db_info) -    args = self.build_command(db_info) -    exec(*args) -  end +      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'] -    ] +      def self.build_command(db_info) +        args = [ +          EXECUTABLE_NAME, +          db_info['database'] +        ] +      end +    end    end  end diff --git a/lib/dbshell/rails/tasks/dbshell.rake b/lib/dbshell/rails/tasks/dbshell.rake index c7d3a93..cc898a4 100644 --- a/lib/dbshell/rails/tasks/dbshell.rake +++ b/lib/dbshell/rails/tasks/dbshell.rake @@ -3,7 +3,7 @@ require 'dbshell/client/database'  task :dbshell do    env = ENV.fetch('RAILS_ENV', 'development') -  DBShell::DatabaseClient.runshell( +  DBShell::Client::Database.runshell(      Rails.application.config.database_configuration[env]    )  end diff --git a/spec/dbshell/client/database_spec.rb b/spec/dbshell/client/database_spec.rb index e08d810..5fe5597 100644 --- a/spec/dbshell/client/database_spec.rb +++ b/spec/dbshell/client/database_spec.rb @@ -1,23 +1,23 @@  require 'minitest/autorun'  require 'dbshell/client/database' -describe DBShell::DatabaseClient do +describe DBShell::Client::Database do    describe ".handler" do      it "handles SQLite3" do -      DBShell::DatabaseClient.handler({ +      DBShell::Client::Database.handler({          'adapter' => 'sqlite3' -      }).must_equal(DBShell::Sqlite3Client) +      }).must_equal(DBShell::Client::Sqlite3)      end      it "handles Postgres" do -      DBShell::DatabaseClient.handler({ +      DBShell::Client::Database.handler({          'adapter' => 'postgresql' -      }).must_equal(DBShell::PostgresClient) +      }).must_equal(DBShell::Client::Postgres)      end      it "raises an error if no suitable adapter is found" do        proc do -        DBShell::DatabaseClient.handler({ +        DBShell::Client::Database.handler({            'adapter' => 'sybil_system'          })        end.must_raise(DBShell::InvalidDatabaseAdapter) diff --git a/spec/dbshell/client/postgres_spec.rb b/spec/dbshell/client/postgres_spec.rb index c267ac0..c9cb2c7 100644 --- a/spec/dbshell/client/postgres_spec.rb +++ b/spec/dbshell/client/postgres_spec.rb @@ -1,10 +1,10 @@  require 'minitest/autorun'  require 'dbshell/client/postgres' -describe DBShell::PostgresClient do +describe DBShell::Client::Postgres do    describe ".build_command" do      it "builds basic arguments" do -      DBShell::PostgresClient.build_command({ +      DBShell::Client::Postgres.build_command({          'adapter' => 'postgresql',          'host' => 'mailmarehost',          'port' => 6027, diff --git a/spec/dbshell/client/sqlite3_spec.rb b/spec/dbshell/client/sqlite3_spec.rb index df42cbd..15d35a5 100644 --- a/spec/dbshell/client/sqlite3_spec.rb +++ b/spec/dbshell/client/sqlite3_spec.rb @@ -1,10 +1,10 @@  require 'minitest/autorun'  require 'dbshell/client/sqlite3' -describe DBShell::Sqlite3Client do +describe DBShell::Client::Sqlite3 do    describe ".build_command" do      it "builds basic arguments" do -      DBShell::Sqlite3Client.build_command({ +      DBShell::Client::Sqlite3.build_command({          'adapter' => 'sqlite3',          'database' => 'db/development.sqlite3'        }).must_equal([ | 
