changeset 276:b757c29b20d7

import changes from idavoll changeset 233 (24be3a11ddbc), by Ralph Meijer and based on a patch by Nuno Santos
author souliane <souliane@mailoo.org>
date Mon, 13 Oct 2014 15:59:25 +0200
parents 9c74cd2635f6
children e749401be529
files INSTALL sat_pubsub/pgsql_storage.py sat_pubsub/tap.py sat_pubsub/test/test_storage.py
diffstat 4 files changed, 30 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/INSTALL	Thu Aug 01 16:22:14 2013 -0700
+++ b/INSTALL	Mon Oct 13 15:59:25 2014 +0200
@@ -12,8 +12,8 @@
 
 For the PostgreSQL backend, the following is also required:
 
-- PostgreSQL
-- Psycopg 2
+- PostgreSQL (including development files for psycopg2)
+- psycopg2
 
 
 Installation
--- a/sat_pubsub/pgsql_storage.py	Thu Aug 01 16:22:14 2013 -0700
+++ b/sat_pubsub/pgsql_storage.py	Mon Oct 13 15:59:25 2014 +0200
@@ -174,7 +174,7 @@
         cursor.execute("""SELECT node_id FROM nodes WHERE node=%s""", (nodeIdentifier,));
         node_id = cursor.fetchone()[0]
 
-        cursor.execute("""SELECT 1 from entities where jid=%s""",
+        cursor.execute("""SELECT 1 as bool from entities where jid=%s""",
                        (owner,))
 
         if not cursor.fetchone():
@@ -383,6 +383,7 @@
                        (self.nodeIdentifier,
                         userhost,
                         resource))
+
         row = cursor.fetchone()
         if not row:
             return None
@@ -402,14 +403,14 @@
                    FROM subscriptions
                    NATURAL JOIN nodes
                    NATURAL JOIN entities
-                   WHERE node=%s""";
+                   WHERE node=%s"""
         values = [self.nodeIdentifier]
 
         if state:
             query += " AND state=%s"
             values.append(state)
 
-        cursor.execute(query, values);
+        cursor.execute(query, values)
         rows = cursor.fetchall()
 
         subscriptions = []
@@ -501,7 +502,7 @@
     def _isSubscribed(self, cursor, entity):
         self._checkNodeExists(cursor)
 
-        cursor.execute("""SELECT 1 FROM entities
+        cursor.execute("""SELECT 1 as bool FROM entities
                           NATURAL JOIN subscriptions
                           NATURAL JOIN nodes
                           WHERE entities.jid=%s
@@ -761,28 +762,28 @@
         """
         cursor.execute("""SELECT count(*) FROM callbacks
                           WHERE service=%s and node=%s""",
-                       service.full(),
-                       nodeIdentifier)
+                       (service.full(),
+                        nodeIdentifier))
         results = cursor.fetchall()
         return results[0][0]
 
 
     def addCallback(self, service, nodeIdentifier, callback):
         def interaction(cursor):
-            cursor.execute("""SELECT 1 FROM callbacks
+            cursor.execute("""SELECT 1 as bool FROM callbacks
                               WHERE service=%s and node=%s and uri=%s""",
-                           service.full(),
+                           (service.full(),
                            nodeIdentifier,
-                           callback)
+                           callback))
             if cursor.fetchall():
                 return
 
             cursor.execute("""INSERT INTO callbacks
                               (service, node, uri) VALUES
                               (%s, %s, %s)""",
-                           service.full(),
+                           (service.full(),
                            nodeIdentifier,
-                           callback)
+                           callback))
 
         return self.dbpool.runInteraction(interaction)
 
@@ -791,9 +792,9 @@
         def interaction(cursor):
             cursor.execute("""DELETE FROM callbacks
                               WHERE service=%s and node=%s and uri=%s""",
-                           service.full(),
-                           nodeIdentifier,
-                           callback)
+                           (service.full(),
+                            nodeIdentifier,
+                            callback))
 
             if cursor.rowcount != 1:
                 raise error.NotSubscribed()
@@ -807,8 +808,8 @@
         def interaction(cursor):
             cursor.execute("""SELECT uri FROM callbacks
                               WHERE service=%s and node=%s""",
-                           service.full(),
-                           nodeIdentifier)
+                           (service.full(),
+                            nodeIdentifier))
             results = cursor.fetchall()
 
             if not results:
--- a/sat_pubsub/tap.py	Thu Aug 01 16:22:14 2013 -0700
+++ b/sat_pubsub/tap.py	Mon Oct 13 15:59:25 2014 +0200
@@ -101,6 +101,7 @@
     if config['backend'] == 'pgsql':
         from twisted.enterprise import adbapi
         from sat_pubsub.pgsql_storage import Storage
+        from psycopg2.extras import NamedTupleConnection
         dbpool = adbapi.ConnectionPool('psycopg2',
                                        user=config['dbuser'],
                                        password=config['dbpass'],
@@ -109,6 +110,7 @@
                                        port=config['dbport'],
                                        cp_reconnect=True,
                                        client_encoding='utf-8',
+                                       connection_factory=NamedTupleConnection,
                                        )
         st = Storage(dbpool)
     elif config['backend'] == 'memory':
--- a/sat_pubsub/test/test_storage.py	Thu Aug 01 16:22:14 2013 -0700
+++ b/sat_pubsub/test/test_storage.py	Mon Oct 13 15:59:25 2014 +0200
@@ -536,6 +536,7 @@
                                             database='pubsub_test',
                                             cp_reconnect=True,
                                             client_encoding='utf-8',
+                                            connection_factory=NamedTupleConnection,
                                             )
         self.s = Storage(self.dbpool)
         self.dbpool.start()
@@ -545,7 +546,8 @@
 
 
     def tearDown(self):
-        return self.dbpool.runInteraction(self.cleandb)
+        d = self.dbpool.runInteraction(self.cleandb)
+        return d.addCallback(lambda _: self.dbpool.close())
 
 
     def init(self, cursor):
@@ -625,13 +627,18 @@
         cursor.execute("""DELETE FROM entities WHERE jid=%s""",
                        (SUBSCRIBER.userhost(),))
         cursor.execute("""DELETE FROM entities WHERE jid=%s""",
+                       (SUBSCRIBER_NEW.userhost(),))
+        cursor.execute("""DELETE FROM entities WHERE jid=%s""",
                        (SUBSCRIBER_TO_BE_DELETED.userhost(),))
         cursor.execute("""DELETE FROM entities WHERE jid=%s""",
                        (SUBSCRIBER_PENDING.userhost(),))
         cursor.execute("""DELETE FROM entities WHERE jid=%s""",
                        (PUBLISHER.userhost(),))
 
+
 try:
     import psycopg2
+    psycopg2
+    from psycopg2.extras import NamedTupleConnection
 except ImportError:
-    PgsqlStorageStorageTestCase.skip = "Psycopg2 not available"
+    PgsqlStorageStorageTestCase.skip = "psycopg2 not available"