Skip to content

Commit

Permalink
Clean up uniques when deleting
Browse files Browse the repository at this point in the history
A bug in the test where the key "unique" was used 
instead of "uniques" meant the test 
"removes the previous index when deleting" was
passing when it was in fact failing. Model.delete 
now mirrors Model.save in how it cleans up indexes
and uniques.
  • Loading branch information
damonmorgan committed Apr 11, 2013
1 parent 58fe564 commit e1473c2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 6 additions & 3 deletions lib/ohm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,8 @@ def delete
transaction do |t|
_uniques = nil
_indices = nil
existing = nil
existing_indices = nil
existing_uniques = nil

t.watch(*_unique_keys)

Expand All @@ -1354,15 +1355,17 @@ def delete
t.watch(key[:_uniques]) if model.uniques.any?

t.read do
existing = _read_attributes(model.indices) if model.indices.any?
existing_indices = _read_attributes(model.indices) if model.indices.any?
existing_uniques = _read_attributes(model.uniques) if model.uniques.any?
_uniques = db.hgetall(key[:_uniques])
_indices = db.smembers(key[:_indices])
end

t.write do
_delete_uniques(_uniques)
_delete_indices(_indices)
_delete_existing_indices(existing)
_delete_existing_uniques(existing_uniques)
_delete_existing_indices(existing_indices)
model.collections.each { |e| db.del(key[e]) }
db.srem(model.key[:all], id)
db.del(key[:counters])
Expand Down
4 changes: 2 additions & 2 deletions test/uniques.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ def provider
u.update(:email => "d@d.com")

assert_equal nil, User.with(:email, "c@c.com")
assert_equal nil, User.key[:unique][:email].hget("c@c.com")
assert_equal nil, User.key[:uniques][:email].hget("c@c.com")
assert_equal u, User.with(:email, "d@d.com")
end

test "removes the previous index when deleting" do |u|
u.delete

assert_equal nil, User.with(:email, "a@a.com")
assert_equal nil, User.key[:unique][:email].hget("a@a.com")
assert_equal nil, User.key[:uniques][:email].hget("a@a.com")
end

test "unique virtual attribute" do
Expand Down

0 comments on commit e1473c2

Please sign in to comment.