Merge pull request #18349 from jdelStrother/primarykeyless

Fix rollback of primarykey-less tables
This commit is contained in:
Sean Griffin 2015-01-05 14:19:01 -07:00
commit 08d6eb2370
2 changed files with 23 additions and 1 deletions

@ -378,7 +378,7 @@ def restore_transaction_record_state(force = false) #:nodoc:
thaw unless restore_state[:frozen?]
@new_record = restore_state[:new_record]
@destroyed = restore_state[:destroyed]
write_attribute(self.class.primary_key, restore_state[:id])
write_attribute(self.class.primary_key, restore_state[:id]) if self.class.primary_key
end
end
end

@ -650,6 +650,28 @@ def test_transactions_state_from_commit
assert transaction.state.committed?
end
def test_transaction_rollback_with_primarykeyless_tables
connection = ActiveRecord::Base.connection
connection.create_table(:transaction_without_primary_keys, force: true, id: false) do |t|
t.integer :thing_id
end
klass = Class.new(ActiveRecord::Base) do
self.table_name = 'transaction_without_primary_keys'
after_commit { } # necessary to trigger the has_transactional_callbacks branch
end
assert_no_difference(-> { klass.count }) do
ActiveRecord::Base.transaction do
klass.create!
raise ActiveRecord::Rollback
end
end
ensure
connection.execute("DROP TABLE IF EXISTS transaction_without_primary_keys")
end
private
%w(validation save destroy).each do |filter|