diff --git a/src/migrations/migrations.js b/src/migrations/migrations.js index ed7fc04d..185798ea 100644 --- a/src/migrations/migrations.js +++ b/src/migrations/migrations.js @@ -18,7 +18,7 @@ const migrations: Migration[] = [ const dbPath = db.getDBPath() const legacyKeys = ['accounts', 'countervalues', 'settings', 'user'] const [accounts, countervalues, settings, user] = await Promise.all( - legacyKeys.map(key => getLegacyData(path.join(dbPath, `${key}.json`))), + legacyKeys.map(key => getFileData(dbPath, key)), ) const appData = { user, settings, accounts, countervalues } await db.setNamespace('app', appData) @@ -28,15 +28,22 @@ const migrations: Migration[] = [ const windowParams = await db.getKey('app', 'settings.window') await db.setKey('app', 'settings.window', undefined) await db.setNamespace('windowParams', windowParams) - await Promise.all(legacyKeys.map(key => fsUnlink(path.join(dbPath, `${key}.json`)))) + await Promise.all( + legacyKeys.map(async key => { + try { + await fsUnlink(path.join(dbPath, `${key}.json`)) + } catch (err) {} // eslint-disable-line + }), + ) }, }, ] -async function getLegacyData(filePath) { +async function getFileData(dbPath, fileName) { + const filePath = path.join(dbPath, `${fileName}.json`) let finalData - const fileContent = await fsReadfile(filePath, 'utf-8') try { + const fileContent = await fsReadfile(filePath, 'utf-8') const { data } = JSON.parse(fileContent) finalData = data } catch (err) { @@ -45,7 +52,8 @@ async function getLegacyData(filePath) { const buf = await fsReadfile(filePath) return buf.toString('base64') } - throw err + // will be stripped down by JSON.stringify + return undefined } return finalData } diff --git a/src/migrations/migrations.spec.js b/src/migrations/migrations.spec.js index 5898cab8..0791326c 100644 --- a/src/migrations/migrations.spec.js +++ b/src/migrations/migrations.spec.js @@ -50,6 +50,41 @@ describe('migration 1', () => { }, }) }) + + test('handle missing file without crash', async () => { + const dir = await extractMock('userdata_v1.0.5_mock-03-missing-file') + let files + files = await fsReaddir(dir) + expect(files).toEqual(['countervalues.json', 'migrations.json', 'settings.json', 'user.json']) + db.init(dir) + let err + try { + await runMigrations() + } catch (e) { + err = e + } + expect(err).toBeUndefined() + files = await fsReaddir(dir) + expect(files).toEqual(['app.json', 'migrations.json', 'windowParams.json']) + }) + + test('handle where app.json is already present', async () => { + const dir = await extractMock('userdata_v1.0.5_mock-04-app-json-present') + let files + files = await fsReaddir(dir) + expect(files).toEqual([ + 'accounts.json', + 'app.json', + 'countervalues.json', + 'migrations.json', + 'settings.json', + 'user.json', + ]) + db.init(dir) + await runMigrations() + files = await fsReaddir(dir) + expect(files).toEqual(['app.json', 'migrations.json', 'windowParams.json']) + }) }) describe('with encryption', () => { diff --git a/src/migrations/mocks/userdata_v1.0.5_mock-03-missing-file.zip b/src/migrations/mocks/userdata_v1.0.5_mock-03-missing-file.zip new file mode 100644 index 00000000..b390d1b0 Binary files /dev/null and b/src/migrations/mocks/userdata_v1.0.5_mock-03-missing-file.zip differ diff --git a/src/migrations/mocks/userdata_v1.0.5_mock-04-app-json-present.zip b/src/migrations/mocks/userdata_v1.0.5_mock-04-app-json-present.zip new file mode 100644 index 00000000..eebdc20c Binary files /dev/null and b/src/migrations/mocks/userdata_v1.0.5_mock-04-app-json-present.zip differ