Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/quiet-taxis-close.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@electric-sql/pglite': patch
---

Reject operations performed through transaction handles after the transaction has closed.
2 changes: 2 additions & 0 deletions packages/pglite/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ export abstract class BasePGlite
sqlStrings: TemplateStringsArray,
...params: any[]
): Promise<Results<T>> => {
checkClosed()
const { query, params: actualParams } = queryTemplate(
sqlStrings,
...params,
Expand Down Expand Up @@ -519,6 +520,7 @@ export abstract class BasePGlite
return result
} catch (e) {
if (!closed) {
closed = true
await this.#runExec('ROLLBACK')
}
this.#inTransaction = false
Expand Down
57 changes: 56 additions & 1 deletion packages/pglite/tests/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { expectToThrowAsync, testEsmCjsAndDTC } from './test-utils.ts'
import { identifier } from '../dist/templating.js'
import { PGlite } from '../dist/index.js'
import { PGlite, type Transaction } from '../dist/index.js'

await testEsmCjsAndDTC(async (importType) => {
const { PGlite } =
Expand Down Expand Up @@ -498,6 +498,61 @@ await testEsmCjsAndDTC(async (importType) => {
affectedRows: 0,
})
})

it('rejects sql on closed transaction handles', async () => {
await db.exec('CREATE TABLE closed_transaction_test (id INT PRIMARY KEY)')

let committedTx: Transaction | undefined
await db.transaction(async (tx) => {
committedTx = tx
})

expect(committedTx?.closed).toBe(true)
await expect(
committedTx!.sql`INSERT INTO closed_transaction_test VALUES (1)`,
).rejects.toThrow('Transaction is closed')

let rolledBackTx: Transaction | undefined
await db.transaction(async (tx) => {
rolledBackTx = tx
await tx.rollback()
})

expect(rolledBackTx?.closed).toBe(true)
await expect(
rolledBackTx!.sql`INSERT INTO closed_transaction_test VALUES (2)`,
).rejects.toThrow('Transaction is closed')

const result = await db.query('SELECT id FROM closed_transaction_test')
expect(result.rows).toEqual([])
})

it('closes the transaction handle when the callback rejects', async () => {
await db.exec('CREATE TABLE closed_transaction_test (id INT PRIMARY KEY)')

let failedTx: Transaction | undefined
await expect(
db.transaction(async (tx) => {
failedTx = tx
throw new Error('boom')
}),
).rejects.toThrow('boom')

expect(failedTx?.closed).toBe(true)
await expect(
failedTx!.query('INSERT INTO closed_transaction_test VALUES (1)'),
).rejects.toThrow('Transaction is closed')
await expect(
failedTx!.exec('INSERT INTO closed_transaction_test VALUES (2)'),
).rejects.toThrow('Transaction is closed')
await expect(
failedTx!.sql`INSERT INTO closed_transaction_test VALUES (3)`,
).rejects.toThrow('Transaction is closed')

const result = await db.query('SELECT id FROM closed_transaction_test')
expect(result.rows).toEqual([])
})

it('merge delete', async () => {
await db.exec(`
CREATE TABLE employees (
Expand Down