comparison libervia/backend/memory/migration/versions/fe3a02cb4bec_convert_legacypickle_columns_to_json.py @ 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 1a7a3e4b52a4
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4227:dfccc90cacc6 4228:79a4870cfbdf
7 """ 7 """
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 try:
13 from libervia.backend.plugins.plugin_xep_0384 import TrustMessageCacheEntry 13 from libervia.backend.plugins.plugin_xep_0373 import PublicKeyMetadata
14 except Exception:
15 PublicKeyMetadata = None
16 print(
17 "Warning: Can't import XEP-0373, its data won't be updated. It's probably not "
18 "used on this installation."
19 )
20 try:
21 from libervia.backend.plugins.plugin_xep_0384 import TrustMessageCacheEntry
22 except Exception:
23 TrustMessageCacheEntry = None
24 print(
25 "Warning: Can't import XEP-0384, its data won't be updated. It's probably not "
26 "used on this installation."
27 )
14 28
15 # revision identifiers, used by Alembic. 29 # revision identifiers, used by Alembic.
16 revision = "fe3a02cb4bec" 30 revision = "fe3a02cb4bec"
17 down_revision = "610345f77e75" 31 down_revision = "610345f77e75"
18 branch_labels = None 32 branch_labels = None
33 deserialized = pickle.loads( 47 deserialized = pickle.loads(
34 value.replace(b"sat.plugins", b"libervia.backend.plugins"), 48 value.replace(b"sat.plugins", b"libervia.backend.plugins"),
35 encoding="utf-8", 49 encoding="utf-8",
36 ) 50 )
37 if ( 51 if (
38 table == "private_ind_bin" 52 PublicKeyMetadata is not None
53 and table == "private_ind_bin"
39 and primary_keys[0] == "XEP-0373" 54 and primary_keys[0] == "XEP-0373"
40 and not primary_keys[1].startswith("/trust") 55 and not primary_keys[1].startswith("/trust")
41 and isinstance(deserialized, set) 56 and isinstance(deserialized, set)
42 and deserialized 57 and deserialized
43 and isinstance(next(iter(deserialized)), PublicKeyMetadata) 58 and isinstance(next(iter(deserialized)), PublicKeyMetadata)
46 # directly to JSON, so we do a special treatment with the add `to_dict` and 61 # directly to JSON, so we do a special treatment with the add `to_dict` and
47 # `from_dict` methods. 62 # `from_dict` methods.
48 deserialized = [pkm.to_dict() for pkm in deserialized] 63 deserialized = [pkm.to_dict() for pkm in deserialized]
49 64
50 elif ( 65 elif (
51 table == "private_ind_bin" 66 TrustMessageCacheEntry is not None
67 and table == "private_ind_bin"
52 and primary_keys[0] == "XEP-0384/TM" 68 and primary_keys[0] == "XEP-0384/TM"
53 and primary_keys[1] == "cache" 69 and primary_keys[1] == "cache"
54 ): 70 ):
55 # Same issue and solution as for XEP-0373 71 # Same issue and solution as for XEP-0373
56 try: 72 try:
111 try: 127 try:
112 deserialized = json.loads(value) 128 deserialized = json.loads(value)
113 # Check for the specific table and primary key conditions that require special 129 # Check for the specific table and primary key conditions that require special
114 # handling 130 # handling
115 if ( 131 if (
116 table == "private_ind_bin" 132 PublicKeyMetadata is not None
133 and table == "private_ind_bin"
117 and primary_keys[0] == "XEP-0373" 134 and primary_keys[0] == "XEP-0373"
118 and not primary_keys[1].startswith("/trust") 135 and not primary_keys[1].startswith("/trust")
119 ): 136 ):
120 # Convert list of dicts back to set of PublicKeyMetadata objects 137 # Convert list of dicts back to set of PublicKeyMetadata objects
121 if isinstance(deserialized, list): 138 if isinstance(deserialized, list):
122 deserialized = {PublicKeyMetadata.from_dict(d) for d in deserialized} 139 deserialized = {PublicKeyMetadata.from_dict(d) for d in deserialized}
123 elif ( 140 elif (
124 table == "private_ind_bin" 141 TrustMessageCacheEntry is not None
142 and table == "private_ind_bin"
125 and primary_keys[0] == "XEP-0384/TM" 143 and primary_keys[0] == "XEP-0384/TM"
126 and primary_keys[1] == "cache" 144 and primary_keys[1] == "cache"
127 ): 145 ):
128 # Convert list of dicts back to set of TrustMessageCacheEntry objects 146 # Convert list of dicts back to set of TrustMessageCacheEntry objects
129 if isinstance(deserialized, list): 147 if isinstance(deserialized, list):