From 79deb26150309f9e3c060982806686c6fb81b3c3 Mon Sep 17 00:00:00 2001 From: Ron Dadon Date: Thu, 22 Aug 2024 09:55:35 +0300 Subject: [PATCH 1/8] findOrCreate transaction fail example --- src/sscce-sequelize-6.ts | 51 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index 132a311d5..7172c5601 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -24,7 +24,7 @@ export async function run() { class Foo extends Model {} Foo.init({ - name: DataTypes.TEXT, + name: DataTypes.STRING, }, { sequelize, modelName: 'Foo', @@ -36,6 +36,51 @@ export async function run() { await sequelize.sync({ force: true }); expect(spy).to.have.been.called; - console.log(await Foo.create({ name: 'TS foo' })); - expect(await Foo.count()).to.equal(1); + const afterCommitCb = sinon.spy(); + const nonTransactionCb = sinon.spy(); + + const resetSpy = () => { + afterCommitCb.reset(); + nonTransactionCb.reset(); + } + + const afterSaveFooHook = (instance, { transaction }) => { + if (transaction) { + transaction.afterCommit(() => afterCommitCb()); + return; + } + nonTransactionCb(); + } + + await Foo.create({ name: 'A' }); + expect(nonTransactionCb).to.have.been.called; + expect(afterCommitCb).not.to.have.been.called; + + resetSpy(); + + await Foo.findOrCreate({ where: { name: 'B' } }); + expect(nonTransactionCb).not.to.have.been.called; + expect(afterCommitCb).to.have.been.called; + + resetSpy(); + + await sequelize.transaction(async (transaction) => { + await Foo.create({ name: 'C' }, { transaction }); + }); + expect(nonTransactionCb).not.to.have.been.called; + expect(afterCommitCb).to.have.been.called; + + resetSpy(); + + // This next case will fail + + const internalAfterCommitCb = sinon.spy(); + + await sequelize.transaction(async (transaction) => { + transaction.internalAfterCommitCb(() => internalAfterCommitCb()); + await Foo.findOrCreate({ where: { name: 'C' } }, { transaction }); + }); + expect(internalAfterCommitCb).to.have.been.called; + expect(nonTransactionCb).not.to.have.been.called; + expect(afterCommitCb).to.have.been.called; } From 146b278d55879d2bc01a9e71a9b449055d1a376b Mon Sep 17 00:00:00 2001 From: Ron Dadon Date: Thu, 22 Aug 2024 10:03:36 +0300 Subject: [PATCH 2/8] Update sscce-sequelize-6.ts --- src/sscce-sequelize-6.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index 7172c5601..7ee44052b 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -1,4 +1,4 @@ -import { DataTypes, Model } from 'sequelize'; +import { DataTypes, Model, Transaction } from 'sequelize'; import { createSequelize6Instance } from '../dev/create-sequelize-instance'; import { expect } from 'chai'; import sinon from 'sinon'; @@ -40,11 +40,11 @@ export async function run() { const nonTransactionCb = sinon.spy(); const resetSpy = () => { - afterCommitCb.reset(); - nonTransactionCb.reset(); + afterCommitCb.resetHistory(); + nonTransactionCb.resetHistory(); } - const afterSaveFooHook = (instance, { transaction }) => { + const afterSaveFooHook = (instance: Foo, { transaction: Transaction }) => { if (transaction) { transaction.afterCommit(() => afterCommitCb()); return; @@ -64,7 +64,7 @@ export async function run() { resetSpy(); - await sequelize.transaction(async (transaction) => { + await sequelize.transaction(async (transaction: Transaction) => { await Foo.create({ name: 'C' }, { transaction }); }); expect(nonTransactionCb).not.to.have.been.called; @@ -76,8 +76,8 @@ export async function run() { const internalAfterCommitCb = sinon.spy(); - await sequelize.transaction(async (transaction) => { - transaction.internalAfterCommitCb(() => internalAfterCommitCb()); + await sequelize.transaction(async (transaction: Transaction) => { + transaction.afterCommit(() => internalAfterCommitCb()); await Foo.findOrCreate({ where: { name: 'C' } }, { transaction }); }); expect(internalAfterCommitCb).to.have.been.called; From 65e076c90e69b20603c18e9cb777e4e9df21967a Mon Sep 17 00:00:00 2001 From: Ron Dadon Date: Thu, 22 Aug 2024 10:06:55 +0300 Subject: [PATCH 3/8] Update sscce-sequelize-6.ts --- src/sscce-sequelize-6.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index 7ee44052b..8af5496b9 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -1,4 +1,4 @@ -import { DataTypes, Model, Transaction } from 'sequelize'; +import { DataTypes, Model } from 'sequelize'; import { createSequelize6Instance } from '../dev/create-sequelize-instance'; import { expect } from 'chai'; import sinon from 'sinon'; @@ -44,7 +44,7 @@ export async function run() { nonTransactionCb.resetHistory(); } - const afterSaveFooHook = (instance: Foo, { transaction: Transaction }) => { + const afterSaveFooHook = (instance: Foo, { transaction }) => { if (transaction) { transaction.afterCommit(() => afterCommitCb()); return; @@ -64,7 +64,7 @@ export async function run() { resetSpy(); - await sequelize.transaction(async (transaction: Transaction) => { + await sequelize.transaction(async (transaction) => { await Foo.create({ name: 'C' }, { transaction }); }); expect(nonTransactionCb).not.to.have.been.called; @@ -76,9 +76,9 @@ export async function run() { const internalAfterCommitCb = sinon.spy(); - await sequelize.transaction(async (transaction: Transaction) => { + await sequelize.transaction(async (transaction) => { transaction.afterCommit(() => internalAfterCommitCb()); - await Foo.findOrCreate({ where: { name: 'C' } }, { transaction }); + await Foo.findOrCreate({ where: { name: 'D' }, transaction }); }); expect(internalAfterCommitCb).to.have.been.called; expect(nonTransactionCb).not.to.have.been.called; From dd211fa7c346bed21adbb79e607d6f8507ac9dc6 Mon Sep 17 00:00:00 2001 From: Ron Dadon Date: Thu, 22 Aug 2024 10:11:19 +0300 Subject: [PATCH 4/8] Update sscce-sequelize-6.ts --- src/sscce-sequelize-6.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index 8af5496b9..b1d228288 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -44,9 +44,9 @@ export async function run() { nonTransactionCb.resetHistory(); } - const afterSaveFooHook = (instance: Foo, { transaction }) => { - if (transaction) { - transaction.afterCommit(() => afterCommitCb()); + const afterSaveFooHook = (instance: Foo, options) => { + if (options.transaction) { + options.transaction.afterCommit(() => afterCommitCb()); return; } nonTransactionCb(); From 2bc4eafce50855a80fbfd14937b9b4144bd9e335 Mon Sep 17 00:00:00 2001 From: Ron Dadon Date: Thu, 22 Aug 2024 10:12:46 +0300 Subject: [PATCH 5/8] Update sscce-sequelize-6.ts --- src/sscce-sequelize-6.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index b1d228288..0a6acff5e 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -44,7 +44,7 @@ export async function run() { nonTransactionCb.resetHistory(); } - const afterSaveFooHook = (instance: Foo, options) => { + const afterSaveFooHook = (instance: Foo, options: any) => { if (options.transaction) { options.transaction.afterCommit(() => afterCommitCb()); return; From c547c3bea2701c3ba7c4caf13e461b7a363f0e36 Mon Sep 17 00:00:00 2001 From: Ron Dadon Date: Thu, 22 Aug 2024 10:17:26 +0300 Subject: [PATCH 6/8] Update sscce-sequelize-6.ts --- src/sscce-sequelize-6.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index 0a6acff5e..ff16ab256 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -21,13 +21,13 @@ export async function run() { }, }); - class Foo extends Model {} + class Bar extends Model {} - Foo.init({ + Bar.init({ name: DataTypes.STRING, }, { sequelize, - modelName: 'Foo', + modelName: 'Bar', }); // You can use sinon and chai assertions directly in your SSCCE. @@ -44,7 +44,7 @@ export async function run() { nonTransactionCb.resetHistory(); } - const afterSaveFooHook = (instance: Foo, options: any) => { + const afterSaveFooHook = (instance: Bar, options: any) => { if (options.transaction) { options.transaction.afterCommit(() => afterCommitCb()); return; From 5eaa2e4c4e6dcd5c0242dba2e79dc2c0a92e68b6 Mon Sep 17 00:00:00 2001 From: Ron Dadon Date: Thu, 22 Aug 2024 10:22:37 +0300 Subject: [PATCH 7/8] Update sscce-sequelize-6.ts --- src/sscce-sequelize-6.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index ff16ab256..c322ea839 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -52,20 +52,20 @@ export async function run() { nonTransactionCb(); } - await Foo.create({ name: 'A' }); + await Bar.create({ name: 'A' }); expect(nonTransactionCb).to.have.been.called; expect(afterCommitCb).not.to.have.been.called; resetSpy(); - await Foo.findOrCreate({ where: { name: 'B' } }); + await Bar.findOrCreate({ where: { name: 'B' } }); expect(nonTransactionCb).not.to.have.been.called; expect(afterCommitCb).to.have.been.called; resetSpy(); await sequelize.transaction(async (transaction) => { - await Foo.create({ name: 'C' }, { transaction }); + await Bar.create({ name: 'C' }, { transaction }); }); expect(nonTransactionCb).not.to.have.been.called; expect(afterCommitCb).to.have.been.called; @@ -78,7 +78,7 @@ export async function run() { await sequelize.transaction(async (transaction) => { transaction.afterCommit(() => internalAfterCommitCb()); - await Foo.findOrCreate({ where: { name: 'D' }, transaction }); + await Bar.findOrCreate({ where: { name: 'D' }, transaction }); }); expect(internalAfterCommitCb).to.have.been.called; expect(nonTransactionCb).not.to.have.been.called; From 4305d3e29764c3403e11c33977633e0275f728f2 Mon Sep 17 00:00:00 2001 From: Ron Dadon Date: Thu, 22 Aug 2024 10:26:24 +0300 Subject: [PATCH 8/8] Update sscce-sequelize-6.ts --- src/sscce-sequelize-6.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sscce-sequelize-6.ts b/src/sscce-sequelize-6.ts index c322ea839..f2086f46d 100644 --- a/src/sscce-sequelize-6.ts +++ b/src/sscce-sequelize-6.ts @@ -51,6 +51,8 @@ export async function run() { } nonTransactionCb(); } + + Bar.addHook('afterSave', 'afterSaveFooHook', afterSaveFooHook); await Bar.create({ name: 'A' }); expect(nonTransactionCb).to.have.been.called;