Skip to content

Support Rails 8.0 #2425

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 14 commits into
base: master
Choose a base branch
from
6 changes: 2 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ jobs:
strategy:
fail-fast: false
matrix:
# Rails 7.0 requires Ruby 2.7 or higeher.
# Rails 8.0 requires Ruby 3.2 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
6 changes: 2 additions & 4 deletions .github/workflows/test_11g.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,15 @@ jobs:
strategy:
fail-fast: false
matrix:
# Rails 7.0 requires Ruby 2.7 or higeher.
# Rails 8.0 requires Ruby 3.2 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: "8-0-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", ["~> 8.0.0"])
s.add_runtime_dependency("ruby-plsql", [">= 0.6.0"])
if /java/.match?(RUBY_PLATFORM)
s.platform = Gem::Platform.new("java")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,58 +8,80 @@ module DatabaseStatements
#
# see: abstract/database_statements.rb

# Executes a SQL statement
def execute(sql, name = nil, async: false, allow_retry: false)
sql = transform_query(sql)
READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(
:close, :declare, :fetch, :move, :set, :show
) # :nodoc:
private_constant :READ_QUERY

def write_query?(sql) # :nodoc:
!READ_QUERY.match?(sql)
rescue ArgumentError # Invalid encoding
!READ_QUERY.match?(sql.b)
end

log(sql, name, async: async) { _connection.exec(sql, allow_retry: allow_retry) }
# Executes a SQL statement
def execute(...)
super
end

def exec_query(sql, name = "SQL", binds = [], prepare: false, async: false, allow_retry: false)
sql = transform_query(sql)
# Low level execution of a SQL statement on the connection returning adapter specific result object.
def raw_execute(sql, name = "SQL", binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: false)
sql = preprocess_query(sql)

type_casted_binds = type_casted_binds(binds)
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
log(sql, name, binds, type_casted_binds, async: async) do
cursor = nil
cached = false
with_retry do
if binds.nil? || binds.empty?
cursor = conn.prepare(sql)
else
unless @statements.key? sql
@statements[sql] = conn.prepare(sql)
end

log(sql, name, binds, type_casted_binds, async: async) do
cursor = nil
cached = false
with_retry do
if without_prepared_statement?(binds)
cursor = _connection.prepare(sql)
else
unless @statements.key? sql
@statements[sql] = _connection.prepare(sql)
end

cursor = @statements[sql]

cursor.bind_params(type_casted_binds)
cursor = @statements[sql]
cursor.bind_params(type_casted_binds)

cached = true
cached = true
end
cursor.exec
end

cursor.exec
end

if (name == "EXPLAIN") && sql.start_with?("EXPLAIN")
res = true
else
columns = cursor.get_col_names.map do |col_name|
oracle_downcase(col_name)
end

rows = []
fetch_options = { get_lob_value: (name != "Writable Large Object") }
while row = cursor.fetch(fetch_options)
rows << row
if sql =~ /\A\s*SELECT/i # This seems a naive way to detect queries that will have row results.
fetch_options = { get_lob_value: (name != "Writable Large Object") }
while row = cursor.fetch(fetch_options)
rows << row
end
end
res = build_result(columns: columns, rows: rows)

affected_rows_count = cursor.row_count


cursor.close unless cached

{ columns: columns, rows: rows, affected_rows_count: affected_rows_count }
end
end
end

cursor.close unless cached
res
def cast_result(result)
if result.nil?
ActiveRecord::Result.empty
else
ActiveRecord::Result.new(result[:columns], result[:rows])
end
end
alias_method :internal_exec_query, :exec_query

def affected_rows(result)
result[:affected_rows_count]
end

def supports_explain?
true
Expand Down Expand Up @@ -106,7 +128,7 @@ def exec_insert(sql, name = nil, binds = [], pk = nil, sequence_name = nil, retu
cursor = nil
returning_id_col = returning_id_index = nil
with_retry do
if without_prepared_statement?(binds)
if binds.nil? || binds.empty?
cursor = _connection.prepare(sql)
else
unless @statements.key?(sql)
Expand Down Expand Up @@ -146,7 +168,7 @@ def exec_update(sql, name = nil, binds = [])
log(sql, name, binds, type_casted_binds) do
with_retry do
cached = false
if without_prepared_statement?(binds)
if binds.nil? || binds.empty?
cursor = _connection.prepare(sql)
else
if @statements.key?(sql)
Expand Down Expand Up @@ -289,6 +311,15 @@ def with_retry
raise
end
end

def handle_warnings(sql)
@notice_receiver_sql_warnings.each do |warning|
next if warning_ignored?(warning)

warning.sql = sql
ActiveRecord.db_warnings_action.call(warning)
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def dbms_output_enabled?

private
def log(sql, name = "SQL", binds = [], type_casted_binds = [], statement_name = nil, async: false, &block)
@instrumenter.instrument(
instrumenter.instrument(
"sql.active_record",
sql: sql,
name: name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,10 @@ def column_names
end
alias :get_col_names :column_names

def row_count
@raw_statement.getUpdateCount
end

def fetch(options = {})
if @raw_result_set.next
get_lob_value = options[:get_lob_value]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ def get_col_names
@raw_cursor.get_col_names
end

def row_count
@raw_cursor.row_count
end

def fetch(options = {})
if row = @raw_cursor.fetch
get_lob_value = options[:get_lob_value]
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
Loading
Loading