From 1cd71461f4bc787685e2c0bcaeebea6a15f714ee Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 22 Oct 2019 08:08:56 +0900 Subject: [PATCH 1/2] allow nested transactions when global transaction is in place --- src/Database/index.js | 12 +++++++----- test/unit/database.spec.js | 10 ++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Database/index.js b/src/Database/index.js index eb7a9221..28c0781b 100644 --- a/src/Database/index.js +++ b/src/Database/index.js @@ -178,12 +178,14 @@ class Database { * ``` */ beginTransaction () { + if (this._globalTrx) { + return new Promise((resolve, reject) => { + this._globalTrx.transaction(resolve).catch(() => {}) + }) + } + return new Promise((resolve, reject) => { - this - .knex - .transaction(function (trx) { - resolve(trx) - }).catch(() => {}) + this.knex.transaction(resolve).catch(() => {}) }) } diff --git a/test/unit/database.spec.js b/test/unit/database.spec.js index 6b938155..cd007606 100644 --- a/test/unit/database.spec.js +++ b/test/unit/database.spec.js @@ -127,6 +127,16 @@ test.group('Database | QueryBuilder', (group) => { assert.lengthOf(users, 1) }) + test('create nested transaction (savepoint) when global transaction is in place', async (assert) => { + await this.database.beginGlobalTransaction() + await this.database.table('users').insert({ username: 'michael' }) + const trx = await this.database.beginTransaction() + await trx.table('users').where({ username: 'michael' }).update({ username: 'virk' }) + await this.database.commitGlobalTransaction() + const firstUser = await this.database.table('users').first() + assert.equal(firstUser.username, 'virk') + }) + test('destroy database connection', async (assert) => { await this.database.close() assert.plan(1) From f8486a4421992123c4c2b2bb0e80aeb601f14166 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 23 Oct 2019 08:55:37 +0900 Subject: [PATCH 2/2] improved test to be more like an actual scenario --- test/unit/database.spec.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/test/unit/database.spec.js b/test/unit/database.spec.js index cd007606..b0e3f2e2 100644 --- a/test/unit/database.spec.js +++ b/test/unit/database.spec.js @@ -129,12 +129,11 @@ test.group('Database | QueryBuilder', (group) => { test('create nested transaction (savepoint) when global transaction is in place', async (assert) => { await this.database.beginGlobalTransaction() - await this.database.table('users').insert({ username: 'michael' }) const trx = await this.database.beginTransaction() - await trx.table('users').where({ username: 'michael' }).update({ username: 'virk' }) - await this.database.commitGlobalTransaction() - const firstUser = await this.database.table('users').first() - assert.equal(firstUser.username, 'virk') + await this.database.table('users').insert({ username: 'michael' }, trx) + await trx.commit() + await this.database.rollbackGlobalTransaction() + assert.isUndefined(await this.database.table('users').first()) }) test('destroy database connection', async (assert) => {