Skip to content

Commit

Permalink
Merge pull request #496 from codeodor/ar6
Browse files Browse the repository at this point in the history
Get all tests passing on MySQL and PostgreSQL on Rails 6.0.0
  • Loading branch information
Charlie Savage committed Sep 4, 2019
2 parents 460807d + 995a7a5 commit a38fede
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.2
2.5.2
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
source 'https://rubygems.org'

gem 'activerecord', '~> 6.0.0.rc1'

gem 'activerecord', '~> 6.0.0'

gem 'rake'
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]

Expand Down
2 changes: 1 addition & 1 deletion composite_primary_keys.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Gem::Specification.new do |s|
# Dependencies
s.required_ruby_version = '>= 2.2.2'

s.add_dependency('activerecord', '~> 6.0.0.rc1')
s.add_dependency('activerecord', '~> 6.0.0')

s.add_development_dependency('rake')
s.add_development_dependency('mysql2')
Expand Down
2 changes: 1 addition & 1 deletion lib/composite_primary_keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

unless defined?(ActiveRecord)
require 'rubygems'
gem 'activerecord', '6.0.0.rc1'
gem 'activerecord', '6.0.0'
require 'active_record'
end

Expand Down
26 changes: 22 additions & 4 deletions lib/composite_primary_keys/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ def update_all(updates)
# CPK
if @klass.composite?
stmt.table(table)
subselect = arel.clone

arel_attributes = primary_keys.map do |key|
arel_attribute(key)
end.to_composite_keys
subselect.projections = arel_attributes

subselect = subquery_for(arel_attributes, arel)

stmt.wheres = [arel_attributes.in(subselect)]
else
stmt.table(arel.join_sources.empty? ? table : arel.source)
Expand Down Expand Up @@ -76,11 +78,13 @@ def delete_all
# CPK
if @klass.composite?
stmt.from(table)
subselect = arel.clone

arel_attributes = primary_keys.map do |key|
arel_attribute(key)
end.to_composite_keys
subselect.projections = arel_attributes

subselect = subquery_for(arel_attributes, arel)

stmt.wheres = [arel_attributes.in(subselect)]
else
stmt.from(arel.join_sources.empty? ? table : arel.source)
Expand All @@ -96,5 +100,19 @@ def delete_all
reset
affected
end

# CPK
def subquery_for(key, select)
subselect = select.clone
subselect.projections = key

# Materialize subquery by adding distinct
# to work with MySQL 5.7.6 which sets optimizer_switch='derived_merge=on'
subselect.distinct unless select.limit || select.offset || select.orders.any?

key_name = Array(key).map {|a_key| a_key.name }.join(',')

Arel::SelectManager.new(subselect.as("__active_record_temp")).project(Arel.sql(key_name))
end
end
end
5 changes: 5 additions & 0 deletions lib/composite_primary_keys/relation/calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ def execute_simple_calculation(operation, column_name, distinct) #:nodoc:
def build_count_subquery(relation, column_name, distinct)
if column_name == :all
relation.select_values = [ Arel.sql(::ActiveRecord::FinderMethods::ONE_AS_ONE) ] unless distinct
if relation.select_values.first.is_a?(Array)
relation.select_values = relation.select_values.first.map do |column|
Arel::Attribute.new(@klass.unscoped.table, column)
end
end
elsif column_name.is_a?(Array)
relation.select_values = column_name.map do |column|
Arel::Attribute.new(@klass.unscoped.table, column)
Expand Down
4 changes: 2 additions & 2 deletions lib/composite_primary_keys/relation/finder_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module CompositePrimaryKeys
module ActiveRecord
module FinderMethods
def apply_join_dependency(eager_loading: group_values.empty?)
join_dependency = construct_join_dependency(eager_load_values + includes_values)
join_dependency = construct_join_dependency(eager_load_values + includes_values, Arel::Nodes::OuterJoin)
relation = except(:includes, :eager_load, :preload).joins!(join_dependency)

if eager_loading && !using_limitable_reflections?(join_dependency.reflections)
Expand Down Expand Up @@ -51,7 +51,7 @@ def construct_relation_for_exists(conditions)
conditions = sanitize_forbidden_attributes(conditions)

if distinct_value && offset_value
relation = limit(1)
relation = except(:order).limit!(1)
else
relation = except(:select, :distinct, :order)._select!(::ActiveRecord::FinderMethods::ONE_AS_ONE).limit!(1)
end
Expand Down

0 comments on commit a38fede

Please sign in to comment.