Datamapper paranoid delete quirks

Posted April 15, 2010

Recently discovered some quirks while working with Datamapper (ver 0.10.2). If you want to fetch deleted objects you need to use the syntax:

Model.with_deleted { Model.all }

Which by the way will only find deleted objects, if you want all the objects your going to have to do something like

Model.with_deleted { Model.all } | Model.all

The next quirky bit is if you want to undelete things you can't simply do:

m = Model.with_deleted { Model.get(1) }
m.update( :deleted_at => nil )

You actually have to do:

m = Model.with_deleted { Model.get(1) }
m.deleted_at
m.update( :deleted_at => nil )

Due to the lazy loading that Datamapper does it actually doesn't load the :deleted_at property until you have accessed and apparently won't save changes to it either.

Full example:

class Test
  include DataMapper::Resource
  
  property :id, Serial
  property :deleted_at, ParanoidDateTime
end

t = Test.new
t.save                          #~ (0.354887) INSERT INTO "tests" DEFAULT VALUES
t = Test.new
t.save                          #~ (0.089852) INSERT INTO "tests" DEFAULT VALUES
t = Test.first                  #~ (0.000191) SELECT "id" FROM "tests" WHERE "deleted_at" IS NULL ORDER BY "id" LIMIT 1
t.destroy                       #~ (0.002124) UPDATE "tests" SET "deleted_at" = '2010-04-15T17:02:14-07:00' WHERE "id" = 1
t = Test.with_deleted { Test.first }  #~ (0.000092) SELECT "id" FROM "tests" WHERE NOT("deleted_at" IS NULL) ORDER BY "id" LIMIT 1
t.deleted_at = nil
t.save                          # true
t = Test.with_deleted { Test.first }  #~ (0.000094) SELECT "id" FROM "tests" WHERE NOT("deleted_at" IS NULL) ORDER BY "id" LIMIT 1
t.deleted_at                    #~ (0.000429) SELECT "id", "deleted_at" FROM "tests" WHERE "id" = 1 ORDER BY "id"

Comments

This bug is now fixed in datamapper ~> 1.0.0. Here is the fix if you are curious: http://datamapper.lighthouseapp.com/projects/20609/tickets/883-paranoiddatetime-doesnt-respect-resourcewith_deleted

Displaying 1 comment

Add comment

Music

Splitting the Atomby Massive AttackPlayed on 2012/02/22 at 12:10AM
Babelby Massive AttackPlayed on 2012/02/22 at 12:04AM
In The Privacy Of Your Loveby Hot ChipPlayed on 2012/02/22 at 12:01AM
Dreams (Feat. Steve Nicks)by Deep DishPlayed on 2012/02/21 at 11:57PM
Kill Everybodyby SkrillexPlayed on 2012/02/21 at 11:52PM