Skip to content

Commit

Permalink
Fix errors thrown on commit
Browse files Browse the repository at this point in the history
  • Loading branch information
porsager committed May 15, 2022
1 parent 7e418cd commit 99ddae4
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
10 changes: 6 additions & 4 deletions cjs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,17 @@ function Postgres(a, b) {
const sql = Sql(handler)
sql.savepoint = savepoint
let uncaughtError
, result

name && await sql`savepoint ${ sql(name) }`
try {
const result = await new Promise((resolve, reject) => {
result = await new Promise((resolve, reject) => {
const x = fn(sql)
Promise.resolve(Array.isArray(x) ? Promise.all(x) : x).then(resolve, reject)
})

if (uncaughtError)
throw uncaughtError

!name && await sql`commit`
return result
} catch (e) {
await (name
? sql`rollback to ${ sql(name) }`
Expand All @@ -230,6 +229,9 @@ function Postgres(a, b) {
throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e
}

!name && await sql`commit`
return result

function savepoint(name, fn) {
if (name && Array.isArray(name.raw))
return savepoint(sql => sql.apply(sql, arguments))
Expand Down
17 changes: 17 additions & 0 deletions cjs/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2123,3 +2123,20 @@ t('Supports arrays of fragments', async() => {
x
]
})

t('Does not try rollback when commit errors', async() => {
let notice = null
const sql = postgres({ ...options, onnotice: x => notice = x })
await sql`create table test(x int constraint test_constraint unique deferrable initially deferred)`

await sql.begin('isolation level serializable', async sql => {
await sql`insert into test values(1)`
await sql`insert into test values(1)`
}).catch(e => e)

return [
notice,
null,
await sql`drop table test`
]
})
10 changes: 6 additions & 4 deletions deno/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,17 @@ function Postgres(a, b) {
const sql = Sql(handler)
sql.savepoint = savepoint
let uncaughtError
, result

name && await sql`savepoint ${ sql(name) }`
try {
const result = await new Promise((resolve, reject) => {
result = await new Promise((resolve, reject) => {
const x = fn(sql)
Promise.resolve(Array.isArray(x) ? Promise.all(x) : x).then(resolve, reject)
})

if (uncaughtError)
throw uncaughtError

!name && await sql`commit`
return result
} catch (e) {
await (name
? sql`rollback to ${ sql(name) }`
Expand All @@ -231,6 +230,9 @@ function Postgres(a, b) {
throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e
}

!name && await sql`commit`
return result

function savepoint(name, fn) {
if (name && Array.isArray(name.raw))
return savepoint(sql => sql.apply(sql, arguments))
Expand Down
17 changes: 17 additions & 0 deletions deno/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2126,4 +2126,21 @@ t('Supports arrays of fragments', async() => {
]
})

t('Does not try rollback when commit errors', async() => {
let notice = null
const sql = postgres({ ...options, onnotice: x => notice = x })
await sql`create table test(x int constraint test_constraint unique deferrable initially deferred)`

await sql.begin('isolation level serializable', async sql => {
await sql`insert into test values(1)`
await sql`insert into test values(1)`
}).catch(e => e)

return [
notice,
null,
await sql`drop table test`
]
})

;window.addEventListener("unload", () => Deno.exit(process.exitCode))
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,18 +210,17 @@ function Postgres(a, b) {
const sql = Sql(handler)
sql.savepoint = savepoint
let uncaughtError
, result

name && await sql`savepoint ${ sql(name) }`
try {
const result = await new Promise((resolve, reject) => {
result = await new Promise((resolve, reject) => {
const x = fn(sql)
Promise.resolve(Array.isArray(x) ? Promise.all(x) : x).then(resolve, reject)
})

if (uncaughtError)
throw uncaughtError

!name && await sql`commit`
return result
} catch (e) {
await (name
? sql`rollback to ${ sql(name) }`
Expand All @@ -230,6 +229,9 @@ function Postgres(a, b) {
throw e instanceof PostgresError && e.code === '25P02' && uncaughtError || e
}

!name && await sql`commit`
return result

function savepoint(name, fn) {
if (name && Array.isArray(name.raw))
return savepoint(sql => sql.apply(sql, arguments))
Expand Down
17 changes: 17 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2123,3 +2123,20 @@ t('Supports arrays of fragments', async() => {
x
]
})

t('Does not try rollback when commit errors', async() => {
let notice = null
const sql = postgres({ ...options, onnotice: x => notice = x })
await sql`create table test(x int constraint test_constraint unique deferrable initially deferred)`

await sql.begin('isolation level serializable', async sql => {
await sql`insert into test values(1)`
await sql`insert into test values(1)`
}).catch(e => e)

return [
notice,
null,
await sql`drop table test`
]
})

0 comments on commit 99ddae4

Please sign in to comment.