Skip to content

Rails 7.2 support! #2424

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ jobs:
strategy:
fail-fast: false
matrix:
# Rails 7.0 requires Ruby 2.7 or higeher.
# Rails 7.2 requires Ruby 3.1 or higeher.
# CI pending the following matrix until JRuby 9.4 that supports Ruby 2.7 will be released.
# https://github.com/jruby/jruby/issues/6464
# - jruby,
# - jruby-head
ruby: [
'3.4',
'3.3',
'3.2',
'3.1',
'3.0',
'2.7'
]
env:
ORACLE_HOME: /opt/oracle/instantclient_23_6
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/test_11g.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ jobs:
strategy:
fail-fast: false
matrix:
# Rails 7.0 requires Ruby 2.7 or higeher.
# Rails 7.2 requires Ruby 3.1 or higeher.
# CI pending the following matrix until JRuby 9.4 that supports Ruby 2.7 will be released.
# https://github.com/jruby/jruby/issues/6464
# - jruby,
# - jruby-head
ruby: [
'3.4',
'3.3',
'3.2',
'3.1',
'3.0',
'2.7'
]
env:
ORACLE_HOME: /opt/oracle/instantclient_21_15
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ group :development do
gem "rubocop-rails", require: false
gem "rubocop-rspec", require: false

gem "activerecord", github: "rails/rails", branch: "7-1-stable"
gem "activerecord", github: "rails/rails", branch: "7-2-stable"
gem "ruby-plsql", github: "rsim/ruby-plsql", branch: "master"

platforms :ruby do
Expand Down
2 changes: 1 addition & 1 deletion activerecord-oracle_enhanced-adapter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ This adapter is superset of original ActiveRecord Oracle adapter.
"rubygems_mfa_required" => "true"
}

s.add_runtime_dependency("activerecord", ["~> 7.1.0"])
s.add_runtime_dependency("activerecord", ["~> 7.2.0"])
s.add_runtime_dependency("ruby-plsql", [">= 0.6.0"])
if /java/.match?(RUBY_PLATFORM)
s.platform = Gem::Platform.new("java")
Expand Down
94 changes: 47 additions & 47 deletions lib/active_record/connection_adapters/oracle_enhanced/quoting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,60 @@ module ActiveRecord
module ConnectionAdapters
module OracleEnhanced
module Quoting
extend ActiveSupport::Concern
# QUOTING ==================================================
#
# see: abstract/quoting.rb
QUOTED_COLUMN_NAMES = Concurrent::Map.new # :nodoc:
QUOTED_TABLE_NAMES = Concurrent::Map.new # :nodoc:

