diff --git a/packages/storage/src/__tests__/index.test.ts b/packages/storage/src/__tests__/index.test.ts index 1ba64e3cd..6501a1ea3 100644 --- a/packages/storage/src/__tests__/index.test.ts +++ b/packages/storage/src/__tests__/index.test.ts @@ -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(`local:count`, { + defaultValue: 0, + version: 2, + migrations: { + 2: migrateToV2, + }, + }); + // 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, @@ -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(); diff --git a/packages/storage/src/index.ts b/packages/storage/src/index.ts index b629ec4de..428f6e370 100644 --- a/packages/storage/src/index.ts +++ b/packages/storage/src/index.ts @@ -436,6 +436,9 @@ function createStorage(): WxtStorage { const getOrInitValue = () => initMutex.runExclusive(async () => { const value = await driver.getItem(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;