2020-01-09 04:34:58 +01:00
|
|
|
import fs from 'fs'
|
|
|
|
import assert from 'assert'
|
|
|
|
import clone from 'clone'
|
|
|
|
import pify from 'pify'
|
|
|
|
import Migrator from '../../../app/scripts/lib/migrator'
|
|
|
|
import liveMigrations from '../../../app/scripts/migrations'
|
|
|
|
|
2018-04-06 02:49:50 +02:00
|
|
|
const stubMigrations = [
|
2017-05-11 19:47:58 +02:00
|
|
|
{
|
|
|
|
version: 1,
|
|
|
|
migrate: (data) => {
|
|
|
|
// clone the data just like we do in migrations
|
|
|
|
const clonedData = clone(data)
|
|
|
|
clonedData.meta.version = 1
|
|
|
|
return Promise.resolve(clonedData)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
version: 2,
|
|
|
|
migrate: (data) => {
|
|
|
|
const clonedData = clone(data)
|
|
|
|
clonedData.meta.version = 2
|
|
|
|
return Promise.resolve(clonedData)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
version: 3,
|
|
|
|
migrate: (data) => {
|
|
|
|
const clonedData = clone(data)
|
|
|
|
clonedData.meta.version = 3
|
|
|
|
return Promise.resolve(clonedData)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]
|
2019-12-03 21:50:55 +01:00
|
|
|
const versionedData = { meta: { version: 0 }, data: { hello: 'world' } }
|
2018-04-06 02:49:50 +02:00
|
|
|
|
2020-01-09 04:34:58 +01:00
|
|
|
import data from '../../../app/scripts/first-time-state'
|
|
|
|
|
2018-04-06 02:49:50 +02:00
|
|
|
const firstTimeState = {
|
|
|
|
meta: { version: 0 },
|
2020-01-09 04:34:58 +01:00
|
|
|
data,
|
2018-04-06 02:49:50 +02:00
|
|
|
}
|
2019-12-13 01:14:29 +01:00
|
|
|
describe('liveMigrations require list', () => {
|
|
|
|
it('should include all the migrations', async () => {
|
|
|
|
const fileNames = await pify(cb => fs.readdir('./app/scripts/migrations/', cb))()
|
|
|
|
const migrationNumbers = fileNames.reduce((agg, filename) => {
|
|
|
|
const name = filename.split('.')[0]
|
|
|
|
if (/^\d+$/.test(name)) {
|
|
|
|
agg.push(name)
|
|
|
|
}
|
|
|
|
return agg
|
|
|
|
}, []).map((num) => parseInt(num))
|
|
|
|
|
|
|
|
migrationNumbers.forEach((num) => {
|
|
|
|
const migration = liveMigrations.find((m) => m.version === num)
|
|
|
|
assert(migration, `migration should be include in the index missing migration ${num}`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2018-04-06 02:49:50 +02:00
|
|
|
|
2018-04-06 03:05:03 +02:00
|
|
|
describe('Migrator', () => {
|
2018-04-06 02:49:50 +02:00
|
|
|
const migrator = new Migrator({ migrations: stubMigrations })
|
2017-05-11 19:47:58 +02:00
|
|
|
it('migratedData version should be version 3', (done) => {
|
|
|
|
migrator.migrateData(versionedData)
|
2019-07-31 22:17:11 +02:00
|
|
|
.then((migratedData) => {
|
|
|
|
assert.equal(migratedData.meta.version, stubMigrations[2].version)
|
|
|
|
done()
|
|
|
|
}).catch(done)
|
2017-05-11 19:47:58 +02:00
|
|
|
})
|
2018-04-06 02:49:50 +02:00
|
|
|
|
|
|
|
it('should match the last version in live migrations', (done) => {
|
|
|
|
const migrator = new Migrator({ migrations: liveMigrations })
|
|
|
|
migrator.migrateData(firstTimeState)
|
2019-07-31 22:17:11 +02:00
|
|
|
.then((migratedData) => {
|
|
|
|
const last = liveMigrations.length - 1
|
|
|
|
assert.equal(migratedData.meta.version, liveMigrations[last].version)
|
|
|
|
done()
|
|
|
|
}).catch(done)
|
2018-04-06 02:49:50 +02:00
|
|
|
})
|
|
|
|
|
2018-04-06 04:28:53 +02:00
|
|
|
it('should emit an error', function (done) {
|
|
|
|
this.timeout(15000)
|
2019-11-20 01:03:20 +01:00
|
|
|
const migrator = new Migrator({ migrations: [{ version: 1, migrate: async () => {
|
|
|
|
throw new Error('test')
|
|
|
|
} } ] })
|
2018-04-06 04:28:53 +02:00
|
|
|
migrator.on('error', () => done())
|
2019-12-03 21:50:55 +01:00
|
|
|
migrator.migrateData({ meta: { version: 0 } })
|
2019-07-31 22:17:11 +02:00
|
|
|
.then(() => {
|
|
|
|
}).catch(done)
|
2018-04-06 04:28:53 +02:00
|
|
|
})
|
|
|
|
|
2017-05-11 19:47:58 +02:00
|
|
|
})
|