-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: add migration for profile syncing controller #26004
Merged
Prithpal-Sooriya
merged 8 commits into
develop
from
fix/add-profile-syncing-enabled-migration
Jul 22, 2024
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f95bf2d
fix: add migration for profile syncing controller
Prithpal-Sooriya 724cee1
Merge branch 'develop' of github.com:MetaMask/metamask-extension into…
Prithpal-Sooriya e543fe5
test: add some additional tests for this migration
Prithpal-Sooriya 4c332fd
fix: fix small mistake and also cover other edge cases during the mig…
Prithpal-Sooriya c3d453f
test: update test names
Prithpal-Sooriya 5c08942
chore: update e2e test snapshots
Prithpal-Sooriya 5d46b4d
refactor: handle and capture migration failures due to malformed state
Prithpal-Sooriya 8c483f9
Merge branch 'develop' into fix/add-profile-syncing-enabled-migration
Prithpal-Sooriya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,75 @@ | ||
import { migrate, version } from './120.1'; | ||
|
||
const oldVersion = 120; | ||
|
||
describe('migration #120.1', () => { | ||
afterEach(() => jest.resetAllMocks()); | ||
|
||
it('updates the version metadata', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: {}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.meta).toStrictEqual({ version }); | ||
}); | ||
|
||
it('sets isProfileSyncingEnabled to null', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
UserStorageController: { | ||
isProfileSyncingEnabled: true, | ||
isProfileSyncingUpdateLoading: false, | ||
}, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data.UserStorageController).toStrictEqual({ | ||
isProfileSyncingEnabled: null, | ||
isProfileSyncingUpdateLoading: false, | ||
}); | ||
}); | ||
|
||
it('initializes a default user storage state if it did not exist before', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
OtherController: {}, | ||
}, | ||
}; | ||
|
||
const expectedStorageData = { | ||
OtherController: {}, | ||
UserStorageController: { | ||
isProfileSyncingEnabled: null, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data).toStrictEqual(expectedStorageData); | ||
}); | ||
|
||
it('should do nothing if existing UserStorageController state is malformed', async () => { | ||
Prithpal-Sooriya marked this conversation as resolved.
Show resolved
Hide resolved
Prithpal-Sooriya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const actAssertInvalidUserStorageState = async (state: unknown) => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: { | ||
OtherController: {}, | ||
UserStorageController: state, | ||
}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
expect(newStorage.data).toStrictEqual(oldStorage.data); | ||
}; | ||
|
||
actAssertInvalidUserStorageState('user storage state is not an object'); | ||
actAssertInvalidUserStorageState({ | ||
// missing the isProfileSyncingEnabled field | ||
isProfileSyncingUpdateLoading: false, | ||
}); | ||
}); | ||
}); |
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,52 @@ | ||
import { cloneDeep, isObject } from 'lodash'; | ||
import { hasProperty } from '@metamask/utils'; | ||
Prithpal-Sooriya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import { captureException } from '@sentry/browser'; | ||
Prithpal-Sooriya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type VersionedData = { | ||
meta: { version: number }; | ||
data: Record<string, unknown>; | ||
}; | ||
|
||
export const version = 120.1; | ||
Prithpal-Sooriya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Add a default value for importTime in the InternalAccount | ||
* | ||
* @param originalVersionedData | ||
*/ | ||
export async function migrate( | ||
originalVersionedData: VersionedData, | ||
): Promise<VersionedData> { | ||
const versionedData = cloneDeep(originalVersionedData); | ||
versionedData.meta.version = version; | ||
transformState(versionedData.data); | ||
return versionedData; | ||
} | ||
Prithpal-Sooriya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function transformState( | ||
Prithpal-Sooriya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
state: Record<string, unknown>, | ||
): Record<string, unknown> { | ||
// Existing users who do not have UserStorageController state. | ||
// Provide some initial state & nullify `isProfileSyncingEnabled` | ||
if (!hasProperty(state, 'UserStorageController')) { | ||
Gudahtt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
state.UserStorageController = { | ||
isProfileSyncingEnabled: null, | ||
}; | ||
return state; | ||
} | ||
|
||
if ( | ||
!isObject(state.UserStorageController) || | ||
!hasProperty(state.UserStorageController, 'isProfileSyncingEnabled') | ||
) { | ||
captureException( | ||
`Migration ${version}: Invalid UserStorageController state: ${typeof state.UserStorageController}`, | ||
); | ||
return state; | ||
Prithpal-Sooriya marked this conversation as resolved.
Show resolved
Hide resolved
Prithpal-Sooriya marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// Existing users who do have UserStorageController state. | ||
// nullify `isProfileSyncingEnabled` | ||
state.UserStorageController.isProfileSyncingEnabled = null; | ||
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
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 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checked all references (JS and TS files) it seems okay to change this field to be nullable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not perform strict boolean checks in references, it is truthy checks (
isProfileSyncingEnabled && ...
orisProfileSyncingEnabled || ...
)