From 101041f106efa273dec338bf22cc72b95963a1b7 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 7 May 2018 15:48:48 -0400 Subject: [PATCH] Derive `Message.CURRENT_SCHEMA_VERSION` --- js/modules/types/message.js | 33 ++++++++++++--------------------- 1 file changed, 12 insertions(+), 21 deletions(-) diff --git a/js/modules/types/message.js b/js/modules/types/message.js index f2d8288cb..defefb4f3 100644 --- a/js/modules/types/message.js +++ b/js/modules/types/message.js @@ -35,13 +35,6 @@ const PRIVATE = 'private'; const INITIAL_SCHEMA_VERSION = 0; -// Increment this version number every time we add a message schema upgrade -// step. This will allow us to retroactively upgrade existing messages. As we -// add more upgrade steps, we could design a pipeline that does this -// incrementally, e.g. from version 0 / unknown -> 1, 1 --> 2, etc., similar to -// how we do database migrations: -exports.CURRENT_SCHEMA_VERSION = 6; - // Public API exports.GROUP = GROUP; exports.PRIVATE = PRIVATE; @@ -212,7 +205,6 @@ exports._mapQuotedAttachments = upgradeAttachment => async ( }; const toVersion0 = async message => exports.initializeSchemaVersion(message); - const toVersion1 = exports._withSchemaVersion( 1, exports._mapAttachments(Attachment.autoOrientJPEG) @@ -230,7 +222,6 @@ const toVersion4 = exports._withSchemaVersion( exports._mapQuotedAttachments(Attachment.migrateDataToFileSystem) ); const toVersion5 = exports._withSchemaVersion(5, initializeAttachmentMetadata); - const toVersion6 = exports._withSchemaVersion( 6, exports._mapContact( @@ -238,6 +229,16 @@ const toVersion6 = exports._withSchemaVersion( ) ); +const VERSIONS = [ + toVersion0, + toVersion1, + toVersion2, + toVersion3, + toVersion4, + toVersion5, +]; +exports.CURRENT_SCHEMA_VERSION = VERSIONS.length - 1; + // UpgradeStep exports.upgradeSchema = async (rawMessage, { writeNewAttachmentData } = {}) => { if (!isFunction(writeNewAttachmentData)) { @@ -245,18 +246,8 @@ exports.upgradeSchema = async (rawMessage, { writeNewAttachmentData } = {}) => { } let message = rawMessage; - const versions = [ - toVersion0, - toVersion1, - toVersion2, - toVersion3, - toVersion4, - toVersion5, - toVersion6, - ]; - - for (let i = 0, max = versions.length; i < max; i += 1) { - const currentVersion = versions[i]; + // eslint-disable-next-line no-restricted-syntax + for (const currentVersion of VERSIONS) { // We really do want this intra-loop await because this is a chained async action, // each step dependent on the previous // eslint-disable-next-line no-await-in-loop