-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix Postgresql CSV import for older versions. #487
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -151,32 +151,55 @@ def compile_from_csv_postgres(element, compiler, **kwargs): | |
raise ValueError( | ||
r'PostgreSQL does not support line terminators other than \n' | ||
) | ||
return compiler.process( | ||
sa.text( | ||
""" | ||
COPY {0} FROM :path ( | ||
FORMAT CSV, | ||
DELIMITER :delimiter, | ||
NULL :na_value, | ||
QUOTE :quotechar, | ||
ESCAPE :escapechar, | ||
HEADER :header, | ||
ENCODING :encoding | ||
) | ||
""".format(compiler.preparer.format_table(element.element)) | ||
).bindparams( | ||
path=os.path.abspath(element.csv.path), | ||
delimiter=element.delimiter, | ||
na_value=element.na_value, | ||
quotechar=element.quotechar, | ||
escapechar=element.escapechar, | ||
header=element.header, | ||
encoding=element.encoding or element.bind( | ||
'show client_encoding' | ||
).execute().scalar() | ||
), | ||
**kwargs | ||
) | ||
postgres_version = element.bind.execute('select version()').scalar() | ||
if int(postgres_version.split()[1].split('.')[0]) >= 9: | ||
return compiler.process( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in order to make this easier to follow, what do you think about breaking these two calls into helper functions like |
||
sa.text( | ||
""" | ||
COPY {0} FROM :path ( | ||
FORMAT CSV, | ||
DELIMITER :delimiter, | ||
NULL :na_value, | ||
QUOTE :quotechar, | ||
ESCAPE :escapechar, | ||
HEADER :header, | ||
ENCODING :encoding | ||
) | ||
""".format(compiler.preparer.format_table(element.element)) | ||
).bindparams( | ||
path=os.path.abspath(element.csv.path), | ||
delimiter=element.delimiter, | ||
na_value=element.na_value, | ||
quotechar=element.quotechar, | ||
escapechar=element.escapechar, | ||
header=element.header, | ||
encoding=element.encoding or element.bind.execute( | ||
'show client_encoding' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could probably also memoize the encoding if we are caching the version. |
||
).scalar() | ||
), | ||
**kwargs | ||
) | ||
else: | ||
return compiler.process( | ||
sa.text(( | ||
""" | ||
COPY {0} FROM :path | ||
NULL :na_value | ||
DELIMITER :delimiter | ||
CSV %s | ||
QUOTE :quotechar | ||
ESCAPE :escapechar | ||
""" % ('HEADER' if element.header else '') | ||
).format(compiler.preparer.format_table(element.element)) | ||
).bindparams( | ||
path=os.path.abspath(element.csv.path), | ||
delimiter=element.delimiter, | ||
na_value=element.na_value, | ||
quotechar=element.quotechar, | ||
escapechar=element.escapechar, | ||
), | ||
**kwargs | ||
) | ||
|
||
|
||
try: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am worried about the perf penalty we will pay to check this on every insert. What do you think about creating a
WeakKeyDictionary
frombind
to version? This will let us save these checks across calls.