-
-
Notifications
You must be signed in to change notification settings - Fork 500
fix(storage): migrations incorrectly running #2130
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -706,16 +706,18 @@ describe('Storage Utils', () => { | |
| expect(migrateToV3).toBeCalledWith(4); | ||
| }); | ||
|
|
||
| it('should set the version without running migrations for empty storage items', async () => { | ||
| it('should not run migrations on initialisation', async () => { | ||
| const migrate = vi.fn((n: number) => n * 2); | ||
|
|
||
| const item = storage.defineItem<number>('local:key', { | ||
| init: () => 1, | ||
| let item = storage.defineItem<number>('local:key', { | ||
| version: 2, | ||
| migrations: { | ||
| 2: migrate, | ||
| }, | ||
| }); | ||
| await item.setValue(1); | ||
| await item.migrate(); | ||
|
|
||
| const value = await item.getValue(); | ||
| const meta = await item.getMeta(); | ||
|
|
||
|
|
@@ -789,7 +791,7 @@ describe('Storage Utils', () => { | |
| const actualMeta = await item.getMeta(); | ||
|
|
||
| expect(actualValue).toEqual(0); | ||
| expect(actualMeta).toEqual({}); | ||
| expect(actualMeta).toEqual({ v: 3 }); | ||
|
Comment on lines
-792
to
+794
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Edit: Ignore this comment, I didn't read the new issue and missed a few comments on the PR. I understand the problem better now and am considering the PR again DetailsThis test is was correct -
That means Here's an example: // v1
const blockedUrls = defineStorageItem<string[]>("local:blockedUrls", {
defaultValue: [],
})
// v2
const blockedUrls = defineStorageItem<{ id: string, url: string }>("local:blockedUrls", {
defaultValue: [],
version: 2,
migrations: {
2: (v1) => v1.map((url, i) => ({ id: String(i), url })),
},
})If a user never updates this storage item, why do we need to track that a blank array is v1 or v2? Either way, it works. But not adding meta will mean storage is less poluted and takes up less space. Hence why we don't set the version for default values.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, with more context, I still believe that this test was valid... I'd prefer to not add a version for a That said, if there's not a better place to put the new logic, we may have to accept this behavioral change. |
||
|
|
||
| expect(migrateToV2).not.toBeCalled(); | ||
| expect(migrateToV3).not.toBeCalled(); | ||
|
|
@@ -899,17 +901,19 @@ describe('Storage Utils', () => { | |
| }); | ||
|
|
||
| it('should handle errors in migration functions', async () => { | ||
| const key = 'local:key'; | ||
| await storage.setItem(key, 1); | ||
|
|
||
| const cause = Error('Some error'); | ||
| const expectedError = new MigrationError('local:key', 2, { cause }); | ||
| const item = storage.defineItem<number>('local:key', { | ||
| const expectedError = new MigrationError(key, 2, { cause }); | ||
| const item = storage.defineItem<number>(key, { | ||
| version: 3, | ||
| migrations: { | ||
| 2: () => { | ||
| throw cause; | ||
| }, | ||
| }, | ||
| }); | ||
| await fakeBrowser.storage.local.set({ key: 1, key$: { v: 1 } }); | ||
|
|
||
| await expect(item.migrate()).rejects.toThrow(expectedError); | ||
| }); | ||
|
Comment on lines
903
to
919
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once again, I beileve the previous test is still valid. Add a new one instead. |
||
|
|
||
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.
This test is still valid I think. Can you create a new test instead of editing this one?
Though I think the test name should have " with init defined".