-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
feat(transactions): Add afterCommit hooks for transactions #9287
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
feat(transactions): Add afterCommit hooks for transactions #9287
Conversation
sushantdhiman
left a comment
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.
Some changes, otherwise looks like a good addition
lib/transaction.js
Outdated
| constructor(sequelize, options) { | ||
| this.sequelize = sequelize; | ||
| this.savepoints = []; | ||
| this.afterCommitHooks = []; |
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.
_afterCommitHooks
lib/transaction.js
Outdated
| }); | ||
| }).tap( | ||
| () => this.sequelize.Promise.each( | ||
| this.afterCommitHooks, |
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.
Use const Promise = require('./promise');
| * @name afterCommit | ||
| * @memberOf Sequelize.Transaction | ||
| */ | ||
| afterCommit(fn) { |
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.
assert for fn being a function + test
test/integration/transaction.test.js
Outdated
| }); | ||
|
|
||
| it('supports running hooks when a transaction is commited', function() { | ||
| const self = this; |
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.
No self = this
test/integration/transaction.test.js
Outdated
| it('supports running hooks when a transaction is commited', function() { | ||
| const self = this; | ||
| const hook = sinon.spy(); | ||
| let t; |
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.
let transaction;
test/integration/transaction.test.js
Outdated
| const self = this; | ||
| const hook = sinon.spy(); | ||
| let t; | ||
| return expect(this.sequelize.transaction(transaction => { |
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.
Call this t
test/integration/transaction.test.js
Outdated
| return expect(this.sequelize.transaction(transaction => { | ||
| t = transaction; | ||
| transaction.afterCommit(hook); | ||
| return self.sequelize.query('SELECT 1+1', {transaction, raw: true}); |
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.
No need to pass { raw: true }, its not required. Instead pass type: QueryType.SELECT
|
@sushantdhiman thanks for the feedback! Just updated - let me know if you see anything else |
7b43f41 to
e6bdc82
Compare
|
Actually just updated now - my push failed yesterday |
|
Linting errors /home/travis/build/sequelize/sequelize/lib/transaction.js
200:23 error Strings must use singlequote quotes
/home/travis/build/sequelize/sequelize/test/integration/transaction.test.js
274:39 error Strings must use singlequote quotes
295:39 error Strings must use singlequote quotes
316:39 error Strings must use singlequote quotes
✖ 4 problems (4 errors, 0 warnings)
4 errors, 0 warnings potentially fixable with the `--fix` option. |
e6bdc82 to
8f6aa84
Compare
sushantdhiman
left a comment
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.
LGTM, just remove self = this approach from extra tests you have added
test/integration/transaction.test.js
Outdated
| }); | ||
|
|
||
| it('should run hooks if a non-auto callback transaction is committed', function() { | ||
| const self = this; |
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.
This test is still using self, next one too
eseliger
left a comment
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.
other than the self in the tests, as mentioned by @sushantdhiman , looks good
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.
Please add documentation covering the fact that you cannot modify the return value of a transaction inside afterCommit (most other hooks allow modification)
8f6aa84 to
7de7551
Compare
|
@sushantdhiman fixed, sorry for missing that again @mickhansen added |
|
There's still lint issues😕 |
Add `afterCommit` hooks for transactions that allows deferring work to after a transaction has been committed, regardless of if you have access to the promise chain that created the transaction Fixes: sequelize#2805
7de7551 to
ca3025a
Compare
|
@eseliger 🤦♂️ whoops. Fixed now |
|
+1 |
|
I think there is a bug in this feature: if transaction is a child (transaction.parent is set), then afterCommit hooks should not be invoked right after this transaction committed, because parent transaction is not committed yet, and is not guaranteed to be committed at all. The easiest way to fix this is change the way how hooks are added: |
|
could you guys port this back to v4? Pretty please? I am basically going to have to monkeypatch support for it into my codebase... |
|
Seconded for asking for a backport to v4, if possible. It would be very much appreciated! |
|
@esalter I just made a PR to backport, hopefully they will merge and release it! |
Pull Request check-list
Please make sure to review and check all of these items:
npm run testornpm run test-DIALECTpass with this change (including linting)?Description of change
Add
afterCommithooks for transactions that allows deferring work toafter a transaction has been committed, regardless of if you have
access to the promise chain that created the transaction
Closes: #2805