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