-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: rename migration 126 to 121.1 (#26742)
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
1 parent
11f4a57
commit b9173da
Showing
7 changed files
with
156 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters