Skip to content

Commit

Permalink
Merge f8486a4 into 6cd666d
Browse files Browse the repository at this point in the history
  • Loading branch information
MZanggl committed Oct 22, 2019
2 parents 6cd666d + f8486a4 commit bfed1c2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Database/index.js
Expand Up @@ -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(() => {})
})
}

Expand Down
9 changes: 9 additions & 0 deletions test/unit/database.spec.js
Expand Up @@ -127,6 +127,15 @@ 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()
const trx = await this.database.beginTransaction()
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) => {
await this.database.close()
assert.plan(1)
Expand Down

0 comments on commit bfed1c2

Please sign in to comment.