Skip to content

Commit

Permalink
fix: rename migration 126 to 121.1 (#26742)
Browse files Browse the repository at this point in the history
This PR renames migration 126 to 121.1. The reason for this renaming is that commit 759b92e is being cherry-picked into version 12.1.1.

Between version 12.1.1 and the develop branch, there are missing migrations. To avoid skipping these migrations in subsequent releases, we need to rename migration 126 to 121.1.
  • Loading branch information
montelaidev authored Aug 29, 2024
1 parent 11f4a57 commit b9173da
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 156 deletions.
69 changes: 69 additions & 0 deletions app/scripts/migrations/121.1.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { AccountsControllerState } from '@metamask/accounts-controller';
import { createMockInternalAccount } from '../../../test/jest/mocks';
import { migrate, version } from './121.1';

const oldVersion = 121;

const mockInternalAccount = createMockInternalAccount();
const mockAccountsControllerState: AccountsControllerState = {
internalAccounts: {
accounts: {
[mockInternalAccount.id]: mockInternalAccount,
},
selectedAccount: mockInternalAccount.id,
},
};

describe('migration #121.1', () => {
afterEach(() => jest.resetAllMocks());

it('updates the version metadata', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
AccountsController: mockAccountsControllerState,
},
};

const newStorage = await migrate(oldStorage);
expect(newStorage.meta).toStrictEqual({ version });
});

it('updates selected account if it is not found in the list of accounts', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
AccountsController: {
...mockAccountsControllerState,
internalAccounts: {
...mockAccountsControllerState.internalAccounts,
selectedAccount: 'unknown id',
},
},
},
};

const newStorage = await migrate(oldStorage);
const {
internalAccounts: { selectedAccount },
} = newStorage.data.AccountsController as AccountsControllerState;
expect(selectedAccount).toStrictEqual(mockInternalAccount.id);
expect(newStorage.data.AccountsController).toStrictEqual(
mockAccountsControllerState,
);
});

it('does nothing if the selectedAccount is found in the list of accounts', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
AccountsController: mockAccountsControllerState,
},
};

const newStorage = await migrate(oldStorage);
expect(newStorage.data.AccountsController).toStrictEqual(
mockAccountsControllerState,
);
});
});
49 changes: 49 additions & 0 deletions app/scripts/migrations/121.1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { AccountsControllerState } from '@metamask/accounts-controller';
import { cloneDeep } from 'lodash';

type VersionedData = {
meta: { version: number };
data: Record<string, unknown>;
};

export const version = 121.1;

