Skip to content

Commit 9438441

Browse files
authored
Migrate state to new database name (#50)
This will prevent users from losing their state when updating.
1 parent bd734e3 commit 9438441

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/vs/platform/storage/browser/storageService.ts

+27-10
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,14 @@ export class BrowserStorageService extends AbstractStorageService {
3737
}
3838

3939
private getId(scope: StorageScope): string {
40-
return scope === StorageScope.GLOBAL ? 'global' : this.payload.id;
40+
/**
41+
* Add a unique ID based on the current path for per-workspace databases.
42+
* This prevents workspaces on different machines that share the same domain
43+
* and file path from colliding (since it does not appear IndexedDB can be
44+
* scoped to a path) as long as they are hosted on different paths.
45+
* @author coder
46+
*/
47+
return scope === StorageScope.GLOBAL ? 'global' : (this.payload.id + '-' + hash(location.pathname.toString().replace(/\/$/, "")).toString(16));
4148
}
4249

4350
protected async doInitialize(): Promise<void> {
@@ -76,6 +83,24 @@ export class BrowserStorageService extends AbstractStorageService {
7683
const firstWorkspaceOpen = this.workspaceStorage.getBoolean(IS_NEW_KEY);
7784
if (firstWorkspaceOpen === undefined) {
7885
this.workspaceStorage.set(IS_NEW_KEY, true);
86+
/**
87+
* Migrate the old database.
88+
* @author coder
89+
*/
90+
let db: IIndexedDBStorageDatabase | undefined
91+
try {
92+
db = await IndexedDBStorageDatabase.create({ id: this.payload.id }, this.logService)
93+
const items = await db.getItems()
94+
for (const [key, value] of items) {
95+
this.workspaceStorage.set(key, value);
96+
}
97+
} catch (error) {
98+
this.logService.error(`[IndexedDB Storage ${this.payload.id}] migrate error: ${toErrorMessage(error)}`);
99+
} finally {
100+
if (db) {
101+
db.close()
102+
}
103+
}
79104
} else if (firstWorkspaceOpen) {
80105
this.workspaceStorage.set(IS_NEW_KEY, false);
81106
}
@@ -215,15 +240,7 @@ export class IndexedDBStorageDatabase extends Disposable implements IIndexedDBSt
215240
) {
216241
super();
217242

218-
/**
219-
* Add a unique ID based on the current path. This prevents workspaces on
220-
* different machines that share the same domain and file path from
221-
* colliding (since it does not appear IndexedDB can be scoped to a path) as
222-
* long as they are hosted on different paths.
223-
* @author coder
224-
*/
225-
const windowId = hash(location.pathname.toString()).toString(16);
226-
this.name = `${IndexedDBStorageDatabase.STORAGE_DATABASE_PREFIX}${windowId}-${options.id}`;
243+
this.name = `${IndexedDBStorageDatabase.STORAGE_DATABASE_PREFIX}${options.id}`;
227244
this.broadcastChannel = options.broadcastChanges && ('BroadcastChannel' in window) ? new BroadcastChannel(IndexedDBStorageDatabase.STORAGE_BROADCAST_CHANNEL) : undefined;
228245

229246
this.whenConnected = this.connect();

0 commit comments

Comments
 (0)