From 62ff3f56d721e192533883e8d517c8d660b540be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Delhome?= Date: Thu, 28 Nov 2024 14:34:37 +0100 Subject: [PATCH] fix(luigi/contrib/postgres.py): replace copy_from with copy_expert copy_from does not accept 'schema.table' notation since psycopg2.9. Then a bug arises in Luigi if the table refers a schema name. This PR modifies the 'copy' method behavior, by refering copy_expert instead of copy_from, as suggested in https://github.com/psycopg/psycopg2/issues/1294. Fix #3198 --- luigi/contrib/postgres.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/luigi/contrib/postgres.py b/luigi/contrib/postgres.py index 719b80a4d7..19e96e8180 100644 --- a/luigi/contrib/postgres.py +++ b/luigi/contrib/postgres.py @@ -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):