diff --git a/desktop/src/main.ts b/desktop/src/main.ts index c2a25a25c..467d9c881 100644 --- a/desktop/src/main.ts +++ b/desktop/src/main.ts @@ -27,6 +27,7 @@ import { setupAutoUpdater } from "./main/services/app-update"; import autoLauncher from "./main/services/auto-launcher"; import { createWatcher } from "./main/services/watch"; import { userPreferences } from "./main/stores/user-preferences"; +import { migrateLegacyWatchStoreIfNeeded } from "./main/stores/watch"; import { registerStreamProtocol } from "./main/stream"; import { isDev } from "./main/util"; @@ -324,6 +325,7 @@ const main = () => { setupRendererServer(); registerPrivilegedSchemes(); increaseDiskCache(); + migrateLegacyWatchStoreIfNeeded(); app.on("second-instance", () => { // Someone tried to run a second instance, we should focus our window. diff --git a/desktop/src/main/services/watch.ts b/desktop/src/main/services/watch.ts index a5cfe58d3..d0c554ec7 100644 --- a/desktop/src/main/services/watch.ts +++ b/desktop/src/main/services/watch.ts @@ -56,6 +56,26 @@ export const watchGet = () => { return folderWatches(); }; +const folderWatches = () => { + let watches = watchStore.get("mappings") ?? []; + + // Previous versions of the store used to store an integer to indicate the + // collection mapping, migrate these to the new schema if we see them still. + let needsUpdate = false; + watches = watches.map((watch) => { + const cm = watch.collectionMapping; + if (cm != "root" && cm != "parent") { + const uploadStrategy = watch.uploadStrategy; + const collectionMapping = uploadStrategy == 1 ? "parent" : "root"; + needsUpdate = true; + return { ...watch, collectionMapping } + } + }) + if (watches.length && watches) + return mappings; +}; + + export const watchAdd = async ( watcher: FSWatcher, folderPath: string, @@ -139,10 +159,6 @@ export function updateWatchMappingIgnoredFiles( setWatchMappings(watchMappings); } -const folderWatches = () => { - const mappings = watchStore.get("mappings") ?? []; - return mappings; -}; function setWatchMappings(watchMappings: WatchStoreType["mappings"]) { watchStore.set("mappings", watchMappings); diff --git a/desktop/src/main/stores/watch.ts b/desktop/src/main/stores/watch.ts index ee077bbcd..df4ad4b7b 100644 --- a/desktop/src/main/stores/watch.ts +++ b/desktop/src/main/stores/watch.ts @@ -1,10 +1,16 @@ import Store, { Schema } from "electron-store"; import { type FolderWatch } from "../../types/ipc"; +import log from "../log"; interface WatchStore { - mappings: FolderWatch[]; + mappings: FolderWatchWithLegacyFields[]; } +type FolderWatchWithLegacyFields = FolderWatch & { + /** @deprecated Only retained for migration, do not use in other code */ + uploadStrategy: number; +}; + const watchStoreSchema: Schema = { mappings: { type: "array", @@ -39,3 +45,23 @@ export const watchStore = new Store({ name: "watch-status", schema: watchStoreSchema, }); + +/** + * Previous versions of the store used to store an integer to indicate the + * collection mapping, migrate these to the new schema if we encounter them. + */ +export const migrateLegacyWatchStoreIfNeeded = () => { + let needsUpdate = false; + const watches = watchStore.get("mappings")?.map((watch) => { + let collectionMapping = watch.collectionMapping; + if (!collectionMapping) { + collectionMapping = watch.uploadStrategy == 1 ? "parent" : "root"; + needsUpdate = true; + } + return { ...watch, collectionMapping }; + }); + if (needsUpdate) { + watchStore.set("mappings", watches); + log.info("Migrated legacy watch store data to new schema"); + } +};