New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix inconsistent previous attributes #1848
Conversation
- This ensures that previousAttributes are kept after a save, so that a user can know what a particular attribute was like before the save.
@ricardograca Sorry for delay - i was on vacation 😎Will look at it asap. |
Is this line still needed? 🤔 Because we no longer reset the previous attributes in the |
Good question. Probably not. Definitive answer in a few minutes. |
@kirrg001 Doesn't seem to be needed. On top of that I'll wait for further reviews before changing this, so I can do it all in one go. |
If i fetch relations with |
That doesn't seem right. I also just found one other somewhat related test that would pass in such a situation when it should fail. There's also no test for that particular scenario you described. I'll have to fix that. |
- The previous attributes won't be reset after save anymore.
- This test would pass if the previous attributes were an empty object, which isn't correct.
@kirrg001 All reported issues fixed, and I added some tests to check for them. |
@ricardograca Great to hear - i'll look at them asap :) |
This is resolved 👍 But when i install this PR as tarball, i am having a different problem. Maybe unrelated to this PR.
This event no longer passes the First argument: is correct, the collection |
That hasn't changed AFAIK. Looking at the relevant code, the |
@ricardograca If you have a guess, let me know. I've also debugged a little, suddenly the |
I used My guess is that it's something in the |
- This is similar to the change introduced recently for single models, but this is for models in relations.
@kirrg001 Done. |
Let's say i fetch a user and it returns
Now i call I am listening on the user updated event and i receive:
Is that expected? I am wondering why the previous attributes object is empty in the updated event 🤔 I think as soon as you save a model without fields being modified, i am receiving I think the behaviour was different before? |
It was different before but only because previously However I think you're correct and it does seem like if the |
I just checked the use case you mentioned and it's working as expected. How are you getting that behavior of empty |
I've added a few more tests to ensure the behavior you mention is observed: 3cdfa6e |
I am on the correct commit. I just double checked. Steps:
|
As you can see the test case is passing, and it seems to do exactly what you're saying. Maybe you have some plugin interfering with this, or some other event listener, or something else. I'm writing a simple stand-alone test case that you can use to try to reproduce the behavior, but at this point I think Bookshelf itself is working fine. Oh yeah, I remember you had a custom property in Ghost models for storing the proper previous attributes, before this change. Could it be related to that? |
Here is the gist: https://gist.github.com/ricardograca/da68dc091fcbe15c395558c166fd7200 |
I have commented out all the logic for that.
Thanks will test asap!! |
The gist works for me too. It must be something else. I am looking into it now. |
I figured it out. That was quick 🤣 https://gist.github.com/kirrg001/b8650a544db01541828d50fbcd1a227d Bookshelf returns an empty |
Nice work! I'll fix it this weekend hopefully. Apart from that are there any other issues? |
@kirrg001 Fixed. Nice catch there 🥇 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💥🔥✨
Thanks so much!
refs TryGhost#9248 - https://github.com/bookshelf/bookshelf/releases/tag/0.14.0 - Bookshelf has fixed it's previous attr handling, see bookshelf/bookshelf#1848
refs #9389, refs #9248 - https://github.com/bookshelf/bookshelf/releases/tag/0.14.0 - Bookshelf has fixed it's previous attr handling, see bookshelf/bookshelf#1848 - SQlite3 double slashes was merged into knex and released 👻tgriesser/knex@c746dea
Introduction
This makes
previousAttributes()
andprevious()
behave like users expect them to, and always return the actual previous attributes.Also updates the documentation to make it clearer.
Motivation
The current behavior causes some confusion, which isn't helped by the documentation not being very clear about it, and it seems to be a bit different from what other ORMs do. It's also not very useful to have the previous attributes be reset back to match the current attributes on each
save()
ordestroy()
call. This makes these methods far less useful since they can only be used to check for the previous attributes before the model is saved or destroyed.Proposed solution
This changes the
previous()
andpreviousAttributes()
methods so that their values are not reset back to the current attributes when saving or destroying a model. This means that the previous attributes are accessible after any of these operations, making them way more useful.This is a breaking change, but currently it's a bit useless having these methods return the same values that are already set on the model after a
save()
, so I doubt it will cause problems for anyone.Fixes #1520.
Alternatives considered
Implementing additional methods to get the previous attributes before a
save()
, but that would require adding more properties to models, more methods, and more chance for confusion. It would mean this change wouldn't break backwards compatibility though.