Skip to content
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

Importing CSV to Postgresql fails for versions < 9.0 #486

Open
giladarnold opened this issue Oct 13, 2016 · 2 comments
Open

Importing CSV to Postgresql fails for versions < 9.0 #486

giladarnold opened this issue Oct 13, 2016 · 2 comments

Comments

@giladarnold
Copy link

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.

--- odo/backends/sql_csv.py
+++ odo/backends/sql_csv.py
@@ -151,32 +151,55 @@
         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(
+          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'
+              ).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:
@llllllllll
Copy link
Member

Thanks for this, could you open this as a pull request?

@giladarnold
Copy link
Author

#487

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants