Roundup Tracker - Issues

Issue 1429391

classification
Updated Postgres backend to use psycopg2
Type: Severity: normal
Components: Database Versions:
process
Status: closed
:
: jpend : dbyrne, fresh, jpend, leonidasxiv, richard, tobias-herp
Priority: normal : patch

Created on 2006-02-10 21:22 by anonymous, last changed 2007-08-29 04:06 by jpend.

Files
File name Uploaded Description Edit Remove
postgres.txt anonymous, 2006-02-10 21:22 back_postgresql.py differences
Messages
msg2806 Author: [hidden] (anonymous) Date: 2006-02-10 21:22
I made changes to the back_postgresql.py to handle
using psycopg2 instead of psycopg.  There weren't that
many changes so I'm not sure if a seperate module would
be necessary or not.  I am including the diff file that
shows the differences between the original
back_postgresql.py and my modified one.

My email is David.Byrne@cambridge-na.com

Thanks,
David Byrne
msg2807 Author: [hidden] (fresh) Date: 2006-06-24 10:01
Logged In: YES 
user_id=24723

I didn't find as many patches were needed:

Index: lib/python/roundup/backends/back_postgresql.py
===================================================================
--- lib/python/roundup/backends/back_postgresql.py     
(revision 1989)
+++ lib/python/roundup/backends/back_postgresql.py     
(working copy)
@@ -9,7 +9,8 @@
 __docformat__ = 'restructuredtext'
 
 import os, shutil, popen2, time
-import psycopg
+import psycopg2
+from psycopg2.extensions import QuotedString
 
 from roundup import hyperdb, date
 from roundup.backends import rdbms_common
@@ -39,8 +40,8 @@
     template1['database'] = 'template1'
     
     try:
-        conn = psycopg.connect(**template1)
-    except psycopg.OperationalError, message:
+        conn = psycopg2.connect(**template1)
+    except psycopg2.OperationalError, message:
         raise hyperdb.DatabaseError, message
     
     conn.set_isolation_level(0)
@@ -59,7 +60,7 @@
     '''
     try:
         cursor.execute(command)
-    except psycopg.ProgrammingError, err:
+    except psycopg2.ProgrammingError, err:
         response = str(err).split('\n')[0]
         if response.find('FATAL') != -1:
             raise RuntimeError, response
@@ -76,7 +77,7 @@
     """Check if database already exists"""
     db = getattr(config, 'POSTGRESQL_DATABASE')
     try:
-        conn = psycopg.connect(**db)
+        conn = psycopg2.connect(**db)
         conn.close()
         if __debug__:
             print >> hyperdb.DEBUG, '+++ database exists +++'
@@ -95,8 +96,8 @@
     def sql_open_connection(self):
         db = getattr(self.config, 'POSTGRESQL_DATABASE')
         try:
-            conn = psycopg.connect(**db)
-        except psycopg.OperationalError, message:
+            conn = psycopg2.connect(**db)
+        except psycopg2.OperationalError, message:
             raise hyperdb.DatabaseError, message
 
         cursor = conn.cursor()
@@ -180,7 +181,7 @@
     def sql_stringquote(self, value):
         ''' psycopg.QuotedString returns a "buffer" object
with the
             single-quotes around it... '''
-        return str(psycopg.QuotedString(str(value)))[1:-1]
+        return str(QuotedString(str(value)))[1:-1]
 
     def sql_index_exists(self, table_name, index_name):
         sql = 'select count(*) from pg_indexes where ' \
msg2808 Author: [hidden] (tobias-herp) Date: 2006-06-27 14:22
Logged In: YES 
user_id=805804

I think the 'old' psycopg should continue to work, too; so
there are 2 possibilities:

1. create a new backend
2. try to import psycopg2 first and psycopg if it failed;
catch ImportError and create methods etc. accordingly.
msg2809 Author: [hidden] (richard) Date: 2006-08-11 00:41
Logged In: YES 
user_id=6405

I've patch Roundup with:

try:
    import psycopg
except ImportError:
    import psycopg2 as psycopg

Since that seems to be the crux of the "simplified" patch 
from fresh and suggested by tobias.

If someone with psycopg2 could *test* this, I'd be 
grateful. Just running the unit test suite should give me 
confidence.
msg2810 Author: [hidden] (tobias-herp) Date: 2006-08-11 01:14
Logged In: YES 
user_id=805804

The documented way to import the compatibility layer of
psycopg2 is

from psycopg2 import psycopg1 as psycopg

Is there a special reason to do it elsewise?
msg2811 Author: [hidden] (dbyrne) Date: 2006-08-11 02:32
Logged In: YES 
user_id=53454

Actually, I don't believe the import psycopg2 as psycopg
works, because I had tried that originally.  I haven't tried
the from psycopg2 import psycopg1 as psycopg, but I will try
that tomorrow at work.  I believe the first doesn't work
because it wouldn't load the compatibility layer.
msg2812 Author: [hidden] (anonymous) Date: 2006-08-11 07:33
Logged In: NO 

The documented way to import the compatibility layer of
psycopg2 is

from psycopg2 import psycopg1 as psycopg

Is there a special reason to do it elsewise?
msg2813 Author: [hidden] (richard) Date: 2006-08-11 11:17
Logged In: YES 
user_id=6405

fresh's patch seemed to imply that we don't need to use 
the "compatibility layer". Then again dbyrne says that 
doesn't work, so I'm a little lost. If someone could give 
the test suite a whirl with the try/except patch I wrote 
below, that will tell us for sure.
msg2814 Author: [hidden] (anonymous) Date: 2006-08-11 12:41
Logged In: NO 

From what I can immediately tell.  Richard's patch seems to
work.  The only problem that I can see is the section with
the QuotedString call.  I'm not sure how to test this area
(I know I hit it before because I wouldn't have known to
change this without hitting a problem).  If I open a python
session and do the import (either import psycopg2 as psycopg
or from psycopg2 import psycopg1 as psycopg), I cannot
successfully call psycopg.QuotedString.  It appears that I
have to import QuotedString seperately from
psycopg2.extensions.  If my analysis is correct, an
alternative patch might be:

try:
    import psycopg
    from psycopg import QuotedString
except:
    from psycopg2 import psycopg1 as psycopg
    from psycopg2.extensions import QuotedString

and then when QuotedString is called just remove the
psycopg. prefix.

I changed the psycopg import from psycopg2 to use the
documented way per Tobias' message.
msg2815 Author: [hidden] (richard) Date: 2006-10-03 23:15
Logged In: YES 
user_id=6405

I've modified the code to match the last message's 
suggestion. I've not tested it.
msg2816 Author: [hidden] (leonidasxiv) Date: 2007-08-11 09:42
Is this issue fixed now? I see there is an entry in the CHANGES.txt about this, but the ticket is still open. I'm running a server with psycopg2 (only), so if this issue is not yet fixed I volunteer to test it.
msg2817 Author: [hidden] (jpend) Date: 2007-08-29 04:06
I've also run roundup with psycopg2; admittedly just in demo/debug type usage, nothing heavy duty. If no one speaks up in a few days I'll assume the problem no longer exists and close this.
History
Date User Action Args
2006-02-10 21:22:47anonymouscreate