comparison libervia/backend/memory/migration/versions/fe3a02cb4bec_convert_legacypickle_columns_to_json.py @ 4216:1a7a3e4b52a4

core (memory/migration): Update XEP-0384 and `fe3a02cb4bec_convert_legacypickle_columns_to_json.py` migration to properly handle (de)serialisation of `TrustMessageCacheEntry`.
author Goffi <goffi@goffi.org>
date Tue, 05 Mar 2024 17:31:12 +0100
parents 5f2d496c633f
children 79a4870cfbdf
comparison
equal deleted inserted replaced
4215:31c84a32c897 4216:1a7a3e4b52a4
8 from alembic import op 8 from alembic import op
9 import sqlalchemy as sa 9 import sqlalchemy as sa
10 import pickle 10 import pickle
11 import json 11 import json
12 from libervia.backend.plugins.plugin_xep_0373 import PublicKeyMetadata 12 from libervia.backend.plugins.plugin_xep_0373 import PublicKeyMetadata
13 from libervia.backend.plugins.plugin_xep_0384 import TrustMessageCacheEntry
13 14
14 # revision identifiers, used by Alembic. 15 # revision identifiers, used by Alembic.
15 revision = "fe3a02cb4bec" 16 revision = "fe3a02cb4bec"
16 down_revision = "610345f77e75" 17 down_revision = "610345f77e75"
17 branch_labels = None 18 branch_labels = None
43 ): 44 ):
44 # XEP-0373 plugin was pickling an internal class, this can't be converted 45 # XEP-0373 plugin was pickling an internal class, this can't be converted
45 # directly to JSON, so we do a special treatment with the add `to_dict` and 46 # directly to JSON, so we do a special treatment with the add `to_dict` and
46 # `from_dict` methods. 47 # `from_dict` methods.
47 deserialized = [pkm.to_dict() for pkm in deserialized] 48 deserialized = [pkm.to_dict() for pkm in deserialized]
49
50 elif (
51 table == "private_ind_bin"
52 and primary_keys[0] == "XEP-0384/TM"
53 and primary_keys[1] == "cache"
54 ):
55 # Same issue and solution as for XEP-0373
56 try:
57 deserialized = [tm.to_dict() for tm in deserialized]
58 except Exception as e:
59 print(
60 "Warning: Failed to convert Trust Management cache with value "
61 f" {deserialized!r}, using empty array instead: {e}"
62 )
63 deserialized=[]
48 64
49 ret = json.dumps(deserialized, ensure_ascii=False, default=str) 65 ret = json.dumps(deserialized, ensure_ascii=False, default=str)
50 if table == 'history' and ret == "{}": 66 if table == 'history' and ret == "{}":
51 # For history, we can remove empty data, but for other tables it may be 67 # For history, we can remove empty data, but for other tables it may be
52 # significant. 68 # significant.
102 and not primary_keys[1].startswith("/trust") 118 and not primary_keys[1].startswith("/trust")
103 ): 119 ):
104 # Convert list of dicts back to set of PublicKeyMetadata objects 120 # Convert list of dicts back to set of PublicKeyMetadata objects
105 if isinstance(deserialized, list): 121 if isinstance(deserialized, list):
106 deserialized = {PublicKeyMetadata.from_dict(d) for d in deserialized} 122 deserialized = {PublicKeyMetadata.from_dict(d) for d in deserialized}
123 elif (
124 table == "private_ind_bin"
125 and primary_keys[0] == "XEP-0384/TM"
126 and primary_keys[1] == "cache"
127 ):
128 # Convert list of dicts back to set of TrustMessageCacheEntry objects
129 if isinstance(deserialized, list):
130 deserialized = {TrustMessageCacheEntry.from_dict(d) for d in deserialized}
107 return pickle.dumps(deserialized, 0) 131 return pickle.dumps(deserialized, 0)
108 except Exception as e: 132 except Exception as e:
109 print( 133 print(
110 f"Warning: Failed to convert JSON to pickle, using NULL instead. Error: {e}" 134 f"Warning: Failed to convert JSON to pickle, using NULL instead. Error: {e}"
111 ) 135 )