From cb113dd2a65d9a650c25fb6cb56e371dd07b2ae5 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Mon, 27 Feb 2023 15:16:22 -0330 Subject: [PATCH] Convert the migration template to TypeScript (#17836) The migration template has been converted to TypeScript. We can start writing migrations in TypeScript today; they have no dependencies on JavaScript modules. --- app/scripts/migrations/template.js | 29 ------------------------- app/scripts/migrations/template.test.js | 8 +++---- app/scripts/migrations/template.ts | 28 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 33 deletions(-) delete mode 100644 app/scripts/migrations/template.js create mode 100644 app/scripts/migrations/template.ts diff --git a/app/scripts/migrations/template.js b/app/scripts/migrations/template.js deleted file mode 100644 index b3f050dfc..000000000 --- a/app/scripts/migrations/template.js +++ /dev/null @@ -1,29 +0,0 @@ -// next version number -/* - -description of migration and what it does - -*/ - -import { cloneDeep } from 'lodash'; - -const version = 0; - -export default { - version, - - async migrate(originalVersionedData) { - const versionedData = cloneDeep(originalVersionedData); - versionedData.meta.version = version; - const state = versionedData.data; - const newState = transformState(state); - versionedData.data = newState; - return versionedData; - }, -}; - -function transformState(state) { - const newState = state; - // transform state here - return newState; -} diff --git a/app/scripts/migrations/template.test.js b/app/scripts/migrations/template.test.js index f24dc1bca..2c0e0bcc9 100644 --- a/app/scripts/migrations/template.test.js +++ b/app/scripts/migrations/template.test.js @@ -1,13 +1,13 @@ -import migrationTemplate from './template'; +import { migrate, version } from './template'; const storage = { - meta: {}, + meta: { version: -1 }, data: {}, }; describe('storage is migrated successfully', () => { it('should work', async () => { - const migratedData = await migrationTemplate.migrate(storage); - expect(migratedData.meta.version).toStrictEqual(0); + const migratedData = await migrate(storage); + expect(migratedData.meta.version).toStrictEqual(version); }); }); diff --git a/app/scripts/migrations/template.ts b/app/scripts/migrations/template.ts new file mode 100644 index 000000000..e9c70cd47 --- /dev/null +++ b/app/scripts/migrations/template.ts @@ -0,0 +1,28 @@ +import { cloneDeep } from 'lodash'; + +export const version = 0; + +/** + * Explain the purpose of the migration here. + * + * @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist. + * @param originalVersionedData.meta - State metadata. + * @param originalVersionedData.meta.version - The current state version. + * @param originalVersionedData.data - The persisted MetaMask state, keyed by controller. + * @returns Updated versioned MetaMask extension state. + */ +export async function migrate(originalVersionedData: { + meta: { version: number }; + data: Record; +}) { + const versionedData = cloneDeep(originalVersionedData); + versionedData.meta.version = version; + versionedData.data = transformState(versionedData.data); + return versionedData; +} + +function transformState(state: Record) { + const newState = state; + // transform state here + return newState; +}