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

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