Skip to content

Commit bf89b63

Browse files
Add dbconsole support to adapter
Adds dbconsole method to be used when invoking bin/rails dbconsole. Co-authored-by: Paarth Madan <[email protected]>
1 parent 3ec3919 commit bf89b63

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

lib/active_record/connection_adapters/oracle_enhanced_adapter.rb

+22-6
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,22 @@
6464

6565
module ActiveRecord
6666
module ConnectionHandling # :nodoc:
67-
# Establishes a connection to the database that's used by all Active Record objects.
68-
def oracle_enhanced_connection(config) # :nodoc:
67+
def oracle_enhanced_connection_class # :nodoc:
6968
if config[:emulate_oracle_adapter] == true
7069
# allows the enhanced adapter to look like the OracleAdapter. Useful to pick up
7170
# conditionals in the rails activerecord test suite
7271
require "active_record/connection_adapters/emulation/oracle_adapter"
73-
ConnectionAdapters::OracleAdapter.new(
74-
ConnectionAdapters::OracleEnhanced::Connection.create(config), logger, config)
72+
ConnectionAdapters::OracleAdapter
7573
else
76-
ConnectionAdapters::OracleEnhancedAdapter.new(
77-
ConnectionAdapters::OracleEnhanced::Connection.create(config), logger, config)
74+
ConnectionAdapters::OracleEnhancedAdapter
7875
end
7976
end
77+
78+
# Establishes a connection to the database that's used by all Active Record objects.
79+
def oracle_enhanced_connection(config) # :nodoc:
80+
oracle_enhanced_connection_class.new(
81+
ConnectionAdapters::OracleEnhanced::Connection.create(config), logger, config)
82+
end
8083
end
8184

8285
module ConnectionAdapters # :nodoc:
@@ -275,6 +278,19 @@ def arel_visitor # :nodoc:
275278
end
276279
end
277280

281+
def self.dbconsole(config, options = {})
282+
oracle_config = config.configuration_hash
283+
logon = ""
284+
285+
if config[:username]
286+
logon = oracle_config[:username].dup
287+
logon << "/#{oracle_config[:password]}" if oracle_config[:password] && options[:include_password]
288+
logon << "@#{config.database}" if config.database
289+
end
290+
291+
find_cmd_and_exec("sqlplus", logon)
292+
end
293+
278294
def build_statement_pool
279295
StatementPool.new(self.class.type_cast_config_to_integer(@config[:statement_limit]))
280296
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
describe "Oracle Enhanced adapter dbconsole" do
4+
subject { ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter }
5+
6+
it "uses sqlplus to connect to database" do
7+
expect(subject).to receive(:find_cmd_and_exec).with("sqlplus", "user@db")
8+
9+
config = make_db_config(adapter: "oracle", database: "db", username: "user", password: "secret")
10+
11+
subject.dbconsole(config)
12+
end
13+
14+
it "uses sqlplus with password when specified" do
15+
expect(subject).to receive(:find_cmd_and_exec).with("sqlplus", "user/secret@db")
16+
17+
config = make_db_config(adapter: "oracle", database: "db", username: "user", password: "secret")
18+
19+
subject.dbconsole(config, include_password: true)
20+
end
21+
22+
private
23+
24+
def make_db_config(config)
25+
ActiveRecord::DatabaseConfigurations::HashConfig.new("test", "primary", config)
26+
end
27+
end

0 commit comments

Comments
 (0)