From 0d13d437fe52ed18aff004d8cb25818a2a01ce84 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Tue, 31 Jul 2018 19:51:17 -0700 Subject: [PATCH] Delete before forced bulk insert to fix broken migrations --- js/modules/data.js | 6 ++++++ js/modules/migrate_to_sql.js | 18 +++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/js/modules/data.js b/js/modules/data.js index ab15ddddba..18a83e51ec 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -37,6 +37,7 @@ module.exports = { saveMessage, saveMessages, removeMessage, + _removeMessages, getUnreadByConversation, removeAllMessagesInConversation, @@ -220,6 +221,11 @@ async function removeMessage(id, { Message }) { } } +// Note: this method will not clean up external files, just delete from SQL +async function _removeMessages(ids) { + await channels.removeMessage(ids); +} + async function getMessageById(id, { Message }) { const message = await channels.getMessageById(id); return new Message(message); diff --git a/js/modules/migrate_to_sql.js b/js/modules/migrate_to_sql.js index f2ceb3590b..006be15547 100644 --- a/js/modules/migrate_to_sql.js +++ b/js/modules/migrate_to_sql.js @@ -1,7 +1,12 @@ /* global window, IDBKeyRange */ const { includes, isFunction, isString, last } = require('lodash'); -const { saveMessages, saveUnprocesseds } = require('./data'); +const { + saveMessages, + _removeMessages, + saveUnprocesseds, + removeUnprocessed, +} = require('./data'); const { getMessageExportLastIndex, setMessageExportLastIndex, @@ -34,6 +39,7 @@ async function migrateToSQL({ db, clearStores, handleDOMException }) { const status = await migrateStoreToSQLite({ db, save: saveMessages, + remove: _removeMessages, storeName: 'messages', handleDOMException, lastIndex, @@ -54,6 +60,7 @@ async function migrateToSQL({ db, clearStores, handleDOMException }) { const status = await migrateStoreToSQLite({ db, save: saveUnprocesseds, + remove: removeUnprocessed, storeName: 'unprocessed', handleDOMException, lastIndex, @@ -74,6 +81,7 @@ async function migrateToSQL({ db, clearStores, handleDOMException }) { async function migrateStoreToSQLite({ db, save, + remove, storeName, handleDOMException, lastIndex = null, @@ -85,6 +93,9 @@ async function migrateStoreToSQLite({ if (!isFunction(save)) { throw new Error('Need save function!'); } + if (!isFunction(remove)) { + throw new Error('Need remove function!'); + } if (!isString(storeName)) { throw new Error('Need storeName!'); } @@ -151,6 +162,11 @@ async function migrateStoreToSQLite({ const { items, complete } = await queryPromise; if (items.length) { + // Because of the force save and some failed imports, we're going to delete before + // we attempt to insert. + const ids = items.map(item => item.id); + await remove(ids); + // We need to pass forceSave parameter, because these items already have an // id key. Normally, this call would be interpreted as an update request. await save(items, { forceSave: true });