From c15a57571d55755cabfde0b87dd89a51876f0790 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Wed, 5 Dec 2018 21:00:22 +0200 Subject: [PATCH] [#6956] fix possible `message` and `user-status` duplicates There is a tiny chance that without this fix some users which had message duplicates because of issues with `message-id` calculation **BEFORE** `0.9.31` still have duplicates in their DB and migrations will not pass without this change. It only checks if the message with a new `message-id` has been added and removes duplicate. The same way it removes duplicates from `user-status` entity. --- .../realm/schemas/account/migrations.cljs | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/status_im/data_store/realm/schemas/account/migrations.cljs b/src/status_im/data_store/realm/schemas/account/migrations.cljs index 01f3a98800..d670690461 100644 --- a/src/status_im/data_store/realm/schemas/account/migrations.cljs +++ b/src/status_im/data_store/realm/schemas/account/migrations.cljs @@ -266,9 +266,15 @@ message-id (transport.utils/message-id from raw-payload) raw-payload-hash (transport.utils/sha3 raw-payload)] (vswap! old-ids->new-ids assoc prev-message-id message-id) - (aset message "message-id" message-id) - (aset message "raw-payload-hash" raw-payload-hash) - (aset message "old-message-id" old-message-id))) + (if (.objectForPrimaryKey + new-realm + "message" + message-id) + (.delete new-realm message) + (do + (aset message "message-id" message-id) + (aset message "raw-payload-hash" raw-payload-hash) + (aset message "old-message-id" old-message-id))))) (dotimes [i (.-length user-statuses)] (let [user-status (aget user-statuses i) @@ -276,6 +282,11 @@ new-message-id (get @old-ids->new-ids message-id) public-key (aget user-status "public-key") new-status-id (str new-message-id "-" public-key)] - (when (contains? @old-ids->new-ids message-id) - (aset user-status "status-id" new-status-id) - (aset user-status "message-id" new-message-id)))))) + (if (.objectForPrimaryKey + new-realm + "user-status" + new-status-id) + (.delete new-realm user-status) + (when (contains? @old-ids->new-ids message-id) + (aset user-status "status-id" new-status-id) + (aset user-status "message-id" new-message-id)))))))