Skip to content

🐛 Fixed URLs not being correctly transformed during insert operations#13618

Merged
kevinansfield merged 2 commits intoTryGhost:mainfrom
kevinansfield:fix-format-on-write-when-inserting
Oct 20, 2021
Merged

🐛 Fixed URLs not being correctly transformed during insert operations#13618
kevinansfield merged 2 commits intoTryGhost:mainfrom
kevinansfield:fix-format-on-write-when-inserting

Conversation

@kevinansfield
Copy link
Copy Markdown
Member

@kevinansfield kevinansfield commented Oct 18, 2021

refs https://github.com/TryGhost/Team/issues/1150

Our override of the base Bookshelf insert operation so that our own formatOnWrite() method is called on attributes was working on a false assumption that an attrs attribute is passed in as it is for the update operation. Instead Bookshelf's base update uses the model.attributes values to create an attrs object that is then passed through the usual .format() method meaning that our insert override was not actually doing anything.

  • added a failing regression test for the formatOnWrite() override behaviour
  • adjusted our insert/update overrides to set an internal _isWriting property on the model, then if that property is true our .format() override (which is called by Bookshelf on a generated attrs object during inserts) will manually call our .formatOnWrite() method
    • updated both overrides even though update was working for consistency and less cognitive overhead reasoning between two different approaches

refs TryGhost/Product#1150

Our override of the base Bookshelf `insert` operation so that our own `formatOnWrite()` method is called on attributes was working on a false assumption that an `attrs` attribute is passed in as it is for the `update` operation. Instead Bookshelf's base update uses the `model.attributes` values to create an `attrs` object that is then passed through the usual `.format()` method meaning that our `insert` override was not actually doing anything.

- added a failing regression test for the `formatOnWrite()` override behaviour
- adjusted our insert/update overrides to set an internal `_isWriting` property on the model, then if that property is true our `.format()` override (which is called by Bookshelf on a generated `attrs` object during inserts) we manually call our `.formatOnWrite()` method
  - updated both overrides even though `update` was working for consistency and less cognitive overhead for reasoning between two different approaches
const models = require('../../../../core/server/models');

describe('Models: Base plugins: Overrides', function () {
before(testUtils.teardownDb);
Copy link
Copy Markdown
Contributor

@naz naz Oct 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love that we have test coverage for more and more pieces of the model 🖤
It's totally outside the scope for this issue, just me thinking out loud 😅 Would be cool to not have extremely expensive teardown/setup cycles in unit tests. Something like what we have with the migration tests would be much more suitable imo. We don't measure this, but essentially each all test cases add ~300ms on average to run because of the hidden cost of db operations 🙀

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be clear, nothing to do with this right now! Just making a mental note to pay closer attention to the before/after clauses setting up db state :)

Co-authored-by: naz <nazargargol@gmail.com>
@kevinansfield kevinansfield merged commit 256f16a into TryGhost:main Oct 20, 2021
@kevinansfield kevinansfield deleted the fix-format-on-write-when-inserting branch October 20, 2021 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants