Permalink
Browse files

Don't wrap operations on collection associations in transactions when…

… they are not needed, so the connection adapter does not send empty BEGIN COMMIT transactions blocks to the database.
  • Loading branch information...
1 parent c1938d1 commit b17fd25a2d554a9b126fc185dc7609f7846ee267 @benedikt benedikt committed with jonleighton Jun 9, 2011
@@ -117,7 +117,7 @@ def concat(*records)
result = true
load_target if owner.new_record?
- transaction do
+ block = lambda do
records.flatten.each do |record|
raise_on_type_mismatch(record)
add_to_target(record) do |r|
@@ -126,6 +126,8 @@ def concat(*records)
end
end
+ owner.new_record? ? block.call : transaction(&block)
+
result && records
end
@@ -295,7 +297,7 @@ def replace(other_array)
other_array.each { |val| raise_on_type_mismatch(val) }
original_target = load_target.dup
- transaction do
+ block = lambda do
delete(target - other_array)
unless concat(other_array - target)
@@ -304,6 +306,8 @@ def replace(other_array)
"new records could not be saved."
end
end
+
+ owner.new_record? ? block.call : transaction(&block)
end
def include?(record)
@@ -444,14 +448,16 @@ def delete_or_destroy(records, method)
records.each { |record| raise_on_type_mismatch(record) }
existing_records = records.reject { |r| r.new_record? }
- transaction do
+ block = lambda do
records.each { |record| callback(:before_remove, record) }
delete_records(existing_records, method) if existing_records.any?
records.each { |record| target.delete(record) }
records.each { |record| callback(:after_remove, record) }
end
+
+ existing_records.any? ? transaction(&block) : block.call
end
# Delete the given records from the association, using one of the methods :destroy,
@@ -537,6 +537,18 @@ def test_adding_a_collection
assert_equal 3, companies(:first_firm).clients_of_firm(true).size
end
+ def test_transactions_when_adding_to_persisted
+ force_signal37_to_load_all_clients_of_firm
+ Client.expects(:transaction)
+ companies(:first_firm).clients_of_firm.concat(Client.new("name" => "Natural Company"))
+ end
+
+ def test_transactions_when_adding_to_new_record
+ Client.expects(:transaction).never
+ firm = Firm.new
+ firm.clients_of_firm.concat(Client.new("name" => "Natural Company"))
+ end
+
def test_new_aliased_to_build
company = companies(:first_firm)
new_client = assert_no_queries { company.clients_of_firm.new("name" => "Another Client") }
@@ -778,6 +790,21 @@ def test_delete_all_with_not_yet_loaded_association_collection
assert_equal 0, companies(:first_firm).clients_of_firm(true).size
end
+ def test_transaction_when_deleting_persisted
+ force_signal37_to_load_all_clients_of_firm
+ client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
+ Client.expects(:transaction)
+ companies(:first_firm).clients_of_firm.delete(client)
+ end
+
+ def test_transaction_when_deleting_new_record
+ client = Client.new("name" => "New Client")
+ firm = Firm.new
+ firm.clients_of_firm << client
+ Client.expects(:transaction).never
+ firm.clients_of_firm.delete(client)
+ end
+
def test_clearing_an_association_collection
firm = companies(:first_firm)
client_id = firm.clients_of_firm.first.id
@@ -1111,6 +1138,24 @@ def test_replace_failure
assert_equal orig_accounts, firm.accounts
end
+ def test_transactions_when_replacing_on_persisted
+ firm = Firm.find(:first, :order => "id")
+ firm.clients = [companies(:first_client)]
+ assert firm.save, "Could not save firm"
+ firm.reload
+
+ Client.expects(:transaction)
+ firm.clients_of_firm = [Client.new("name" => "Natural Company")]
+ end
+
+ def test_transactions_when_replacing_on_new_record
+ firm = Firm.new
+ firm.clients_of_firm << Client.new("name" => "Natural Company")
+
+ Client.expects(:transaction).never
+ firm.clients_of_firm = [Client.new("name" => "New Client")]
+ end
+
def test_get_ids
assert_equal [companies(:first_client).id, companies(:second_client).id], companies(:first_firm).client_ids
end

0 comments on commit b17fd25

Please sign in to comment.