Skip to content

Commit db72b9d

Browse files
committed
Automatically drop cache databases from older versions when upgrading
1 parent 7e6cf09 commit db72b9d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ export default class QuartzSyncer extends Plugin {
416416

417417
await this.saveSettings();
418418
await this.datastore.persister.clear();
419+
await this.datastore.dropOutdatedCache();
419420
}
420421
}
421422
}

src/publishFile/DataStore.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,46 @@ export class DataStore {
5555
*/
5656
public async recreate() {
5757
await localforage.dropInstance({
58-
name: "quartz-syncer/cache/" + this.appId,
58+
name: `quartz-syncer/cache/${this.vaultName}/${this.appId}/${this.version}`,
5959
});
6060

61+
await this.dropOutdatedCache();
62+
6163
this.persister = localforage.createInstance({
62-
name: "quartz-syncer/cache/" + this.appId,
64+
name: `quartz-syncer/cache/${this.vaultName}/${this.appId}/${this.version}`,
6365
driver: [localforage.INDEXEDDB],
6466
description:
6567
"Cache metadata about files and sections in the quartz syncer index.",
6668
});
6769
}
6870

71+
/**
72+
* Drop outdated cache instance. This is used to clear the cache when the version changes.
73+
*
74+
* returns A promise that resolves when the cache is dropped.
75+
*/
76+
public async dropOutdatedCache(): Promise<void> {
77+
// Get all IndexedDB instances
78+
const instances = await indexedDB.databases();
79+
80+
// Filter instances that match the current vault and app ID
81+
const matchingInstances = instances.filter(
82+
(instance) =>
83+
instance.name &&
84+
instance.name.startsWith(
85+
`quartz-syncer/cache/${this.vaultName}/${this.appId}/`,
86+
) &&
87+
instance.name !==
88+
`quartz-syncer/cache/${this.vaultName}/${this.appId}/${this.version}`, // Exclude the current version
89+
);
90+
91+
// Drop each matching instance
92+
for (const instance of matchingInstances) {
93+
// instance.name is guaranteed to be non-null due to the filter above
94+
indexedDB.deleteDatabase(instance.name!);
95+
}
96+
}
97+
6998
/**
7099
* Check if a local file is outdated compared to the given timestamp and version.
71100
*

0 commit comments

Comments
 (0)