diff --git a/lib/utils/migration-utils.js b/lib/utils/migration-utils.js --- a/lib/utils/migration-utils.js +++ b/lib/utils/migration-utils.js @@ -208,6 +208,7 @@ +[number | string]: (PersistedState) => Promise<{ +state: T, +ops: StoreOperations, + +changesSchema?: boolean, }>, }; @@ -251,7 +252,7 @@ return state; } - return await runMigrations( + const { state: newState } = await runMigrations( legacyMigrations, migrations, state, @@ -260,6 +261,8 @@ debug, handleException, ); + + return newState; }; } @@ -271,7 +274,10 @@ currentVersion: number, debug: boolean, handleException?: (error: Error, state: T) => T, -): Promise> { +): Promise<{ + +state: PersistedState, + +schemaChanged: boolean, +}> { const migrationKeys = [ ...Object.keys(legacyMigrations), ...Object.keys(migrations), @@ -285,6 +291,7 @@ } let migratedState = state; + let schemaChanged = false; for (const versionKey of sortedMigrationKeys) { if (debug) { console.log( @@ -296,39 +303,45 @@ if (!versionKey) { continue; } - - if (legacyMigrations[versionKey]) { - migratedState = await legacyMigrations[versionKey](migratedState); - } else { - const { state: newState, ops } = - await migrations[versionKey](migratedState); - migratedState = newState; - const versionUpdateOp = { - type: 'replace_synced_metadata_entry', - payload: { - name: syncedMetadataNames.DB_VERSION, - data: versionKey.toString(), - }, - }; - const dbOps = { - ...ops, - syncedMetadataStoreOperations: [ - ...(ops.syncedMetadataStoreOperations ?? []), - versionUpdateOp, - ], - }; - try { + try { + if (legacyMigrations[versionKey]) { + migratedState = await legacyMigrations[versionKey](migratedState); + } else { + const { + state: newState, + ops, + changesSchema, + } = await migrations[versionKey](migratedState); + schemaChanged ||= !!changesSchema; + migratedState = newState; + const versionUpdateOp = { + type: 'replace_synced_metadata_entry', + payload: { + name: syncedMetadataNames.DB_VERSION, + data: versionKey.toString(), + }, + }; + const dbOps = { + ...ops, + syncedMetadataStoreOperations: [ + ...(ops.syncedMetadataStoreOperations ?? []), + versionUpdateOp, + ], + }; await getConfig().sqliteAPI.processDBStoreOperations(dbOps); - } catch (exception) { - if (handleException) { - return handleException(exception, state); - } - throw exception; } + } catch (exception) { + if (handleException) { + return { + state: handleException(exception, state), + schemaChanged, + }; + } + throw exception; } } - return migratedState; + return { state: migratedState, schemaChanged }; } export {