Skip to content

Commit e34826d

Browse files
committed
Fix reserved queries failing on connect
1 parent ca4da7c commit e34826d

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/connection.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
109109
queue: queues.closed,
110110
idleTimer,
111111
connect(query) {
112-
initial = query || true
112+
initial = query
113113
reconnect()
114114
},
115115
terminate,
@@ -381,10 +381,13 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
381381
function errored(err) {
382382
stream && (stream.destroy(err), stream = null)
383383
query && queryError(query, err)
384-
initial && (initial.reserve ? initial.reject(err) : queryError(initial, err), initial = null)
384+
initial && (queryError(initial, err), initial = null)
385385
}
386386

387387
function queryError(query, err) {
388+
if (query.reserve)
389+
return query.reject(err)
390+
388391
if (!err || typeof err !== 'object')
389392
err = new Error(err)
390393

@@ -535,11 +538,11 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
535538
}
536539

537540
if (needsTypes) {
538-
initial === true && (initial = null)
541+
initial.reserve && (initial = null)
539542
return fetchArrayTypes()
540543
}
541544

542-
initial !== true && execute(initial)
545+
initial && !initial.reserve && execute(initial)
543546
options.shared.retries = retries = 0
544547
initial = null
545548
return

tests/index.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ t('Proper handling of null Errors', async() => {
445445
]
446446
})
447447

448-
t('Ensure reserve throws proper error', async() => {
448+
t('Ensure reserve on connection throws proper error', async() => {
449449
const sql = postgres({ socket: () => { throw 'wat' }, idle_timeout }) // eslint-disable-line
450450

451451
return [
@@ -2604,3 +2604,13 @@ t('arrays in reserved connection', async() => {
26042604
x.join('')
26052605
]
26062606
})
2607+
2608+
t('Ensure reserve on query throws proper error', async() => {
2609+
const sql = postgres({ idle_timeout }) // eslint-disable-line
2610+
const reserved = await sql.reserve()
2611+
const [{ x }] = await reserved`select 'wat' as x`
2612+
2613+
return [
2614+
'wat', x, reserved.release()
2615+
]
2616+
})

0 commit comments

Comments
 (0)