Skip to content

Commit

Permalink
Merge pull request #3324 from delhomer/contrib-postgres-copy-expert
Browse files Browse the repository at this point in the history
fix(luigi/contrib/postgres.py): replace copy_from with copy_expert
  • Loading branch information
dlstadther authored Nov 30, 2024
2 parents 80549f6 + e7608c0 commit e71265b
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions luigi/contrib/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,16 +356,15 @@ def copy(self, cursor, file):
else:
raise Exception('columns must consist of column strings or (column string, type string) tuples (was %r ...)' % (self.columns[0],))

# cursor.copy_from is not available in pg8000
if hasattr(cursor, 'copy_from'):
cursor.copy_from(
file, self.table, null=r'\\N', sep=self.column_separator, columns=column_names)
copy_sql = (
"COPY {table} ({column_list}) FROM STDIN "
"WITH (FORMAT text, NULL '{null_string}', DELIMITER '{delimiter}')"
).format(table=self.table, delimiter=self.column_separator, null_string=r'\\N',
column_list=", ".join(column_names))
# cursor.copy_expert is not available in pg8000
if hasattr(cursor, 'copy_expert'):
cursor.copy_expert(copy_sql, file)
else:
copy_sql = (
"COPY {table} ({column_list}) FROM STDIN "
"WITH (FORMAT text, NULL '{null_string}', DELIMITER '{delimiter}')"
).format(table=self.table, delimiter=self.column_separator, null_string=r'\\N',
column_list=", ".join(column_names))
cursor.execute(copy_sql, stream=file)

def run(self):
Expand Down

0 comments on commit e71265b

Please sign in to comment.