Skip to content

Commit 8f1bf9a

Browse files
committed
Fix Postgresql CSV import for older versions.
Postresql's COPY command syntax has changed from 8.x to 9.x. odo uses the new syntax, which produces a syntax error when running against 8.x and older. Below is a patch that probes the database version and applies the correct syntax. It also fixes a (suspected) bug in how encoding is inferred.
1 parent b3b1ed9 commit 8f1bf9a

File tree

1 file changed

+49
-26
lines changed

1 file changed

+49
-26
lines changed

odo/backends/sql_csv.py

+49-26
Original file line numberDiff line numberDiff line change
@@ -151,32 +151,55 @@ def compile_from_csv_postgres(element, compiler, **kwargs):
151151
raise ValueError(
152152
r'PostgreSQL does not support line terminators other than \n'
153153
)
154-
return compiler.process(
155-
sa.text(
156-
"""
157-
COPY {0} FROM :path (
158-
FORMAT CSV,
159-
DELIMITER :delimiter,
160-
NULL :na_value,
161-
QUOTE :quotechar,
162-
ESCAPE :escapechar,
163-
HEADER :header,
164-
ENCODING :encoding
165-
)
166-
""".format(compiler.preparer.format_table(element.element))
167-
).bindparams(
168-
path=os.path.abspath(element.csv.path),
169-
delimiter=element.delimiter,
170-
na_value=element.na_value,
171-
quotechar=element.quotechar,
172-
escapechar=element.escapechar,
173-
header=element.header,
174-
encoding=element.encoding or element.bind(
175-
'show client_encoding'
176-
).execute().scalar()
177-
),
178-
**kwargs
179-
)
154+
postgres_version = element.bind.execute('select version()').scalar()
155+
if int(postgres_version.split()[1].split('.')[0]) >= 9:
156+
return compiler.process(
157+
sa.text(
158+
"""
159+
COPY {0} FROM :path (
160+
FORMAT CSV,
161+
DELIMITER :delimiter,
162+
NULL :na_value,
163+
QUOTE :quotechar,
164+
ESCAPE :escapechar,
165+
HEADER :header,
166+
ENCODING :encoding
167+
)
168+
""".format(compiler.preparer.format_table(element.element))
169+
).bindparams(
170+
path=os.path.abspath(element.csv.path),
171+
delimiter=element.delimiter,
172+
na_value=element.na_value,
173+
quotechar=element.quotechar,
174+
escapechar=element.escapechar,
175+
header=element.header,
176+
encoding=element.encoding or element.bind.execute(
177+
'show client_encoding'
178+
).scalar()
179+
),
180+
**kwargs
181+
)
182+
else:
183+
return compiler.process(
184+
sa.text((
185+
"""
186+
COPY {0} FROM :path
187+
NULL :na_value
188+
DELIMITER :delimiter
189+
CSV %s
190+
QUOTE :quotechar
191+
ESCAPE :escapechar
192+
""" % ('HEADER' if element.header else '')
193+
).format(compiler.preparer.format_table(element.element))
194+
).bindparams(
195+
path=os.path.abspath(element.csv.path),
196+
delimiter=element.delimiter,
197+
na_value=element.na_value,
198+
quotechar=element.quotechar,
199+
escapechar=element.escapechar,
200+
),
201+
**kwargs
202+
)
180203

181204

182205
try:

0 commit comments

Comments
 (0)