Also create plugin UserStore data tables
[cascardo/ipsilon.git] / ipsilon / util / data.py
index 3f4ccff..65bf4b5 100644 (file)
@@ -69,7 +69,10 @@ class SqlStore(BaseStore):
             # It's not possible to share connections for SQLite between
             #  threads, so let's use the SingletonThreadPool for them
             pool_args = {'poolclass': SingletonThreadPool}
-        self._dbengine = create_engine(engine_name, **pool_args)
+        self._dbengine = create_engine(engine_name,
+                                       echo=cherrypy.config.get('db.echo',
+                                                                False),
+                                       **pool_args)
         self.is_readonly = False
 
     def add_constraint(self, constraint):
@@ -507,7 +510,8 @@ class Store(Log):
             q = self._query(self._db, table, columns, trans=False)
             rows = q.select(kvfilter)
         except Exception, e:  # pylint: disable=broad-except
-            self.error("Failed to load data for table %s: [%s]" % (table, e))
+            self.error("Failed to load data for table %s for store %s: [%s]"
+                       % (table, self.__class__.__name__, e))
         return self._rows_to_dict_tree(rows)
 
     def load_config(self):
@@ -735,14 +739,23 @@ class UserStore(Store):
         else:
             raise NotImplementedError()
 
+    def create_plugin_data_table(self, plugin_name):
+        if not self.is_readonly:
+            table = plugin_name+'_data'
+            q = self._query(self._db, table, OPTIONS_TABLE,
+                            trans=False)
+            q.create()
+            q._con.close()  # pylint: disable=protected-access
+
 
 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 +764,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 +773,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):