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

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