Add Message schema version 3

This commit is contained in:
Daniel Gasienica 2018-03-14 20:45:33 -04:00
parent e0428355be
commit 2cd3d5ac16
2 changed files with 21 additions and 8 deletions

View file

@ -13,9 +13,11 @@ const PRIVATE = 'private';
// Version 0
// - Schema initialized
// Version 1
// - Attachments: Auto-orient JPEG attachments using EXIF `Orientation` data
// - Attachments: Auto-orient JPEG attachments using EXIF `Orientation` data.
// Version 2
// - Attachments: Sanitize Unicode order override characters
// - Attachments: Sanitize Unicode order override characters.
// Version 3
// - Attachments: Write attachment data to disk and store relative path to it.
const INITIAL_SCHEMA_VERSION = 0;
// Increment this version number every time we add a message schema upgrade
@ -23,7 +25,7 @@ const INITIAL_SCHEMA_VERSION = 0;
// 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 = 2;
exports.CURRENT_SCHEMA_VERSION = 3;
// Public API
@ -163,5 +165,8 @@ const toVersion3 = exports._withSchemaVersion(
);
// UpgradeStep
exports.upgradeSchema = async (message, context) =>
toVersion2(await toVersion1(await toVersion0(message)));
exports.upgradeSchema = async (message, { writeAttachmentData } = {}) =>
toVersion3(
await toVersion2(await toVersion1(await toVersion0(message))),
{ writeAttachmentData }
);

View file

@ -1,3 +1,4 @@
const stringToArrayBuffer = require('string-to-arraybuffer');
const { assert } = require('chai');
const Message = require('../../../js/modules/types/message');
@ -66,7 +67,7 @@ describe('Message', () => {
const input = {
attachments: [{
contentType: 'application/json',
data: null,
data: stringToArrayBuffer('Its easy if you try'),
fileName: 'test\u202Dfig.exe',
size: 1111,
}],
@ -75,14 +76,21 @@ describe('Message', () => {
const expected = {
attachments: [{
contentType: 'application/json',
data: null,
path: 'abc/abcdefg',
fileName: 'test\uFFFDfig.exe',
size: 1111,
}],
schemaVersion: Message.CURRENT_SCHEMA_VERSION,
};
const actual = await Message.upgradeSchema(input);
const expectedAttachmentData = stringToArrayBuffer('Its easy if you try');
const context = {
writeAttachmentData: async (attachmentData) => {
assert.deepEqual(attachmentData, expectedAttachmentData);
return 'abc/abcdefg';
},
};
const actual = await Message.upgradeSchema(input, context);
assert.deepEqual(actual, expected);
});