From d8eb6451fb27845d266a2d42e943ed18efc40a6a Mon Sep 17 00:00:00 2001 From: KtorZ Date: Thu, 24 Sep 2020 09:33:33 +0200 Subject: [PATCH] add fallback for metadata JSON parsing from pre-existing database This is NOT 100% safe and it assumes that pre-existing metadata that were serialized with the 'direct' format does not parse with the 'detailed' format. This is however a reasonable assumption to make provided that we only released that for a single week and the number of users was fairly limited. --- .../src/Cardano/Wallet/DB/Sqlite/Types.hs | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs b/lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs index d723ed8b3f9..c0bc75f4fd9 100644 --- a/lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs +++ b/lib/core/src/Cardano/Wallet/DB/Sqlite/Types.hs @@ -355,9 +355,27 @@ instance PersistField TxMetadata where Aeson.encode . metadataToJson TxMetadataJsonDetailedSchema fromPersistValue = - (left (T.pack . displayError) . metadataFromJson TxMetadataJsonDetailedSchema) <=< + (left (T.pack . displayError) . metadataFromJsonWithFallback) <=< (left T.pack . Aeson.eitherDecode . BL.fromStrict . encodeUtf8) <=< fromPersistValue + where + -- FIXME + -- Because of time constraints, we have had two consecutives releases + -- of cardano-wallet which ended up using different conversions method + -- for metadata to/from JSON. + -- As a result, some users' databases contain metadata using the direct + -- JSON conversion while we now expect the detailed schema variant. + -- + -- We do therefore fallback when deserializing data do the direct + -- conversion (which will then be serialized back using the detailed + -- schema). We can remove that fallback after some time has passed since + -- release v2020-09-22. + metadataFromJsonWithFallback json = + case metadataFromJson TxMetadataJsonDetailedSchema json of + Right meta -> Right meta + Left e -> case metadataFromJson TxMetadataJsonNoSchema json of + Right meta -> Right meta + Left{} -> Left e instance PersistFieldSql TxMetadata where sqlType _ = sqlType (Proxy @Text)