Implement cleeanup for TranStore
authorPatrick Uiterwijk <puiterwijk@redhat.com>
Fri, 4 Sep 2015 16:05:33 +0000 (18:05 +0200)
committerPatrick Uiterwijk <puiterwijk@redhat.com>
Fri, 4 Sep 2015 17:53:34 +0000 (19:53 +0200)
Signed-off-by: Patrick Uiterwijk <puiterwijk@redhat.com>
Reviewed-by: Rob Crittenden <rcritten@redhat.com>
ipsilon/util/data.py

index 3f4ccff..52fde62 100644 (file)
@@ -740,9 +740,10 @@ class TranStore(Store):
 
     def __init__(self, path=None):
         super(TranStore, self).__init__('transactions.db')
+        self.table = 'transactions'
 
     def _initialize_schema(self):
-        q = self._query(self._db, 'transactions', UNIQUE_DATA_TABLE,
+        q = self._query(self._db, self.table, UNIQUE_DATA_TABLE,
                         trans=False)
         q.create()
         q._con.close()  # pylint: disable=protected-access
@@ -751,7 +752,7 @@ class TranStore(Store):
         if old_version == 1:
             # In schema version 2, we added indexes and primary keys
             # pylint: disable=protected-access
-            table = self._query(self._db, 'transactions', UNIQUE_DATA_TABLE,
+            table = self._query(self._db, self.table, UNIQUE_DATA_TABLE,
                                 trans=False)._table
             self._db.add_constraint(table.primary_key)
             for index in table.indexes:
@@ -760,6 +761,17 @@ class TranStore(Store):
         else:
             raise NotImplementedError()
 
+    def _cleanup(self):
+        # pylint: disable=protected-access
+        table = SqlQuery(self._db, self.table, UNIQUE_DATA_TABLE)._table
+        in_one_hour = datetime.datetime.now() - datetime.timedelta(hours=1)
+        sel = select([table.columns.uuid]). \
+            where(and_(table.c.name == 'origintime',
+                       table.c.value <= in_one_hour))
+        # pylint: disable=no-value-for-parameter
+        d = table.delete().where(table.c.uuid.in_(sel))
+        return d.execute().rowcount
+
 
 class SAML2SessionStore(Store):