def quote_column_name(name) # :nodoc:
name = name.to_s
QUOTED_COLUMN_NAMES[name] ||= if /\A[a-z][a-z_0-9$#]*\Z/.match?(name)
"\"#{name.upcase}\""
else
# remove double quotes which cannot be used inside quoted identifier
"\"#{name.delete('"')}\""
module ClassMethods # :nodoc:
def column_name_matcher
/
\A
(
(?:
# "table_name"."column_name" | function(one or no argument)
((?:\w+\.|"\w+"\.)?(?:\w+|"\w+") | \w+\((?:|\g<2>)\))
)
(?:(?:\s+AS)?\s+(?:\w+|"\w+"))?
)
(?:\s*,\s*\g<1>)*
\z
/ix
end

def column_name_with_order_matcher
/
\A
(
(?:
# "table_name"."column_name" | function(one or no argument)
((?:\w+\.|"\w+"\.)?(?:\w+|"\w+") | \w+\((?:|\g<2>)\))
)
(?:\s+ASC|\s+DESC)?
(?:\s+NULLS\s+(?:FIRST|LAST))?
)
(?:\s*,\s*\g<1>)*
\z
/ix
end

def quote_column_name(name) # :nodoc:
name = name.to_s
QUOTED_COLUMN_NAMES[name] ||= if /\A[a-z][a-z_0-9$#]*\Z/.match?(name)
"\"#{name.upcase}\""
else
# remove double quotes which cannot be used inside quoted identifier
"\"#{name.delete('"')}\""
end
end

def quote_table_name(name) # :nodoc:
name, _link = name.to_s.split("@")
QUOTED_TABLE_NAMES[name] ||= [name.split(".").map { |n| quote_column_name(n) }].join(".")
end

end

# This method is used in add_index to identify either column name (which is quoted)
Expand Down Expand Up @@ -67,10 +107,6 @@ def self.mixed_case?(name)
!!(object_name =~ /[A-Z]/ && object_name =~ /[a-z]/)
end

def quote_table_name(name) # :nodoc:
name, _link = name.to_s.split("@")
QUOTED_TABLE_NAMES[name] ||= [name.split(".").map { |n| quote_column_name(n) }].join(".")
end

def quote_string(s) # :nodoc:
s.gsub(/'/, "''")
Expand Down Expand Up @@ -131,42 +167,6 @@ def type_cast(value)
end
end

def column_name_matcher
COLUMN_NAME
end

def column_name_with_order_matcher
COLUMN_NAME_WITH_ORDER
end

COLUMN_NAME = /
\A
(
(?:
# "table_name"."column_name" | function(one or no argument)
((?:\w+\.|"\w+"\.)?(?:\w+|"\w+") | \w+\((?:|\g<2>)\))
)
(?:(?:\s+AS)?\s+(?:\w+|"\w+"))?
)
(?:\s*,\s*\g<1>)*
\z
/ix

COLUMN_NAME_WITH_ORDER = /
\A
(
(?:
# "table_name"."column_name" | function(one or no argument)
((?:\w+\.|"\w+"\.)?(?:\w+|"\w+") | \w+\((?:|\g<2>)\))
)
(?:\s+ASC|\s+DESC)?
(?:\s+NULLS\s+(?:FIRST|LAST))?
)
(?:\s*,\s*\g<1>)*
\z
/ix
private_constant :COLUMN_NAME, :COLUMN_NAME_WITH_ORDER

private
def oracle_downcase(column_name)
return nil if column_name.nil?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def drop_table(table_name, **options) # :nodoc:
end

def insert_versions_sql(versions) # :nodoc:
sm_table = quote_table_name(ActiveRecord::Base.connection.schema_migration.table_name)
sm_table = quote_table_name(ActiveRecord::Tasks::DatabaseTasks.migration_connection_pool.schema_migration.table_name)

if supports_multi_insert?
versions.inject(+"INSERT ALL\n") { |sql, version|
Expand Down
35 changes: 20 additions & 15 deletions lib/active_record/connection_adapters/oracle_enhanced_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,6 @@
require "active_record/type/oracle_enhanced/character_string"

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:
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)
else
ConnectionAdapters::OracleEnhancedAdapter.new(
ConnectionAdapters::OracleEnhanced::Connection.create(config), logger, config)
end
end
end

module ConnectionAdapters # :nodoc:
# Oracle enhanced adapter will work with both
Expand Down Expand Up @@ -837,6 +822,26 @@ def select_value_forcing_binds(arel, name, binds)
end
end

## Register OracleEnhancedAdapter as the adapter to use for "oracle_enhanced" connection string
if ActiveRecord::ConnectionAdapters.respond_to?(:register)
ActiveRecord::ConnectionAdapters.register(
"oracle_enhanced",
"ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter",
"active_record/connection_adapters/oracle_enhanced_adapter"
)

# This is similar to the notion of emulating the original OracleAdapter but
# using the OracleEnhancedAdapter instead, but without using the emulate flag.
# Instead this will get picked up if you set the adapter to 'oracle' in the database config.
#
# Register OracleAdapter as the adapter to use for "oracle" connection string
ActiveRecord::ConnectionAdapters.register(
"oracle",
"ActiveRecord::ConnectionAdapters::OracleAdapter",
"active_record/connection_adapters/emulation/oracle_adapter"
)
end

require "active_record/connection_adapters/oracle_enhanced/version"

module ActiveRecord
Expand Down
8 changes: 0 additions & 8 deletions lib/arel/visitors/oracle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,6 @@ def split_order_string(string)
array
end

def visit_ActiveModel_Attribute(o, collector)
collector.add_bind(o) { |i| ":a#{i}" }
end

def visit_Arel_Nodes_BindParam(o, collector)
collector.add_bind(o.value) { |i| ":a#{i}" }
end

def is_distinct_from(o, collector)
collector << "DECODE("
collector = visit [o.left, o.right, 0, 1], collector
Expand Down
8 changes: 0 additions & 8 deletions lib/arel/visitors/oracle12.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,6 @@ def visit_Arel_Nodes_UpdateStatement(o, collector)
super
end

def visit_ActiveModel_Attribute(o, collector)
collector.add_bind(o) { |i| ":a#{i}" }
end

def visit_Arel_Nodes_BindParam(o, collector)
collector.add_bind(o.value) { |i| ":a#{i}" }
end

def is_distinct_from(o, collector)
collector << "DECODE("
collector = visit [o.left, o.right, 0, 1], collector
Expand Down
6 changes: 6 additions & 0 deletions lib/arel/visitors/oracle_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
module Arel # :nodoc: all
module Visitors
module OracleCommon

BIND_BLOCK = proc { |i| ":a#{i}" }
private_constant :BIND_BLOCK

def bind_block; BIND_BLOCK; end

private
# Oracle can't compare CLOB columns with standard SQL operators for comparison.
# We need to replace standard equality for text/binary columns to use DBMS_LOB.COMPARE function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
end

it "should be an OracleAdapter" do
@conn = ActiveRecord::Base.establish_connection(CONNECTION_PARAMS.merge(emulate_oracle_adapter: true))
@conn = ActiveRecord::Base.establish_connection(CONNECTION_PARAMS.merge(adapter: 'oracle'))
expect(ActiveRecord::Base.connection).not_to be_nil
expect(ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::OracleAdapter)).to be_truthy
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def fake_terminal(input)
describe "structure" do
let(:temp_file) { Tempfile.create(["oracle_enhanced", ".sql"]).path }
before do
ActiveRecord::Base.connection.schema_migration.create_table
ActiveRecord::Base.connection_pool.schema_migration.create_table
ActiveRecord::Base.connection.execute "INSERT INTO schema_migrations (version) VALUES ('20150101010000')"
end

Expand Down Expand Up @@ -109,7 +109,7 @@ def fake_terminal(input)

after do
File.unlink(temp_file)
ActiveRecord::Base.connection.schema_migration.drop_table
ActiveRecord::Base.connection_pool.schema_migration.drop_table
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
def standard_dump(options = {})
stream = StringIO.new
ActiveRecord::SchemaDumper.ignore_tables = options[:ignore_tables] || []
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection_pool, stream)
stream.string
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1227,8 +1227,7 @@ class << @conn

before do
@conn = ActiveRecord::Base.connection

ActiveRecord::Base.connection.schema_migration.create_table
ActiveRecord::Base.connection_pool.schema_migration.create_table
end

context "multi insert is supported" do
Expand Down Expand Up @@ -1256,7 +1255,7 @@ class << @conn
end

after do
ActiveRecord::Base.connection.schema_migration.drop_table
ActiveRecord::Base.connection_pool.schema_migration.drop_table
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ class ::TestPost < ActiveRecord::Base
let(:dump) { ActiveRecord::Base.connection.dump_schema_information }

before do
ActiveRecord::Base.connection.schema_migration.create_table
ActiveRecord::Base.connection_pool.schema_migration.create_table
versions.each do |i|
ActiveRecord::Base.connection.schema_migration.create_version(i)
ActiveRecord::Base.connection_pool.schema_migration.create_version(i)
end
end

Expand Down Expand Up @@ -376,7 +376,7 @@ class ::TestPost < ActiveRecord::Base
end

after do
ActiveRecord::Base.connection.schema_migration.drop_table
ActiveRecord::Base.connection_pool.schema_migration.drop_table
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,15 +354,15 @@ class ::TestPost < ActiveRecord::Base

it "should explain query" do
explain = TestPost.where(id: 1).explain
expect(explain).to include("Cost")
expect(explain).to include("INDEX UNIQUE SCAN")
expect(explain.inspect).to include("Cost")
expect(explain.inspect).to include("INDEX UNIQUE SCAN")
end

it "should explain query with binds" do
binds = [ActiveRecord::Relation::QueryAttribute.new("id", 1, ActiveRecord::Type::OracleEnhanced::Integer.new)]
explain = TestPost.where(id: binds).explain
expect(explain).to include("Cost")
expect(explain).to include("INDEX UNIQUE SCAN").or include("TABLE ACCESS FULL")
expect(explain.inspect).to include("Cost")
expect(explain.inspect).to include("INDEX UNIQUE SCAN").or include("TABLE ACCESS FULL")
end
end

Expand Down Expand Up @@ -768,13 +768,13 @@ class ::TestPost < ActiveRecord::Base
it "should explain considers hints" do
post = TestPost.optimizer_hints("FULL (\"TEST_POSTS\")")
post = post.where(id: 1)
expect(post.explain).to include("| TABLE ACCESS FULL| TEST_POSTS |")
expect(post.explain.inspect).to include("| TABLE ACCESS FULL| TEST_POSTS |")
end

it "should explain considers hints with /*+ */" do
post = TestPost.optimizer_hints("/*+ FULL (\"TEST_POSTS\") */")
post = post.where(id: 1)
expect(post.explain).to include("| TABLE ACCESS FULL| TEST_POSTS |")
expect(post.explain.inspect).to include("| TABLE ACCESS FULL| TEST_POSTS |")
end
end

Expand Down
Loading
Loading