Skip to content
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

WIP Rails 7 2 support #2424

Open
wants to merge 6 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
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
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,23 @@ module ContextIndexClassMethods
# Add context index condition.
def contains(column, query, options = {})
score_label = options[:label].to_i || 1
where("CONTAINS(#{connection.quote_table_name(column)}, ?, #{score_label}) > 0", query).
order(Arel.sql("SCORE(#{score_label}) DESC"))
quoted_column = connection.quote_table_name(column)

# Create an Arel node for the CONTAINS function
contains_node = Arel::Nodes::NamedFunction.new(
'CONTAINS',
[
Arel::Nodes::SqlLiteral.new(quoted_column),
Arel::Nodes::BindParam.new(query),
Arel::Nodes::SqlLiteral.new(score_label.to_s)
]
)

# Create comparison node: CONTAINS(...) > 0
condition = Arel::Nodes::GreaterThan.new(contains_node, Arel::Nodes::SqlLiteral.new('0'))

# Create the where clause and order by score
where(condition).order(Arel.sql("SCORE(#{score_label}) DESC"))
end
end
end
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
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
11 changes: 9 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,16 @@
require "active_record/log_subscriber"

require "logger"
require "ruby-plsql"

require "active_record/connection_adapters/oracle_enhanced_adapter"
require "ruby-plsql"
if ActiveRecord::ConnectionAdapters.respond_to?(:register)
ActiveRecord::ConnectionAdapters.register(
"oracle_enhanced",
"ActiveRecord::ConnectionAdapters::OracleEnhancedAdapter",
"active_record/connection_adapters/oracle_enhanced_adapter"
)
end

puts "==> Effective ActiveRecord version #{ActiveRecord::VERSION::STRING}"

Expand Down Expand Up @@ -133,7 +140,7 @@ def dump_table_schema(table, connection = ActiveRecord::Base.connection)
old_ignore_tables = ActiveRecord::SchemaDumper.ignore_tables
ActiveRecord::SchemaDumper.ignore_tables = connection.data_sources - [table]
stream = StringIO.new
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection_pool, stream)
stream.string
ensure
ActiveRecord::SchemaDumper.ignore_tables = old_ignore_tables
Expand Down
Loading