Skip to content

Commit f1247a6

Browse files
committed
fix: settings schema migration
1 parent b0b289d commit f1247a6

File tree

3 files changed

+59
-56
lines changed

3 files changed

+59
-56
lines changed

src/entrypoints/background/listeners.ts

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,9 @@ import { onMessage } from "webext-bridge/background";
44
import { APP_CONFIG } from "@/app.config";
55
import { ExtensionLocalStorageService } from "@/services/extension-local-storage";
66
import { ExtensionLocalStorageApi } from "@/services/extension-local-storage/extension-local-storage-api";
7-
import {
8-
ExtensionLocalStorage,
9-
ExtensionLocalStorageSchema,
10-
} from "@/services/extension-local-storage/extension-local-storage.types";
117
import { hasRequiredPermissions } from "@/services/pplx-theme-preloader";
12-
import { errorWrapper } from "@/utils/error-wrapper";
138
import { ExtensionVersion } from "@/utils/ext-version";
149
import { getThemeCss } from "@/utils/pplx-theme-loader-utils";
15-
import { EXT_UPDATE_MIGRATIONS } from "@/utils/update-migrations";
1610
import { getOptionsPageUrl } from "@/utils/utils";
1711

1812
export type BackgroundEvents = {
@@ -27,8 +21,6 @@ export function setupBackgroundListeners() {
2721

2822
onboardingFlowTrigger();
2923

30-
updateMigrations();
31-
3224
invalidateCdnCache();
3325

3426
createDashboardShortcut();
@@ -137,42 +129,3 @@ function extensionIconActionListener() {
137129
else chrome.runtime.openOptionsPage();
138130
});
139131
}
140-
141-
function updateMigrations() {
142-
chrome.runtime.onInstalled.addListener(
143-
async ({ reason, previousVersion }) => {
144-
if (reason !== chrome.runtime.OnInstalledReason.UPDATE) return;
145-
146-
if (!previousVersion) return;
147-
148-
console.log("Upgraded from", previousVersion, "to", APP_CONFIG.VERSION);
149-
150-
const migrations = Object.entries(EXT_UPDATE_MIGRATIONS);
151-
152-
let migratedSettings: ExtensionLocalStorage | null = null;
153-
154-
for (const [version, migrationFns] of migrations) {
155-
if (new ExtensionVersion(version).isNewerThan(previousVersion)) {
156-
for (const migrationFn of migrationFns) {
157-
const oldRawSettings =
158-
migratedSettings ?? (await ExtensionLocalStorageApi.get());
159-
const [newSettings, error] = await errorWrapper(
160-
(): Promise<ExtensionLocalStorage> =>
161-
migrationFn({ oldRawSettings }),
162-
)();
163-
164-
if (error) continue;
165-
166-
migratedSettings = newSettings;
167-
}
168-
}
169-
}
170-
171-
if (migratedSettings) {
172-
ExtensionLocalStorageApi.set(
173-
ExtensionLocalStorageSchema.parse(migratedSettings),
174-
);
175-
}
176-
},
177-
);
178-
}

src/services/extension-local-storage/index.ts

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { produce } from "immer";
22

3+
import { APP_CONFIG } from "@/app.config";
34
import { ExtensionLocalStorageApi } from "@/services/extension-local-storage/extension-local-storage-api";
45
import {
56
ExtensionLocalStorageSchema,
@@ -16,7 +17,10 @@ import {
1617
} from "@/services/extension-local-storage/utils";
1718
import { isZodError } from "@/types/utils.types";
1819
import { csLoaderRegistry } from "@/utils/cs-loader-registry";
20+
import { errorWrapper } from "@/utils/error-wrapper";
21+
import { ExtensionVersion } from "@/utils/ext-version";
1922
import { queryClient } from "@/utils/ts-query-client";
23+
import { EXT_UPDATE_MIGRATIONS } from "@/utils/update-migrations";
2024
import { isInContentScript, whereAmI } from "@/utils/utils";
2125
import packageJson from "~/package.json";
2226

@@ -119,16 +123,24 @@ async function mergeData(
119123
return DEFAULT_STORAGE;
120124
}
121125

122-
console.log("[Cplx] Settings schema mismatch, merging with defaults...");
126+
console.log("[Cplx] Settings schema mismatch");
123127

124-
const cleanSettings = error.issues.reduce(
125-
(settings, issue) =>
126-
setPathToUndefined({
127-
paths: issue.path as string[],
128-
obj: settings,
129-
}) as ExtensionLocalStorage,
130-
rawSettings,
131-
);
128+
if (error.issues.some((issue) => issue.path[0] === "schemaVersion")) {
129+
return mergeData(
130+
(await updateMigrations({
131+
previousVersion: rawSettings.schemaVersion,
132+
rawSettings,
133+
})) ?? rawSettings,
134+
DEFAULT_STORAGE,
135+
);
136+
}
137+
138+
const cleanSettings = error.issues.reduce((settings, issue) => {
139+
return setPathToUndefined({
140+
paths: issue.path as string[],
141+
obj: settings,
142+
}) as ExtensionLocalStorage;
143+
}, rawSettings);
132144

133145
const updatedSettings = {
134146
...mergeUndefined({
@@ -155,3 +167,39 @@ csLoaderRegistry.register({
155167
await ExtensionLocalStorageService.get();
156168
},
157169
});
170+
171+
async function updateMigrations({
172+
previousVersion,
173+
rawSettings,
174+
}: {
175+
previousVersion: string;
176+
rawSettings: ExtensionLocalStorage;
177+
}) {
178+
if (!previousVersion) return;
179+
180+
console.log("Migrate schema from", previousVersion, "to", APP_CONFIG.VERSION);
181+
182+
const migrations = Object.entries(EXT_UPDATE_MIGRATIONS);
183+
184+
let migratedSettings: ExtensionLocalStorage = rawSettings;
185+
186+
for (const [version, migrationFns] of migrations) {
187+
if (new ExtensionVersion(version).isNewerThan(previousVersion)) {
188+
for (const migrationFn of migrationFns) {
189+
const oldRawSettings = migratedSettings ?? rawSettings;
190+
const [newSettings, error] = await errorWrapper(
191+
(): Promise<ExtensionLocalStorage> => migrationFn({ oldRawSettings }),
192+
)();
193+
194+
if (error || !newSettings) continue;
195+
196+
migratedSettings = newSettings;
197+
}
198+
}
199+
}
200+
201+
return {
202+
...migratedSettings,
203+
schemaVersion: packageJson.version,
204+
};
205+
}

src/utils/update-migrations.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ type MigrationFn = ({
1111
export const enableThreadMessageTtsKey: MigrationFn = async ({
1212
oldRawSettings,
1313
}) => {
14+
console.log("[ExtUpdateMigrations] Enable Thread Message TTS Key");
15+
1416
return produce(oldRawSettings, (draft) => {
1517
draft.plugins["thread:messageTts"] = {
1618
enabled: true,

0 commit comments

Comments
 (0)