diff --git a/lib/backup/use-user-data-restore.js b/lib/backup/use-user-data-restore.js --- a/lib/backup/use-user-data-restore.js +++ b/lib/backup/use-user-data-restore.js @@ -7,7 +7,11 @@ import { restoreUserDataStepActionTypes } from '../actions/backup-actions.js'; import { setClientDBStoreActionType } from '../actions/client-db-store-actions.js'; import { generateOpsToEstablishHoldersOnDevice } from '../actions/holder-actions.js'; -import { logTypes, useDebugLogs } from '../components/debug-logs-context.js'; +import { + type AddLogCallback, + logTypes, + useDebugLogs, +} from '../components/debug-logs-context.js'; import { restoreUserDataSteps, type RestoreUserDataStep, @@ -20,6 +24,56 @@ import { useDispatchActionPromise } from '../utils/redux-promise-utils.js'; import { useSelector, useDispatch } from '../utils/redux-utils.js'; +type StoreAndDatabaseVersions = { + +mainDatabaseVersion: number, + +restoredDatabaseVersion: number, + +mainStoreVersion: number, + +restoredStoreVersion: number, +}; + +async function getStoreAndDatabaseVersions( + addLog: AddLogCallback, +): Promise { + const { sqliteAPI } = getConfig(); + + const [ + mainDatabaseVersion, + restoredDatabaseVersion, + restoredStoreVersionString, + ] = await Promise.all([ + sqliteAPI.getDatabaseVersion(databaseIdentifier.MAIN), + sqliteAPI.getDatabaseVersion(databaseIdentifier.RESTORED), + sqliteAPI.getSyncedMetadata( + syncedMetadataNames.STORE_VERSION, + databaseIdentifier.RESTORED, + ), + ]); + + const mainStoreVersion = getConfig().platformDetails.stateVersion; + + if (!mainStoreVersion || !restoredStoreVersionString) { + addLog( + 'User Data Restore', + `Error when restoring user data, main store version(${ + mainStoreVersion ?? 'undefined' + }) or restored store version (${ + restoredStoreVersionString ?? 'undefined' + }) are undefined`, + new Set([logTypes.BACKUP, logTypes.ERROR]), + ); + throw new Error('version_check_failed'); + } + + const restoredStoreVersion = parseInt(restoredStoreVersionString); + + return { + mainDatabaseVersion, + restoredDatabaseVersion, + mainStoreVersion, + restoredStoreVersion, + }; +} + function useUserDataRestore(): ( backupData: QRAuthBackupData, identityAuthResult: IdentityAuthResult, @@ -81,13 +135,12 @@ } // 2. Check database versions and migrate if needed + let storeAndDatabaseVersions: ?StoreAndDatabaseVersions = null; if (startStepIndex <= 1) { const migrateBackupSchemaPromise = (async () => { - const [mainDatabaseVersion, restoredDatabaseVersion] = - await Promise.all([ - sqliteAPI.getDatabaseVersion(databaseIdentifier.MAIN), - sqliteAPI.getDatabaseVersion(databaseIdentifier.RESTORED), - ]); + storeAndDatabaseVersions = await getStoreAndDatabaseVersions(addLog); + const { mainDatabaseVersion, restoredDatabaseVersion } = + storeAndDatabaseVersions; if (mainDatabaseVersion === restoredDatabaseVersion) { addLog( @@ -128,27 +181,14 @@ // 3. Check store versions and migrate if needed if (startStepIndex <= 2) { const runRestoredMigrationsPromise = (async () => { - const mainStoreVersion = getConfig().platformDetails.stateVersion; - const restoredStoreVersionString = await sqliteAPI.getSyncedMetadata( - syncedMetadataNames.STORE_VERSION, - databaseIdentifier.RESTORED, - ); - - if (!mainStoreVersion || !restoredStoreVersionString) { - addLog( - 'User Data Restore', - `Error when restoring user data, main store version(${ - mainStoreVersion ?? 'undefined' - }) or restored store version (${ - restoredStoreVersionString ?? 'undefined' - }) are undefined`, - new Set([logTypes.BACKUP, logTypes.ERROR]), - ); - throw new Error('version_check_failed'); + if (!storeAndDatabaseVersions) { + // There is a chance of restarting this process from this + // condition, in that case versions might be missing + storeAndDatabaseVersions = + await getStoreAndDatabaseVersions(addLog); } - - const restoredStoreVersion = parseInt(restoredStoreVersionString); - + const { mainStoreVersion, restoredStoreVersion } = + storeAndDatabaseVersions; if (mainStoreVersion === restoredStoreVersion) { addLog( 'User Data Restore',