changeset 4228:79a4870cfbdf

migration: fix migration when XEP-0373 or XEP-0384 can't be imported: Those plugins are using optional modules, and if their importation fails, there is probably no data to migrate either.
author Goffi <goffi@goffi.org>
date Sat, 09 Mar 2024 15:13:14 +0100
parents dfccc90cacc6
children dd9bc7d791d7
files libervia/backend/memory/migration/versions/fe3a02cb4bec_convert_legacypickle_columns_to_json.py
diffstat 1 files changed, 24 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/backend/memory/migration/versions/fe3a02cb4bec_convert_legacypickle_columns_to_json.py	Tue Mar 05 17:31:56 2024 +0100
+++ b/libervia/backend/memory/migration/versions/fe3a02cb4bec_convert_legacypickle_columns_to_json.py	Sat Mar 09 15:13:14 2024 +0100
@@ -9,8 +9,22 @@
 import sqlalchemy as sa
 import pickle
 import json
-from libervia.backend.plugins.plugin_xep_0373 import PublicKeyMetadata
-from libervia.backend.plugins.plugin_xep_0384 import TrustMessageCacheEntry
+try:
+    from libervia.backend.plugins.plugin_xep_0373 import PublicKeyMetadata
+except Exception:
+    PublicKeyMetadata = None
+    print(
+        "Warning: Can't import XEP-0373, its data won't be updated. It's probably not "
+        "used on this installation."
+    )
+try:
+    from libervia.backend.plugins.plugin_xep_0384 import TrustMessageCacheEntry
+except Exception:
+    TrustMessageCacheEntry = None
+    print(
+        "Warning: Can't import XEP-0384, its data won't be updated. It's probably not "
+        "used on this installation."
+    )
 
 # revision identifiers, used by Alembic.
 revision = "fe3a02cb4bec"
@@ -35,7 +49,8 @@
                 encoding="utf-8",
             )
         if (
-            table == "private_ind_bin"
+            PublicKeyMetadata is not None
+            and table == "private_ind_bin"
             and primary_keys[0] == "XEP-0373"
             and not primary_keys[1].startswith("/trust")
             and isinstance(deserialized, set)
@@ -48,7 +63,8 @@
             deserialized = [pkm.to_dict() for pkm in deserialized]
 
         elif (
-            table == "private_ind_bin"
+            TrustMessageCacheEntry is not None
+            and table == "private_ind_bin"
             and primary_keys[0] == "XEP-0384/TM"
             and primary_keys[1] == "cache"
         ):
@@ -113,7 +129,8 @@
         # Check for the specific table and primary key conditions that require special
         # handling
         if (
-            table == "private_ind_bin"
+            PublicKeyMetadata is not None
+            and table == "private_ind_bin"
             and primary_keys[0] == "XEP-0373"
             and not primary_keys[1].startswith("/trust")
         ):
@@ -121,7 +138,8 @@
             if isinstance(deserialized, list):
                 deserialized = {PublicKeyMetadata.from_dict(d) for d in deserialized}
         elif (
-            table == "private_ind_bin"
+            TrustMessageCacheEntry is not None
+            and table == "private_ind_bin"
             and primary_keys[0] == "XEP-0384/TM"
             and primary_keys[1] == "cache"
         ):