From 1276a1f212c5512a00416bb60bd72d2791fb78b7 Mon Sep 17 00:00:00 2001 From: Gannon McGibbon Date: Wed, 28 Sep 2022 14:15:03 -0500 Subject: [PATCH] Add dbconsole support to adapter Adds dbconsole method to be used when invoking bin/rails dbconsole. Co-authored-by: Paarth Madan --- .../oracle_enhanced_adapter.rb | 28 +++++++++++++++---- .../oracle_enhanced/dbconsole_spec.rb | 27 ++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 spec/active_record/connection_adapters/oracle_enhanced/dbconsole_spec.rb diff --git a/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb b/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb index d623b5e84..eb14afa59 100644 --- a/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb +++ b/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb @@ -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_adapter_class # :nodoc: 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_adapter_class.new( + ConnectionAdapters::OracleEnhanced::Connection.create(config), logger, config) + end end module ConnectionAdapters # :nodoc: @@ -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 diff --git a/spec/active_record/connection_adapters/oracle_enhanced/dbconsole_spec.rb b/spec/active_record/connection_adapters/oracle_enhanced/dbconsole_spec.rb new file mode 100644 index 000000000..802db4974 --- /dev/null +++ b/spec/active_record/connection_adapters/oracle_enhanced/dbconsole_spec.rb @@ -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