Blame SOURCES/0005-db-fix-upgrade-backup.patch

32281e
From 64b6bd89d0faad3274d0b224b1d1c92fcd397a62 Mon Sep 17 00:00:00 2001
32281e
From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Hor=C3=A1=C4=8Dek?=
32281e
 <shoracek@redhat.com>
32281e
Date: Wed, 2 Nov 2022 19:23:13 +0100
32281e
Subject: [PATCH 5/6] db: fix upgrade backup
32281e
MIME-Version: 1.0
32281e
Content-Type: text/plain; charset=UTF-8
32281e
Content-Transfer-Encoding: 8bit
32281e
32281e
During a failed upgrade, the original database was deleted and replaced
32281e
with the upgraded one, making it impossible to revert the failed
32281e
upgrade.
32281e
32281e
This commit fixes this problem by keeping the old version of the
32281e
database as a separate file for upgrades that finished successfully and
32281e
keeping the original database for those that did not.
32281e
32281e
Signed-off-by: Štěpán Horáček <shoracek@redhat.com>
32281e
---
32281e
 tools/tpm2_pkcs11/db.py | 36 +++++++++++++++++++++---------------
32281e
 1 file changed, 21 insertions(+), 15 deletions(-)
32281e
32281e
diff --git a/tools/tpm2_pkcs11/db.py b/tools/tpm2_pkcs11/db.py
32281e
index 1b18b8f..d0a526b 100644
32281e
--- a/tools/tpm2_pkcs11/db.py
32281e
+++ b/tools/tpm2_pkcs11/db.py
32281e
@@ -454,27 +454,33 @@ class Db(object):
32281e
                     REPLACE INTO schema (id, schema_version) VALUES (1, {version});
32281e
                 '''.format(version=new_version))
32281e
             dbbakcon.execute(sql)
32281e
-        finally:
32281e
-            # Close the connections
32281e
-            self._conn.commit()
32281e
-            self._conn.close()
32281e
-
32281e
+        except Exception as e:
32281e
+            # Close the connection to backup
32281e
             dbbakcon.commit()
32281e
             dbbakcon.close()
32281e
 
32281e
-            # move old db to ".old" suffix
32281e
-            olddbpath = self._path + ".old"
32281e
-            os.rename(self._path, olddbpath)
32281e
+            # unlink the backup
32281e
+            os.unlink(dbbakpath)
32281e
+
32281e
+            raise e
32281e
+
32281e
+        # Close the connections
32281e
+        self._conn.commit()
32281e
+        self._conn.close()
32281e
 
32281e
-            # move the backup to the normal dbpath
32281e
-            os.rename(dbbakpath, self._path)
32281e
+        dbbakcon.commit()
32281e
+        dbbakcon.close()
32281e
 
32281e
-            # unlink the old
32281e
-            os.unlink(olddbpath)
32281e
+        # move old db to ".old" suffix
32281e
+        olddbpath = self._path + ".old"
32281e
+        os.rename(self._path, olddbpath)
32281e
 
32281e
-            # re-establish a connection
32281e
-            self._conn = sqlite3.connect(self._path)
32281e
-            self._conn.row_factory = sqlite3.Row
32281e
+        # move the backup to the normal dbpath
32281e
+        os.rename(dbbakpath, self._path)
32281e
+
32281e
+        # re-establish a connection
32281e
+        self._conn = sqlite3.connect(self._path)
32281e
+        self._conn.row_factory = sqlite3.Row
32281e
 
32281e
     def _get_version(self):
32281e
         c = self._conn.cursor()
32281e
-- 
32281e
2.38.1
32281e