/**
* This migration removes depreciated `Txcontroller` key if it is present in state.
*
* @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: VersionedData,
): Promise<VersionedData> {
const versionedData = cloneDeep(originalVersionedData);
versionedData.meta.version = version;
transformState(versionedData.data);
return versionedData;
}

function transformState(state: Record<string, unknown>) {
const accountsControllerState = state?.AccountsController as
| AccountsControllerState
| undefined;

if (
accountsControllerState &&
Object.values(accountsControllerState?.internalAccounts.accounts).length >
0 &&
!accountsControllerState?.internalAccounts.accounts[
accountsControllerState?.internalAccounts.selectedAccount
]
) {
accountsControllerState.internalAccounts.selectedAccount = Object.values(
accountsControllerState?.internalAccounts.accounts,
)[0].id;
}
return state;
}
73 changes: 27 additions & 46 deletions app/scripts/migrations/126.test.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,50 @@
import { AccountsControllerState } from '@metamask/accounts-controller';
import { createMockInternalAccount } from '../../../test/jest/mocks';
import { migrate, version } from './126';

const oldVersion = 125;

const mockInternalAccount = createMockInternalAccount();
const mockAccountsControllerState: AccountsControllerState = {
internalAccounts: {
accounts: {
[mockInternalAccount.id]: mockInternalAccount,
},
selectedAccount: mockInternalAccount.id,
},
};

describe('migration #126', () => {
afterEach(() => jest.resetAllMocks());

describe(`migration #${version}`, () => {
it('updates the version metadata', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
AccountsController: mockAccountsControllerState,
},
data: {},
};

const newStorage = await migrate(oldStorage);

expect(newStorage.meta).toStrictEqual({ version });
});

it('updates selected account if it is not found in the list of accounts', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
AccountsController: {
...mockAccountsControllerState,
internalAccounts: {
...mockAccountsControllerState.internalAccounts,
selectedAccount: 'unknown id',
},
},
it('Does nothing if `providerConfig` is not in the network controller state', async () => {
const oldState = {
NetworkController: {
selectedNetworkClientId: 'mainnet',
},
};

const newStorage = await migrate(oldStorage);
const {
internalAccounts: { selectedAccount },
} = newStorage.data.AccountsController as AccountsControllerState;
expect(selectedAccount).toStrictEqual(mockInternalAccount.id);
expect(newStorage.data.AccountsController).toStrictEqual(
mockAccountsControllerState,
);
const transformedState = await migrate({
meta: { version: oldVersion },
data: oldState,
});

expect(transformedState.data).toStrictEqual(oldState);
});

it('does nothing if the selectedAccount is found in the list of accounts', async () => {
const oldStorage = {
meta: { version: oldVersion },
data: {
AccountsController: mockAccountsControllerState,
it('Removes providerConfig from the network controller state', async () => {
const oldState = {
NetworkController: {
selectedNetworkClientId: 'mainnet',
providerConfig: {
chainId: '0x1',
ticker: 'ETH',
} as object | undefined,
},
};
const transformedState = await migrate({
meta: { version: oldVersion },
data: oldState,
});

const newStorage = await migrate(oldStorage);
expect(newStorage.data.AccountsController).toStrictEqual(
mockAccountsControllerState,
);
delete oldState.NetworkController.providerConfig;
expect(transformedState.data).toStrictEqual(oldState);
});
});
30 changes: 10 additions & 20 deletions app/scripts/migrations/126.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccountsControllerState } from '@metamask/accounts-controller';
import { hasProperty, isObject } from '@metamask/utils';
import { cloneDeep } from 'lodash';

type VersionedData = {
Expand All @@ -9,14 +9,12 @@ type VersionedData = {
export const version = 126;

/**
* This migration removes depreciated `Txcontroller` key if it is present in state.
* This migration removes `providerConfig` from the network controller state.
*
* @param originalVersionedData - Versioned MetaMask extension state, exactly
* what we persist to dist.
* @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.
* @param originalVersionedData.data - The persisted MetaMask state, keyed by controller.
* @returns Updated versioned MetaMask extension state.
*/
export async function migrate(
Expand All @@ -28,22 +26,14 @@ export async function migrate(
return versionedData;
}

function transformState(state: Record<string, unknown>) {
const accountsControllerState = state?.AccountsController as
| AccountsControllerState
| undefined;

function transformState(
state: Record<string, unknown>,
): Record<string, unknown> {
if (
accountsControllerState &&
Object.values(accountsControllerState?.internalAccounts.accounts).length >
0 &&
!accountsControllerState?.internalAccounts.accounts[
accountsControllerState?.internalAccounts.selectedAccount
]
hasProperty(state, 'NetworkController') &&
isObject(state.NetworkController)
) {
accountsControllerState.internalAccounts.selectedAccount = Object.values(
accountsControllerState?.internalAccounts.accounts,
)[0].id;
delete state.NetworkController.providerConfig;
}
return state;
}
50 changes: 0 additions & 50 deletions app/scripts/migrations/127.test.ts

This file was deleted.

39 changes: 0 additions & 39 deletions app/scripts/migrations/127.ts

This file was deleted.

2 changes: 1 addition & 1 deletion app/scripts/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ const migrations = [
require('./120.5'),
require('./120.6'),
require('./121'),
require('./121.1'),
require('./122'),
require('./123'),
require('./124'),
require('./125'),
require('./126'),
require('./127'),
];

export default migrations;

0 comments on commit b9173da

Please sign in to comment.