Skip to content

Commit 7aac14e

Browse files
committed
Raise an error when a binary COPY FROM is attempted w/o appropriate codec.
COPY .. FROM .. (FORMAT binary) currently requires that all data is encoded in binary format. Hence, we need to check if we have appropriate codecs for all columns before initiating CopyIn. Issue: #157.
1 parent 991b1ae commit 7aac14e

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

asyncpg/protocol/protocol.pyx

+4-2
Original file line numberDiff line numberDiff line change
@@ -376,9 +376,11 @@ cdef class BaseProtocol(CoreProtocol):
376376
settings = self.settings
377377

378378
for codec in codecs:
379-
if not codec.has_encoder():
379+
if (not codec.has_encoder() or
380+
codec.format != PG_FORMAT_BINARY):
380381
raise RuntimeError(
381-
'no encoder for OID {}'.format(codec.oid))
382+
'no binary format encoder for '
383+
'type {} (OID {})'.format(codec.name, codec.oid))
382384

383385
for row in records:
384386
# Tuple header

tests/test_copy.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ async def __anext__(self):
574574
finally:
575575
await self.con.execute('DROP TABLE copytab')
576576

577-
async def test_copy_records_to_table(self):
577+
async def test_copy_records_to_table_1(self):
578578
await self.con.execute('''
579579
CREATE TABLE copytab(a text, b int, c timestamptz);
580580
''')
@@ -597,3 +597,19 @@ async def test_copy_records_to_table(self):
597597

598598
finally:
599599
await self.con.execute('DROP TABLE copytab')
600+
601+
async def test_copy_records_to_table_no_binary_codec(self):
602+
await self.con.execute('''
603+
CREATE TABLE copytab(a numeric);
604+
''')
605+
606+
try:
607+
records = [(123.123,)]
608+
609+
with self.assertRaisesRegex(
610+
RuntimeError, 'no binary format encoder'):
611+
await self.con.copy_records_to_table(
612+
'copytab', records=records)
613+
614+
finally:
615+
await self.con.execute('DROP TABLE copytab')

0 commit comments

Comments
 (0)