Skip to content
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: pass CLS transaction to model hooks #13927

Merged
merged 3 commits into from Jul 9, 2022
Merged

Conversation

heroic
Copy link
Contributor

@heroic heroic commented Jan 9, 2022

Pull Request Checklist

Please make sure to review and check all of these items:

  • Have you added new tests to prevent regressions?
  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description Of Change

Fixes: #12973

Todos

  • Figure out how and where to write a test case to prevent future regression.

@ephys
Copy link
Member

ephys commented Jan 9, 2022

Thank you for the PR :)
Could you add a test in (i think) test/integration/cls.test.js that checks the transaction is properly passed where it wasn't before?

@ephys ephys added the type: bug label Jan 9, 2022
@heroic
Copy link
Contributor Author

heroic commented Jan 9, 2022

Hi @ephys I tried! Couldn't figure out how to make it work though. I did test the change manually in our app though. Happy to give it a shot if you can guide.

@ephys ephys self-assigned this Jan 11, 2022
@ephys
Copy link
Member

ephys commented Jan 11, 2022

I'll try to take a stab at it next time I can

@ephys
Copy link
Member

ephys commented Jan 12, 2022

I'm looking into this more and it's weird that your fix is necessary.

Model#save calls either QueryInterface#insert or QueryInterface#update depending on Model#isNewRecord.
Both of these methods then call Sequelize#query, which retrieves the transaction from Sequelize._cls.

Edit: nevermind it's not about query it's about the hook. 🤦‍♀️
It's early.

@ephys
Copy link
Member

ephys commented Jan 12, 2022

I've added tests for this in branch feature/ephys/cls-in-hooks

Here they are: https://github.com/sequelize/sequelize/blob/feature/ephys/cls-in-hooks/test/integration/cls.test.js#L143
And the commit: 6294414

Feel free to copy them in your PR / to cherry pick the commit

It tests every Model hooks:
image

test/integration/cls.test.js Outdated Show resolved Hide resolved
@heroic
Copy link
Contributor Author

heroic commented Jan 13, 2022

@ephys Still fails. Do you know why?

@ephys
Copy link
Member

ephys commented Jan 13, 2022

Well passing the transaction to hooks has been implemented in the #save method but the other model methods aren't implementing it

image

@heroic
Copy link
Contributor Author

heroic commented Jan 17, 2022

Well passing the transaction to hooks has been implemented in the #save method but the other model methods aren't implementing it

image

So we'll need to pass the transaction in other functions too. Got it. Will update

@ephys ephys changed the title fix: sequelize should take transaction from CLS everywhere fix: pass CLS transaction to model hooks Jan 26, 2022
@WikiRik
Copy link
Member

WikiRik commented Jan 31, 2022

Hi @heroic, did you get a chance to look at this again? Or is there something we can do to help?

@heroic
Copy link
Contributor Author

heroic commented Jan 31, 2022

@WikiRik a colleague of mine is looking to get this closed! We should have this working soon

@AmirMelio
Copy link

@WikiRik a colleague of mine is looking to get this closed! We should have this working soon

Hi @heroic , any update?

@joker00777
Copy link
Contributor

joker00777 commented May 30, 2022

Hi @AmirMelio I will be continuing it from here.

Copy link
Contributor

@joker00777 joker00777 left a comment

Choose a reason for hiding this comment

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

Test file typo

@joker00777 joker00777 force-pushed the main branch 2 times, most recently from 7758520 to e1d4dc2 Compare June 2, 2022 05:03
Copy link
Contributor

@joker00777 joker00777 left a comment

Choose a reason for hiding this comment

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

any idea why these are failing?

lib/model.js Outdated Show resolved Hide resolved
test/integration/cls.test.js Outdated Show resolved Hide resolved
test/integration/cls.test.js Outdated Show resolved Hide resolved
Copy link
Contributor

@joker00777 joker00777 left a comment

Choose a reason for hiding this comment

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

Sorry for the late response : (

test/integration/cls.test.js Outdated Show resolved Hide resolved
test/integration/cls.test.js Outdated Show resolved Hide resolved
@WikiRik
Copy link
Member

WikiRik commented Jun 13, 2022

GitHub has issues with the caching today, I'll rerun the checks after they have solved that incident.

src/model-internals.ts Outdated Show resolved Hide resolved
@joker00777
Copy link
Contributor

@ephys any updates?

@joker00777
Copy link
Contributor

@ephys waiting for your feedback : )

@ephys ephys self-requested a review June 21, 2022 05:31
@joker00777
Copy link
Contributor

@ephys can we merge it now : ) ?

@WikiRik WikiRik requested a review from ephys June 24, 2022 10:49
Copy link
Member

@ephys ephys left a comment

Choose a reason for hiding this comment

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

There are a few unresolved comments from the previous review :)

test/integration/cls.test.js Outdated Show resolved Hide resolved
@joker00777 joker00777 force-pushed the main branch 4 times, most recently from e8d2346 to aeecb25 Compare July 6, 2022 14:28
@joker00777
Copy link
Contributor

Hey @ephys can you take a look now?

@ephys ephys self-requested a review July 8, 2022 13:41
@ephys
Copy link
Member

ephys commented Jul 8, 2022

This is great :) There are two remaining comments and then this is ready to merge:

Could you enable the option to allow maintainers to add commits on your branch once you are ready? We use it to update the branch before merging.

@heroic
Copy link
Contributor Author

heroic commented Jul 8, 2022

Hi @ephys, since the fork is owned by an org, there isn't an option to enable Allow edits by maintainers. We can add the maintainers write access on the repo by adding them individually. Would that work?

@ephys
Copy link
Member

ephys commented Jul 9, 2022

I think we can try coordinating. If you can click the "update branch" button then @ me and/or @WikiRik, I'll merge this immediately before another PR

@joker00777
Copy link
Contributor

@ephys @WikiRik update branch Done : )

@WikiRik
Copy link
Member

WikiRik commented Jul 9, 2022

@joker00777 we accidentally merged something else just now, could you update the branch once again?

@ephys
Copy link
Member

ephys commented Jul 9, 2022

Sorry 😅

@joker00777
Copy link
Contributor

haha done @WikiRik @ephys ; )

@WikiRik
Copy link
Member

WikiRik commented Jul 9, 2022

Thanks! After the tests pass, I'll merge this

@joker00777
Copy link
Contributor

Thank You : )

@WikiRik WikiRik merged commit be98856 into sequelize:main Jul 9, 2022
@WikiRik
Copy link
Member

WikiRik commented Jul 9, 2022

There we go! Thanks again for working on this @joker00777 @heroic

@github-actions
Copy link
Contributor

github-actions bot commented Aug 7, 2022

🎉 This PR is included in version 7.0.0-alpha.15 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

hooks do not get CLS transaction
5 participants