summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2014-02-10 22:52:02 +0100
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2014-02-10 22:52:02 +0100
commit5bb9406b9a35747f51af637a01da29135d3dd95d (patch)
tree0fef360a78bcbeee21936d65a73996e6e8c5cf99
parent1dd622488bcea0ad61bfbee36cd3cd9cb1abc3ce (diff)
downloadpykolab-5bb9406b9a35747f51af637a01da29135d3dd95d.tar.gz
Allow the cache to be reinitialized automatically
-rw-r--r--pykolab/auth/ldap/cache.py20
1 files changed, 17 insertions, 3 deletions
diff --git a/pykolab/auth/ldap/cache.py b/pykolab/auth/ldap/cache.py
index 55a47c3..ad50e9b 100644
--- a/pykolab/auth/ldap/cache.py
+++ b/pykolab/auth/ldap/cache.py
@@ -107,8 +107,17 @@ def delete_entry(domain, entry):
def get_entry(domain, entry, update=True):
result_attribute = conf.get('cyrus-sasl', 'result_attribute')
+ _entry = None
+
db = init_db(domain)
- _entry = db.query(Entry).filter_by(uniqueid=entry['id']).first()
+ try:
+ _entry = db.query(Entry).filter_by(uniqueid=entry['id']).first()
+ except sqlalchemy.exc.OperationalError, errmsg:
+ db = init_db(domain,reinit=True)
+ except sqlalchemy.exc.InvalidRequestError, errmsg:
+ db = init_db(domain,reinit=True)
+ finally:
+ _entry = db.query(Entry).filter_by(uniqueid=entry['id']).first()
if not update:
return _entry
@@ -149,15 +158,20 @@ def get_entry(domain, entry, update=True):
return _entry
-def init_db(domain):
+def init_db(domain,reinit=False):
"""
Returns a SQLAlchemy Session() instance.
"""
global db
- if not db == None:
+ if not db == None and not reinit:
return db
+ if reinit:
+ import os
+ if os.path.isfile('sqlite:///%s/%s.db' % (KOLAB_LIB_PATH, domain)):
+ os.unlink('sqlite:///%s/%s.db' % (KOLAB_LIB_PATH, domain))
+
db_uri = 'sqlite:///%s/%s.db' % (KOLAB_LIB_PATH, domain)
echo = conf.debuglevel > 8
engine = create_engine(db_uri, echo=echo)