mirror of
https://github.com/knex/knex.git
synced 2025-09-26 16:48:29 +00:00
Change default behaviour for transaction rollback (#4195)
This commit is contained in:
parent
6275ea3b08
commit
c16b731fdc
10
UPGRADING.md
10
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)
|
||||
|
@ -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);
|
||||
|
10
package.json
10
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",
|
||||
|
@ -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) {
|
||||
.transaction(
|
||||
function (tr) {
|
||||
tr.rollback();
|
||||
})
|
||||
},
|
||||
{
|
||||
doNotRejectOnRollback: false,
|
||||
}
|
||||
)
|
||||
.then(function () {
|
||||
expect(true).to.equal(false, 'Transaction should not have commited');
|
||||
})
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user