@@ -37,7 +37,14 @@ export class BrowserStorageService extends AbstractStorageService {
37
37
}
38
38
39
39
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 ) ) ;
41
48
}
42
49
43
50
protected async doInitialize ( ) : Promise < void > {
@@ -76,6 +83,24 @@ export class BrowserStorageService extends AbstractStorageService {
76
83
const firstWorkspaceOpen = this . workspaceStorage . getBoolean ( IS_NEW_KEY ) ;
77
84
if ( firstWorkspaceOpen === undefined ) {
78
85
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
+ }
79
104
} else if ( firstWorkspaceOpen ) {
80
105
this . workspaceStorage . set ( IS_NEW_KEY , false ) ;
81
106
}
@@ -215,15 +240,7 @@ export class IndexedDBStorageDatabase extends Disposable implements IIndexedDBSt
215
240
) {
216
241
super ( ) ;
217
242
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 } ` ;
227
244
this . broadcastChannel = options . broadcastChanges && ( 'BroadcastChannel' in window ) ? new BroadcastChannel ( IndexedDBStorageDatabase . STORAGE_BROADCAST_CHANNEL ) : undefined ;
228
245
229
246
this . whenConnected = this . connect ( ) ;
0 commit comments