Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion packages/storage/src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,35 @@ describe('Storage Utils', () => {
expect(migrateToV3).toBeCalledWith(4);
});

it('should not run migrations if initialized on a version greater than 1', async () => {
const migrateToV2 = vi.fn((oldValue) => oldValue * 2);

const item = storage.defineItem<number, { v: number }>(`local:count`, {
defaultValue: 0,
version: 2,
migrations: {
2: migrateToV2,
},
});
Comment on lines +712 to +718
Copy link
Member

@aklinker1 aklinker1 Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test does not use init when defining the item... doesn't seem like it's testing the PR's change? Or am I missing something and still don't understand what's going on here?

Copy link
Contributor Author

@42willow 42willow Dec 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested this test and it fails on main.

I made the change inside getOrInitValue, which runs after the migration is finished. Migrate is called by setValue (i think?)

await migrationsDone;
but for some reason this doesn't happen in the test environment, so I added the migration call manually.

await item.migrate();
await waitForMigrations();

// Initialize the value once migrations have finished
migrationsDone.then(getOrInitValue);

When I first found this bug I wasn't actually using the storageItem.init() method but was using storage item fallbacks and setValue.

I may be misunderstanding something though as I don't really know what I'm doing :)

// runs getOrInitValue, sets meta
await item.migrate();
await waitForMigrations();

await item.setValue(1);

// should do nothing
await item.migrate();
await waitForMigrations();

const actualValue = await item.getValue();
const actualMeta = await item.getMeta();

expect(actualValue).toEqual(1);
expect(actualMeta).toEqual({ v: 2 });

expect(migrateToV2).not.toBeCalled();
});

it('should call onMigrationComplete callback function if defined', async () => {
await fakeBrowser.storage.local.set({
count: 2,
Expand Down Expand Up @@ -748,7 +777,7 @@ describe('Storage Utils', () => {
const actualMeta = await item.getMeta();

expect(actualValue).toEqual(0);
expect(actualMeta).toEqual({});
expect(actualMeta).toEqual({ v: 3 });

expect(migrateToV2).not.toBeCalled();
expect(migrateToV3).not.toBeCalled();
Expand Down
3 changes: 3 additions & 0 deletions packages/storage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ function createStorage(): WxtStorage {
const getOrInitValue = () =>
initMutex.runExclusive(async () => {
const value = await driver.getItem<any>(driverKey);
if (value == null && targetVersion > 1)
await setMeta(driver, driverKey, { v: targetVersion });

// Don't init value if it already exists or the init function isn't provided
if (value != null || opts?.init == null) return value;

Expand Down