mirror of
https://github.com/knex/knex.git
synced 2025-12-03 10:26:50 +00:00
Fix return duplicate transaction promise for standalone transactions (#3328)
This commit is contained in:
parent
4cde0fcb84
commit
a0e9be548d
@ -8,7 +8,7 @@ const makeKnex = require('./util/make-knex');
|
||||
|
||||
const debug = Debug('knex:tx');
|
||||
|
||||
const { uniqueId, isUndefined, get } = require('lodash');
|
||||
const { uniqueId, isUndefined } = require('lodash');
|
||||
|
||||
// Acts as a facade for a Promise, keeping the internal state
|
||||
// and managing any child transactions.
|
||||
@ -75,7 +75,13 @@ class Transaction extends EventEmitter {
|
||||
return makeTransactor(this, connection, trxClient);
|
||||
})
|
||||
.then((transactor) => {
|
||||
transactor.executionPromise = executionPromise;
|
||||
if (this.initPromise) {
|
||||
transactor.executionPromise = executionPromise.catch((err) => {
|
||||
throw err;
|
||||
});
|
||||
} else {
|
||||
transactor.executionPromise = executionPromise;
|
||||
}
|
||||
|
||||
// If we've returned a "thenable" from the transaction container, assume
|
||||
// the rollback and commit are chained to this object's success / failure.
|
||||
@ -177,10 +183,7 @@ class Transaction extends EventEmitter {
|
||||
}
|
||||
if (status === 2) {
|
||||
if (isUndefined(value)) {
|
||||
if (
|
||||
get(res, 'context.sql', '').toUpperCase() === 'ROLLBACK' &&
|
||||
this.doNotRejectOnRollback
|
||||
) {
|
||||
if (this.doNotRejectOnRollback && /^ROLLBACK\b/i.test(sql)) {
|
||||
this._resolver();
|
||||
return;
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ function initContext(knexFn) {
|
||||
let trx;
|
||||
return () => {
|
||||
if (!trx) {
|
||||
trx = this.transaction(config);
|
||||
trx = this.transaction(undefined, config);
|
||||
}
|
||||
return trx;
|
||||
};
|
||||
|
||||
@ -377,11 +377,10 @@ module.exports = function(knex) {
|
||||
|
||||
it('does not reject promise when rolling back a transaction', async () => {
|
||||
const trxProvider = knex.transactionProvider();
|
||||
const trxPromise = trxProvider();
|
||||
const trx = await trxProvider();
|
||||
|
||||
await trxPromise.then((trx) => {
|
||||
return trx.rollback();
|
||||
});
|
||||
await trx.rollback();
|
||||
await trx.executionPromise;
|
||||
});
|
||||
|
||||
it('should allow for nested transactions', function() {
|
||||
|
||||
@ -323,6 +323,9 @@ describe('knex', () => {
|
||||
.then((rows) => {
|
||||
expect(rows[0].result).to.equal(1);
|
||||
return transaction.commit();
|
||||
})
|
||||
.then(() => {
|
||||
return transaction.executionPromise;
|
||||
});
|
||||
});
|
||||
|
||||
@ -405,11 +408,10 @@ describe('knex', () => {
|
||||
it('does not reject promise when rolling back a transaction', async () => {
|
||||
const knex = Knex(sqliteConfig);
|
||||
const trxProvider = knex.transactionProvider();
|
||||
const trxPromise = trxProvider();
|
||||
const trx = await trxProvider();
|
||||
|
||||
await trxPromise.then((trx) => {
|
||||
return trx.rollback();
|
||||
});
|
||||
await trx.rollback();
|
||||
await trx.executionPromise;
|
||||
});
|
||||
|
||||
it('creating transaction copy with user params should throw an error', () => {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user