diff --git a/UPGRADING.md b/UPGRADING.md index 2a3e6a0c..6abbe46e 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -12,6 +12,16 @@ const config: Knex.Config = {} // this is a type from the Knex namespace const knexInstance: Knex = knex(config) ``` +* Transaction rollback does not trigger a promise rejection for transactions with specified handler. If you want to preserve previous behavior, pass `config` object with `doNotRejectOnRollback: false`: +```javascript + await knex.transaction(async trx => { + const ids = await trx('catalogues') + .insert({ + name: 'Old Books' + }, 'id') + }, { doNotRejectOnRollback: false }); +``` + * Connection url parsing changed from legacy [url.parse](https://nodejs.org/docs/latest-v10.x/api/url.html#url_legacy_url_api) to [WHATWG URL](https://nodejs.org/docs/latest-v10.x/api/url.html#url_the_whatwg_url_api). If you have symbols, unusual for a URL (not A-z, not digits, not dot, not dash) - check [Node.js docs](https://nodejs.org/docs/latest-v10.x/api/url.html#url_percent_encoding_in_urls) for details * `Knex.raw` support dropped, use `knex.raw` (`require('knex').raw()` won't work anymore) diff --git a/lib/knex-builder/make-knex.js b/lib/knex-builder/make-knex.js index df8c01d4..4b6956c9 100644 --- a/lib/knex-builder/make-knex.js +++ b/lib/knex-builder/make-knex.js @@ -133,9 +133,7 @@ function initContext(knexFn) { const config = Object.assign({}, _config); config.userParams = this.userParams || {}; if (config.doNotRejectOnRollback === undefined) { - // Backwards-compatibility: default value changes depending upon - // whether or not a `container` was provided. - config.doNotRejectOnRollback = !container; + config.doNotRejectOnRollback = true; } return this._transaction(container, config); diff --git a/package.json b/package.json index 6051d04d..088f64a3 100644 --- a/package.json +++ b/package.json @@ -55,7 +55,7 @@ "tildify": "2.0.0" }, "peerDependencies": { - "mssql": "^6.3.0", + "mssql": "^6.3.1", "mysql": "^2.18.1", "mysql2": "^2.2.5", "pg": "^8.5.1", @@ -85,7 +85,7 @@ ] }, "devDependencies": { - "@types/node": "^14.14.16", + "@types/node": "^14.14.19", "chai": "^4.2.0", "chai-as-promised": "^7.1.1", "chai-subset-in-order": "^2.1.4", @@ -93,7 +93,7 @@ "coveralls": "^3.1.0", "cross-env": "^7.0.3", "dtslint": "4.0.6", - "eslint": "^7.16.0", + "eslint": "^7.17.0", "eslint-config-prettier": "^7.1.0", "eslint-plugin-import": "^2.22.1", "husky": "^4.3.6", @@ -102,7 +102,7 @@ "lint-staged": "^10.5.3", "mocha": "^8.2.1", "mock-fs": "^4.13.0", - "mssql": "^6.3.0", + "mssql": "^6.3.1", "mysql": "^2.18.1", "mysql2": "^2.2.5", "nyc": "^15.1.0", @@ -116,7 +116,7 @@ "source-map-support": "^0.5.19", "sqlite3": "^5.0.0", "tap-spec": "^5.0.0", - "tape": "^5.0.1", + "tape": "^5.1.0", "toxiproxy-node-client": "^2.0.6", "ts-node": "^9.1.1", "tsd": "^0.14.0", diff --git a/test/integration/execution/transaction.js b/test/integration/execution/transaction.js index 81321ed8..0fb657ab 100644 --- a/test/integration/execution/transaction.js +++ b/test/integration/execution/transaction.js @@ -587,9 +587,14 @@ module.exports = function (knex) { it('Rollback without an error should not reject with undefined #1966', function () { return knex - .transaction(function (tr) { - tr.rollback(); - }) + .transaction( + function (tr) { + tr.rollback(); + }, + { + doNotRejectOnRollback: false, + } + ) .then(function () { expect(true).to.equal(false, 'Transaction should not have commited'); }) diff --git a/test/unit/knex.js b/test/unit/knex.js index dbbd5d1c..785e9761 100644 --- a/test/unit/knex.js +++ b/test/unit/knex.js @@ -511,6 +511,15 @@ describe('knex', () => { return knex.destroy(); }); + it('does not reject transaction by default when handler is provided and there is a rollback', async () => { + const knex = Knex(sqliteConfig); + await knex.transaction((trx) => { + trx.rollback(); + }); + + return knex.destroy(); + }); + it('rejects execution promise if there was a manual rollback and transaction is set to reject', async () => { const knex = Knex(sqliteConfig);