1
1
import { produce } from "immer" ;
2
2
3
+ import { APP_CONFIG } from "@/app.config" ;
3
4
import { ExtensionLocalStorageApi } from "@/services/extension-local-storage/extension-local-storage-api" ;
4
5
import {
5
6
ExtensionLocalStorageSchema ,
@@ -16,7 +17,10 @@ import {
16
17
} from "@/services/extension-local-storage/utils" ;
17
18
import { isZodError } from "@/types/utils.types" ;
18
19
import { csLoaderRegistry } from "@/utils/cs-loader-registry" ;
20
+ import { errorWrapper } from "@/utils/error-wrapper" ;
21
+ import { ExtensionVersion } from "@/utils/ext-version" ;
19
22
import { queryClient } from "@/utils/ts-query-client" ;
23
+ import { EXT_UPDATE_MIGRATIONS } from "@/utils/update-migrations" ;
20
24
import { isInContentScript , whereAmI } from "@/utils/utils" ;
21
25
import packageJson from "~/package.json" ;
22
26
@@ -119,16 +123,24 @@ async function mergeData(
119
123
return DEFAULT_STORAGE ;
120
124
}
121
125
122
- console . log ( "[Cplx] Settings schema mismatch, merging with defaults... " ) ;
126
+ console . log ( "[Cplx] Settings schema mismatch" ) ;
123
127
124
- const cleanSettings = error . issues . reduce (
125
- ( settings , issue ) =>
126
- setPathToUndefined ( {
127
- paths : issue . path as string [ ] ,
128
- obj : settings ,
129
- } ) as ExtensionLocalStorage ,
130
- rawSettings ,
131
- ) ;
128
+ if ( error . issues . some ( ( issue ) => issue . path [ 0 ] === "schemaVersion" ) ) {
129
+ return mergeData (
130
+ ( await updateMigrations ( {
131
+ previousVersion : rawSettings . schemaVersion ,
132
+ rawSettings,
133
+ } ) ) ?? rawSettings ,
134
+ DEFAULT_STORAGE ,
135
+ ) ;
136
+ }
137
+
138
+ const cleanSettings = error . issues . reduce ( ( settings , issue ) => {
139
+ return setPathToUndefined ( {
140
+ paths : issue . path as string [ ] ,
141
+ obj : settings ,
142
+ } ) as ExtensionLocalStorage ;
143
+ } , rawSettings ) ;
132
144
133
145
const updatedSettings = {
134
146
...mergeUndefined ( {
@@ -155,3 +167,39 @@ csLoaderRegistry.register({
155
167
await ExtensionLocalStorageService . get ( ) ;
156
168
} ,
157
169
} ) ;
170
+
171
+ async function updateMigrations ( {
172
+ previousVersion,
173
+ rawSettings,
174
+ } : {
175
+ previousVersion : string ;
176
+ rawSettings : ExtensionLocalStorage ;
177
+ } ) {
178
+ if ( ! previousVersion ) return ;
179
+
180
+ console . log ( "Migrate schema from" , previousVersion , "to" , APP_CONFIG . VERSION ) ;
181
+
182
+ const migrations = Object . entries ( EXT_UPDATE_MIGRATIONS ) ;
183
+
184
+ let migratedSettings : ExtensionLocalStorage = rawSettings ;
185
+
186
+ for ( const [ version , migrationFns ] of migrations ) {
187
+ if ( new ExtensionVersion ( version ) . isNewerThan ( previousVersion ) ) {
188
+ for ( const migrationFn of migrationFns ) {
189
+ const oldRawSettings = migratedSettings ?? rawSettings ;
190
+ const [ newSettings , error ] = await errorWrapper (
191
+ ( ) : Promise < ExtensionLocalStorage > => migrationFn ( { oldRawSettings } ) ,
192
+ ) ( ) ;
193
+
194
+ if ( error || ! newSettings ) continue ;
195
+
196
+ migratedSettings = newSettings ;
197
+ }
198
+ }
199
+ }
200
+
201
+ return {
202
+ ...migratedSettings ,
203
+ schemaVersion : packageJson . version ,
204
+ } ;
205
+ }
0 commit comments