This commit is contained in:
Manav Rathi 2024-04-18 19:59:06 +05:30
parent 14285b66ae
commit ca36b3c750
No known key found for this signature in database
3 changed files with 49 additions and 5 deletions

View file

@ -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.

View file

@ -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);

View file

@ -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<WatchStore> = {
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");
}
};