From 2a02bee83297524abc07f4c9796e27cf41db0d72 Mon Sep 17 00:00:00 2001 From: Rikki Guy Date: Wed, 22 May 2024 18:18:10 +0100 Subject: [PATCH] cache: Switch to apsw for cache SQLite db The bulitin sqlite3 library uses the version of sqlite3 linked to python, which is too old on the production datastore. This commit instead uses the apsw library which bundles a newer version of sqlite3. --- getter/cache.py | 11 ++++------- requirements.in | 1 + requirements.txt | 2 ++ 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/getter/cache.py b/getter/cache.py index dbeb515..595f062 100644 --- a/getter/cache.py +++ b/getter/cache.py @@ -1,6 +1,6 @@ import os import shutil -import sqlite3 +import apsw import hashlib DATABASE_NAME = "cache_datagetter.db" @@ -12,7 +12,7 @@ class DatagetterCacheError(Exception): def setup_database(): try: - con = sqlite3.connect(DATABASE_NAME) + con = apsw.Connection(DATABASE_NAME) cur = con.cursor() cur.execute( """CREATE TABLE IF NOT EXISTS cache @@ -20,7 +20,6 @@ def setup_database(): hash TEXT NOT NULL UNIQUE, json_file TEXT NOT NULL UNIQUE);""" ) - con.commit() con.close() except Exception as e: raise DatagetterCacheError(e) @@ -52,7 +51,7 @@ def hash_file(original_file_path): def get_file(file_hash_str): try: - con = sqlite3.connect(DATABASE_NAME) + con = apsw.Connection(DATABASE_NAME) cur = con.cursor() cur.execute("SELECT json_file FROM cache WHERE hash = ?", (file_hash_str,)) row = cur.fetchone() @@ -76,7 +75,7 @@ def update_cache(json_file_name, file_hash_str, file_identifier, file_type): # Todo clean up cache functionality for orphaned files or to reset the cache try: - con = sqlite3.connect(DATABASE_NAME) + con = apsw.Connection(DATABASE_NAME) cur = con.cursor() cur.execute( @@ -96,8 +95,6 @@ def update_cache(json_file_name, file_hash_str, file_identifier, file_type): ), ) - con.commit() - shutil.copy(json_file_name, CACHE_DIR) except Exception as e: raise DatagetterCacheError(e) diff --git a/requirements.in b/requirements.in index 92ca317..0ea6021 100644 --- a/requirements.in +++ b/requirements.in @@ -3,6 +3,7 @@ jsonschema strict-rfc3339 rfc3987 requests[socks] +apsw>=3.35,<4 # Compatibility with CoVE attrs>=20.3.0 diff --git a/requirements.txt b/requirements.txt index 32c9d9c..f442491 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,6 +4,8 @@ # # pip-compile # +apsw==3.45.3.0 + # via -r requirements.in attrs==21.2.0 # via # -r requirements.in