summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2014-02-16 01:46:24 +0100
committerJeroen van Meeuwen (Kolab Systems) <vanmeeuwen@kolabsys.com>2014-02-16 01:46:24 +0100
commit67284e13dd1a7504e20b8f22fb8895cea9a66f1d (patch)
tree5c06c373e27b95666bf85c9b31072a96d96caeb4
parentf5ce4bf8d795d38acc033489b33f9a77bba2ae39 (diff)
downloadpykolab-67284e13dd1a7504e20b8f22fb8895cea9a66f1d.tar.gz
Allow the auth_cache database to be regenerated automatically when
sqlite on the local filesystem gives errors
-rw-r--r--pykolab/auth/ldap/auth_cache.py39
1 files changed, 34 insertions, 5 deletions
diff --git a/pykolab/auth/ldap/auth_cache.py b/pykolab/auth/ldap/auth_cache.py
index 488deac..bcf38dc 100644
--- a/pykolab/auth/ldap/auth_cache.py
+++ b/pykolab/auth/ldap/auth_cache.py
@@ -88,12 +88,29 @@ mapper(Entry, entry_table)
def del_entry(key):
db = init_db()
- _entries = db.query(Entry).filter_by(key=key).delete()
+
+ try:
+ _entries = db.query(Entry).filter_by(key=key).delete()
+ except sqlalchemy.exc.OperationalError, errmsg:
+ db = init_db(reinit=True)
+ except: sqlalchemy.exc.InvalidRequest, errmsg:
+ db = init_db(reinit=True)
+ finally:
+ _entries = db.query(Entry).filter_by(key=key).delete()
+
db.commit()
def get_entry(key):
db = init_db()
- _entries = db.query(Entry).filter_by(key=key).all()
+
+ try:
+ _entries = db.query(Entry).filter_by(key=key).all()
+ except sqlalchemy.exc.OperationalError, errmsg:
+ db = init_db(reinit=True)
+ except: sqlalchemy.exc.InvalidRequest, errmsg:
+ db = init_db(reinit=True)
+ finally:
+ _entries = db.query(Entry).filter_by(key=key).all()
if len(_entries) == 0:
return None
@@ -107,7 +124,14 @@ def get_entry(key):
def set_entry(key, value):
db = init_db()
- _entries = db.query(Entry).filter_by(key=key).all()
+ try:
+ _entries = db.query(Entry).filter_by(key=key).all()
+ except sqlalchemy.exc.OperationalError, errmsg:
+ db = init_db(reinit=True)
+ except: sqlalchemy.exc.InvalidRequest, errmsg:
+ db = init_db(reinit=True)
+ finally:
+ _entries = db.query(Entry).filter_by(key=key).all()
if len(_entries) == 0:
db.add(
@@ -129,19 +153,24 @@ def purge_entries(db):
db.query(Entry).filter(Entry.last_change <= (datetime.datetime.now() - datetime.timedelta(1))).delete()
db.commit()
-def init_db():
+def init_db(reinit=False):
"""
Returns a SQLAlchemy Session() instance.
"""
global db
- if not db == None:
+ if not db == None and not reinit:
return db
db_uri = conf.get('ldap', 'auth_cache_uri')
if db_uri == None:
db_uri = 'sqlite:///%s/auth_cache.db' % (KOLAB_LIB_PATH)
+ if reinit:
+ import os
+ os.path.isfile('%s/auth_cache.db' % (KOLAB_LIB_PATH)):
+ os.unlink('%s/auth_cache.db' % (KOLAB_LIB_PATH))
+
echo = conf.debuglevel > 8
engine = create_engine(db_uri, echo=echo)
metadata.create_all(engine)