Browse Source

Improve robustness of first migration

master
meriadec 7 years ago
committed by Gaëtan Renaudeau
parent
commit
1a57c186aa
  1. 18
      src/migrations/migrations.js
  2. 35
      src/migrations/migrations.spec.js
  3. BIN
      src/migrations/mocks/userdata_v1.0.5_mock-03-missing-file.zip
  4. BIN
      src/migrations/mocks/userdata_v1.0.5_mock-04-app-json-present.zip

18
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
}

35
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', () => {

BIN
src/migrations/mocks/userdata_v1.0.5_mock-03-missing-file.zip

Binary file not shown.

BIN
src/migrations/mocks/userdata_v1.0.5_mock-04-app-json-present.zip

Binary file not shown.
Loading…
Cancel
Save