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

Ignore orders for delete_all/update_all queries. #621

Closed
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: 0 additions & 2 deletions lib/composite_primary_keys/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def update_all(updates)

stmt.take(arel.limit)
stmt.offset(arel.offset)
stmt.order(*arel.orders)

if updates.is_a?(Hash)
if klass.locking_enabled? &&
Expand Down Expand Up @@ -81,7 +80,6 @@ def delete_all

stmt.take(arel.limit)
stmt.offset(arel.offset)
stmt.order(*arel.orders)

affected = klass.connection.delete(stmt, "#{klass} Destroy")

Expand Down
7 changes: 4 additions & 3 deletions test/fixtures/tariff.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Tariff < ActiveRecord::Base
self.primary_keys = [:tariff_id, :start_date]
has_many :product_tariffs, :foreign_key => [:tariff_id, :tariff_start_date], :dependent => :delete_all
has_many :products, :through => :product_tariffs, :foreign_key => [:tariff_id, :tariff_start_date]
self.primary_keys = [:tariff_id, :start_date]
has_many :product_tariffs, :foreign_key => [:tariff_id, :tariff_start_date], :dependent => :delete_all
has_many :products, :through => :product_tariffs, :foreign_key => [:tariff_id, :tariff_start_date]
default_scope { order(created_at: :asc) }
end
2 changes: 1 addition & 1 deletion test/test_calculations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def test_count
expected = {Date.today => 2,
Date.today.next => 1}

assert_equal(expected, Tariff.group(:start_date).count)
assert_equal(expected, Tariff.unscoped.group(:start_date).count)
end

def test_count_distinct
Expand Down
7 changes: 7 additions & 0 deletions test/test_delete.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ def test_delete_all
assert_equal(5, deleted)
end

def test_delete_all_default_order
refute_empty(Tariff.all)
deleted = Tariff.delete_all
assert_empty(Tariff.all)
assert_equal(3, deleted)
end

def test_delete_all_with_join
tested_delete_all = false
Arel::Table.engine = nil # should not rely on the global Arel::Table.engine
Expand Down
10 changes: 9 additions & 1 deletion test/test_update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ def test_update_all
end
end

def test_update_all_default_order
Tariff.update_all(amount: 1337)

Tariff.all.each do |reference_code|
assert_equal(1337, reference_code.amount)
end
end

def test_update_all_join
tested_update_all = false
Arel::Table.engine = nil # should not rely on the global Arel::Table.engine
Expand Down Expand Up @@ -100,4 +108,4 @@ def test_update_with_uniqueness
assignment.save!
assert_equal(room_2, assignment.room)
end
end
end