Change default behaviour for transaction rollback (#4195)

This commit is contained in:
Igor Savin 2021-01-02 14:23:40 +02:00 committed by GitHub
parent 6275ea3b08
commit c16b731fdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 11 deletions

View File

@ -12,6 +12,16 @@ const config: Knex.Config = {} // this is a type from the Knex namespace
const knexInstance: Knex = knex(config) 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 * 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) * `Knex.raw` support dropped, use `knex.raw` (`require('knex').raw()` won't work anymore)

View File

@ -133,9 +133,7 @@ function initContext(knexFn) {
const config = Object.assign({}, _config); const config = Object.assign({}, _config);
config.userParams = this.userParams || {}; config.userParams = this.userParams || {};
if (config.doNotRejectOnRollback === undefined) { if (config.doNotRejectOnRollback === undefined) {
// Backwards-compatibility: default value changes depending upon config.doNotRejectOnRollback = true;
// whether or not a `container` was provided.
config.doNotRejectOnRollback = !container;
} }
return this._transaction(container, config); return this._transaction(container, config);

View File

@ -55,7 +55,7 @@
"tildify": "2.0.0" "tildify": "2.0.0"
}, },
"peerDependencies": { "peerDependencies": {
"mssql": "^6.3.0", "mssql": "^6.3.1",
"mysql": "^2.18.1", "mysql": "^2.18.1",
"mysql2": "^2.2.5", "mysql2": "^2.2.5",
"pg": "^8.5.1", "pg": "^8.5.1",
@ -85,7 +85,7 @@
] ]
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^14.14.16", "@types/node": "^14.14.19",
"chai": "^4.2.0", "chai": "^4.2.0",
"chai-as-promised": "^7.1.1", "chai-as-promised": "^7.1.1",
"chai-subset-in-order": "^2.1.4", "chai-subset-in-order": "^2.1.4",
@ -93,7 +93,7 @@
"coveralls": "^3.1.0", "coveralls": "^3.1.0",
"cross-env": "^7.0.3", "cross-env": "^7.0.3",
"dtslint": "4.0.6", "dtslint": "4.0.6",
"eslint": "^7.16.0", "eslint": "^7.17.0",
"eslint-config-prettier": "^7.1.0", "eslint-config-prettier": "^7.1.0",
"eslint-plugin-import": "^2.22.1", "eslint-plugin-import": "^2.22.1",
"husky": "^4.3.6", "husky": "^4.3.6",
@ -102,7 +102,7 @@
"lint-staged": "^10.5.3", "lint-staged": "^10.5.3",
"mocha": "^8.2.1", "mocha": "^8.2.1",
"mock-fs": "^4.13.0", "mock-fs": "^4.13.0",
"mssql": "^6.3.0", "mssql": "^6.3.1",
"mysql": "^2.18.1", "mysql": "^2.18.1",
"mysql2": "^2.2.5", "mysql2": "^2.2.5",
"nyc": "^15.1.0", "nyc": "^15.1.0",
@ -116,7 +116,7 @@
"source-map-support": "^0.5.19", "source-map-support": "^0.5.19",
"sqlite3": "^5.0.0", "sqlite3": "^5.0.0",
"tap-spec": "^5.0.0", "tap-spec": "^5.0.0",
"tape": "^5.0.1", "tape": "^5.1.0",
"toxiproxy-node-client": "^2.0.6", "toxiproxy-node-client": "^2.0.6",
"ts-node": "^9.1.1", "ts-node": "^9.1.1",
"tsd": "^0.14.0", "tsd": "^0.14.0",

View File

@ -587,9 +587,14 @@ module.exports = function (knex) {
it('Rollback without an error should not reject with undefined #1966', function () { it('Rollback without an error should not reject with undefined #1966', function () {
return knex return knex
.transaction(function (tr) { .transaction(
tr.rollback(); function (tr) {
}) tr.rollback();
},
{
doNotRejectOnRollback: false,
}
)
.then(function () { .then(function () {
expect(true).to.equal(false, 'Transaction should not have commited'); expect(true).to.equal(false, 'Transaction should not have commited');
}) })

View File

@ -511,6 +511,15 @@ describe('knex', () => {
return knex.destroy(); 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 () => { it('rejects execution promise if there was a manual rollback and transaction is set to reject', async () => {
const knex = Knex(sqliteConfig); const knex = Knex(sqliteConfig);