Skip to content

Commit 4a0fe34

Browse files
committed
Better error if trying to run query during copy
1 parent 32feb25 commit 4a0fe34

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,6 +1331,11 @@ This error is thrown for any queries that were pending when the timeout to [`sql
13311331
13321332
This error is thrown if the startup phase of the connection (tcp, protocol negotiation, and auth) took more than the default 30 seconds or what was specified using `connect_timeout` or `PGCONNECT_TIMEOUT`.
13331333

1334+
##### COPY_IN_PROGRESS
1335+
> You cannot execute queries during copy
1336+
1337+
This error is thrown if trying to run a query during a copy operation (writable / readable).
1338+
13341339
## TypeScript support
13351340

13361341
`postgres` has TypeScript support. You can pass a row list type for your queries in this way:

src/connection.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,10 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
154154
function execute(q) {
155155
if (terminated)
156156
return queryError(q, Errors.connection('CONNECTION_DESTROYED', options))
157-
157+
158+
if (stream)
159+
return queryError(q, Errors.generic('COPY_IN_PROGRESS', 'You cannot execute queries during copy'))
160+
158161
if (q.cancelled)
159162
return
160163

@@ -878,6 +881,7 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
878881
final(callback) {
879882
socket.write(b().c().end())
880883
final = callback
884+
stream = null
881885
}
882886
})
883887
query.resolve(stream)

tests/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,3 +2614,16 @@ t('Ensure reserve on query throws proper error', async() => {
26142614
'wat', x, reserved.release()
26152615
]
26162616
})
2617+
2618+
t('query during copy error', async() => {
2619+
const sql = postgres(options) // eslint-disable-line
2620+
await sql`create table test (id serial primary key, name text)`
2621+
const copy = await sql`copy test from stdin`.writable()
2622+
const error = await sql`select 1`.catch(e => e)
2623+
await copy.end()
2624+
2625+
return [
2626+
'COPY_IN_PROGRESS', error.code,
2627+
await sql`drop table test`
2628+
]
2629+
})

0 commit comments

Comments
 (0)