diff mbox series

[v6,6/8] prserv: store_value() improvements

Message ID 20240430171512.936371-7-michael.opdenacker@bootlin.com
State New
Headers show
Series prserv: add support for an "upstream" server | expand

Commit Message

Michael Opdenacker April 30, 2024, 5:15 p.m. UTC
From: Michael Opdenacker <michael.opdenacker@bootlin.com>

Add a test_checksum_value() to test whether
a (version, pkgarch, checksum, value) entry already
exists in the database.

This is used to protect the store_value() function from
an error when trying to store a duplicate entry in the database.

Also check whether the current database is open in read-only mode.

Signed-off-by: Michael Opdenacker <michael.opdenacker@bootlin.com>
Cc: Joshua Watt <JPEWhacker@gmail.com>
Cc: Tim Orling <ticotimo@gmail.com>
Cc: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
---
 lib/prserv/db.py | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/lib/prserv/db.py b/lib/prserv/db.py
index 79c9001bf5..88ed8e2125 100644
--- a/lib/prserv/db.py
+++ b/lib/prserv/db.py
@@ -78,6 +78,18 @@  class PRTable(object):
             else:
                 return False
 
+    def test_checksum_value(self, version, pkgarch, checksum, value):
+        """Returns whether the specified value is found in the database for the specified package, architecture and checksum"""
+
+        with closing(self.conn.cursor()) as cursor:
+            data=cursor.execute("SELECT value FROM %s WHERE version=? AND pkgarch=? and checksum=? and value=?;" % self.table,
+                               (version, pkgarch, checksum, value))
+            row=data.fetchone()
+            if row is not None:
+                return True
+            else:
+                return False
+
     def test_value(self, version, pkgarch, value):
         """Returns whether the specified value is found in the database for the specified package and architecture"""
 
@@ -143,15 +155,13 @@  class PRTable(object):
                 return base + ".0"
 
     def store_value(self, version, pkgarch, checksum, value):
-        """Store new value in the database"""
+        """Store value in the database"""
 
-        with closing(self.conn.cursor()) as cursor:
-            try:
+        if not self.read_only and not self.test_checksum_value(version, pkgarch, checksum, value):
+            with closing(self.conn.cursor()) as cursor:
                 cursor.execute("INSERT INTO %s VALUES (?, ?, ?, ?);"  % (self.table),
                            (version, pkgarch, checksum, value))
-            except sqlite3.IntegrityError as exc:
-                logger.error(str(exc))
-            self.conn.commit()
+                self.conn.commit()
 
     def _get_value(self, version, pkgarch, checksum, history):