Skip to content

Commit

Permalink
Add dbconsole support to adapter
Browse files Browse the repository at this point in the history
Adds dbconsole method to be used when invoking bin/rails dbconsole.

Co-authored-by: Paarth Madan <paarth.madan@shopify.com>
  • Loading branch information
gmcgibbon and paarthmadan committed Sep 29, 2022
1 parent 3ec3919 commit 74cfe19
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
28 changes: 22 additions & 6 deletions lib/active_record/connection_adapters/oracle_enhanced_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,22 @@

module ActiveRecord
module ConnectionHandling # :nodoc:
# Establishes a connection to the database that's used by all Active Record objects.
def oracle_enhanced_connection(config) # :nodoc:
def oracle_enhanced_connection_class
if config[:emulate_oracle_adapter] == true
# allows the enhanced adapter to look like the OracleAdapter. Useful to pick up
# conditionals in the rails activerecord test suite
require "active_record/connection_adapters/emulation/oracle_adapter"
ConnectionAdapters::OracleAdapter.new(
ConnectionAdapters::OracleEnhanced::Connection.create(config), logger, config)
ConnectionAdapters::OracleAdapter
else
ConnectionAdapters::OracleEnhancedAdapter.new(
ConnectionAdapters::OracleEnhanced::Connection.create(config), logger, config)
ConnectionAdapters::OracleEnhancedAdapter
end
end

# Establishes a connection to the database that's used by all Active Record objects.
def oracle_enhanced_connection(config) # :nodoc:
oracle_enhanced_connection_class.new(
ConnectionAdapters::OracleEnhanced::Connection.create(config), logger, config)
end
end

module ConnectionAdapters # :nodoc:
Expand Down Expand Up @@ -275,6 +278,19 @@ def arel_visitor # :nodoc:
end
end

def self.dbconsole(config, options = {})
oracle_config = config.configuration_hash
logon = ""

if config[:username]
logon = oracle_config[:username].dup
logon << "/#{oracle_config[:password]}" if oracle_config[:password] && options[:include_password]
logon << "@#{config.database}" if config.database
end

find_cmd_and_exec("sqlplus", logon)
end

def build_statement_pool
StatementPool.new(self.class.type_cast_config_to_integer(@config[:statement_limit]))
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

describe "Oracle Enhanced adapter dbconsole" do
subject { ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter }

it "uses sqlplus to connect to database" do
expect(subject).to receive(:find_cmd_and_exec).with("sqlplus", "user@db")

config = make_db_config(adapter: "oracle", database: "db", username: "user", password: "secret")

subject.dbconsole(config)
end

it "uses sqlplus with password when specified" do
expect(subject).to receive(:find_cmd_and_exec).with("sqlplus", "user/secret@db")

config = make_db_config(adapter: "oracle", database: "db", username: "user", password: "secret")

subject.dbconsole(config, include_password: true)
end

private

def make_db_config(config)
ActiveRecord::DatabaseConfigurations::HashConfig.new("test", "primary", config)
end
end

0 comments on commit 74cfe19

Please sign in to comment.