Skip to content

Error in PATCH does not rollback changes #279

@thinkerbot

Description

@thinkerbot

I'm trying to upgrade to 0.4.2 from 0.3.1 and found that an error in a PATCH request will no longer rollback changes to the database made during the update. Something is going on that makes it so the request is not wrapped in a transaction. I'm not sure if other methods are affected as well.

To reproduce I forked peeps and added a bit of code make a change to the database and then raise an error. PhoneNumberResource looks like this afterwards (the code is here).

class PhoneNumberResource < JSONAPI::Resource
  attributes :name, :phone_number
  has_one :contact

  filter :contact

  def name=(value)
    if value == "boom"
      model.name = "change to database that isn't rolled back"
      model.save
      raise "some error"
    end

    model.name = value
  end
end

With this code make a phone number:

curl -i -H "Accept: application/vnd.api+json" -H 'Content-Type:application/vnd.api+json' -X POST -d '{ "data": { "type": "phone-numbers", "relationships": { "contact": { "data": { "type": "contacts", "id": "1" } } }, "attributes": { "name": "home", "phone-number": "(603) 555-1212" } } }' http://localhost:3000/phone-numbers
# ...
# {"data":{"id":"2","type":"phone-numbers","links":{"self":"http://localhost:3000/phone-numbers/2"},"attributes":{"name":"home","phone-number":"(603) 555-1212"},"relationships":{"contact":{"links":{"self":"http://localhost:3000/phone-numbers/2/relationships/contact","related":"http://localhost:3000/phone-numbers/2/contact"},"data":{"type":"contacts","id":"1"}}}}}

Then cause the error:

curl -i -H "Accept: application/vnd.api+json" -H 'Content-Type:application/vnd.api+json' -X PATCH -d '{ "data": { "type": "phone-numbers", "id": "2", "attributes": { "name": "boom" } } }' http://localhost:3000/phone-numbers/2
# HTTP/1.1 500 Internal Server Error
# ...

Then see that the change prior to the error (setting name = "change to database that isn't rolled back") was committed and not rolled back:

curl http://localhost:3000/phone-numbers/2
# {"data":{"id":"2","type":"phone-numbers","links":{"self":"http://localhost:3000/phone-numbers/2"},"attributes":{"name":"change to database that isn't rolled back","phone-number":"(603) 555-1212"},"relationships":{"contact":{"links":{"self":"http://localhost:3000/phone-numbers/2/relationships/contact","related":"http://localhost:3000/phone-numbers/2/contact"},"data":{"type":"contacts","id":"1"}}}}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions