Skip to content
Snippets Groups Projects

F cache version

Merged Henrik tom Wörden requested to merge f-cache-version into dev
2 files
+ 137
4
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -50,6 +50,8 @@ def get_pretty_xml(cont):
return etree.tounicode(cont.to_xml(
local_serialization=True), pretty_print=True)
# Increase this, when changes to the cache tables are made:
CACHE_SCHEMA_VERSION = 2
class Cache(object):
"""
@@ -59,20 +61,67 @@ class Cache(object):
without querying.
"""
def __init__(self, db_file=None):
def __init__(self, db_file=None, force_creation=False):
"""
db_file: The path of the database file.
if force_creation is set to True, the file will be created
regardless of a file at the same path already exists.
"""
if db_file is None:
self.db_file = "cache.db"
else:
self.db_file = db_file
if force_creation:
self.create_cache()
else:
self.check_cache()
def check_cache(self):
"""
Check whether the cache in db file self.db_file exists and conforms
to the latest database schema.
If it does not exist, it will be created using the newest database schema.
If it exists, but the schema is outdated, an exception will be raised.
"""
if not os.path.exists(self.db_file):
self.create_cache()
else:
try:
current_schema = self.get_cache_version()
except sqlite3.OperationalError:
current_schema = 1
# TODO: Write unit tests for too old, too new and non-existent version of cache.
if current_schema > CACHE_SCHEMA_VERSION:
raise RuntimeError("Cache is corrupt or was created with a future version of this program.")
elif current_schema < CACHE_SCHEMA_VERSION:
raise RuntimeError("Cache version too old.")
def get_cache_version(self):
try:
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
c.execute("SELECT schema FROM version")
version_row = c.fetchall()
if len(version_row) != 1:
raise RuntimeError("Cache version table broken.")
return version_row[0][0]
finally:
conn.close()
def create_cache(self):
conn = sqlite3.connect(self.db_file)
c = conn.cursor()
c.execute(
'''CREATE TABLE identifiables (digest text primary key, caosdb_id integer)''')
'''CREATE TABLE identifiables (digest TEXT PRIMARY KEY, caosdb_id INTEGER, caosdb_version TEXT)''')
c.execute(
'''CREATE TABLE version (schema INTEGER)''')
c.execute("INSERT INTO version VALUES (?)", (CACHE_SCHEMA_VERSION,))
conn.commit()
conn.close()
Loading