Fix return duplicate transaction promise for standalone transactions (#3328)

This commit is contained in:
Dmitrii Maganov 2019-07-04 23:05:23 +03:00 committed by Igor Savin
parent 4cde0fcb84
commit a0e9be548d
4 changed files with 19 additions and 15 deletions

View File

@ -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;
}

View File

@ -53,7 +53,7 @@ function initContext(knexFn) {
let trx;
return () => {
if (!trx) {
trx = this.transaction(config);
trx = this.transaction(undefined, config);
}
return trx;
};

View File

@ -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() {

View File

@ -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', () => {