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.
This commit is contained in:
Goffi 2024-03-09 15:13:14 +01:00
commit 188c2c97ba

View file

@ -9,8 +9,22 @@ from alembic import op
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 @@ def convert_pickle_to_json(value, table, primary_keys):
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 @@ def convert_pickle_to_json(value, table, primary_keys):
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 @@ def convert_json_to_pickle(value, table, primary_keys):
# 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 @@ def convert_json_to_pickle(value, table, primary_keys):
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"
):