2020-01-09 04:34:58 +01:00
|
|
|
import fs from 'fs'
|
|
|
|
import assert from 'assert'
|
2020-01-29 18:14:33 +01:00
|
|
|
import { cloneDeep } from 'lodash'
|
2020-01-09 04:34:58 +01:00
|
|
|
import Migrator from '../../../app/scripts/lib/migrator'
|
|
|
|
import liveMigrations from '../../../app/scripts/migrations'
|
2020-08-18 21:24:47 +02:00
|
|
|
import data from '../../../app/scripts/first-time-state'
|
2020-01-09 04:34:58 +01:00
|
|
|
|
2018-04-06 02:49:50 +02:00
|
|
|
const stubMigrations = [
|
2017-05-11 19:47:58 +02:00
|
|
|
{
|
|
|
|
version: 1,
|
2020-08-18 21:24:47 +02:00
|
|
|
migrate: (state) => {
|
2017-05-11 19:47:58 +02:00
|
|
|
// clone the data just like we do in migrations
|
2020-08-18 21:24:47 +02:00
|
|
|
const clonedData = cloneDeep(state)
|
2017-05-11 19:47:58 +02:00
|
|
|
clonedData.meta.version = 1
|
|
|
|
return Promise.resolve(clonedData)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
version: 2,
|
2020-08-18 21:24:47 +02:00
|
|
|
migrate: (state) => {
|
|
|
|
const clonedData = cloneDeep(state)
|
2017-05-11 19:47:58 +02:00
|
|
|
clonedData.meta.version = 2
|
|
|
|
return Promise.resolve(clonedData)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
version: 3,
|
2020-08-18 21:24:47 +02:00
|
|
|
migrate: (state) => {
|
|
|
|
const clonedData = cloneDeep(state)
|
2017-05-11 19:47:58 +02:00
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('migrations', function () {
|
|
|
|
describe('liveMigrations require list', function () {
|
|
|
|
|
2020-02-27 05:49:59 +01:00
|
|
|
let migrationNumbers
|
|
|
|
|
|
|
|
before(function () {
|
|
|
|
const fileNames = fs.readdirSync('./app/scripts/migrations/')
|
|
|
|
migrationNumbers = fileNames
|
|
|
|
.reduce((acc, filename) => {
|
|
|
|
const name = filename.split('.')[0]
|
2020-08-19 18:27:05 +02:00
|
|
|
if ((/^\d+$/u).test(name)) {
|
2020-02-27 05:49:59 +01:00
|
|
|
acc.push(name)
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, [])
|
2020-08-18 21:08:22 +02:00
|
|
|
.map((num) => parseInt(num, 10))
|
2020-02-27 05:49:59 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
it('should include all migrations', function () {
|
2020-02-11 17:51:13 +01:00
|
|
|
migrationNumbers.forEach((num) => {
|
|
|
|
const migration = liveMigrations.find((m) => m.version === num)
|
2020-02-27 05:49:59 +01:00
|
|
|
assert(migration, `migration not included in 'migrations/index.js': ${num}`)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should have tests for all migrations', function () {
|
|
|
|
const fileNames = fs.readdirSync('./test/unit/migrations/')
|
|
|
|
const testNumbers = fileNames
|
|
|
|
.reduce((acc, filename) => {
|
|
|
|
const name = filename.split('-test.')[0]
|
2020-08-19 18:27:05 +02:00
|
|
|
if ((/^\d+$/u).test(name)) {
|
2020-02-27 05:49:59 +01:00
|
|
|
acc.push(name)
|
|
|
|
}
|
|
|
|
return acc
|
|
|
|
}, [])
|
2020-08-18 21:08:22 +02:00
|
|
|
.map((num) => parseInt(num, 10))
|
2020-02-27 05:49:59 +01:00
|
|
|
|
|
|
|
migrationNumbers.forEach((num) => {
|
|
|
|
if (num >= 33) {
|
|
|
|
assert.ok(testNumbers.includes(num), `no test found for migration: ${num}`)
|
|
|
|
}
|
2020-02-11 17:51:13 +01:00
|
|
|
})
|
2019-12-13 01:14:29 +01:00
|
|
|
})
|
|
|
|
})
|
2018-04-06 02:49:50 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('Migrator', function () {
|
|
|
|
it('migratedData version should be version 3', async function () {
|
2020-08-18 18:36:45 +02:00
|
|
|
const migrator = new Migrator({ migrations: stubMigrations })
|
2020-02-11 17:51:13 +01:00
|
|
|
const migratedData = await migrator.migrateData(versionedData)
|
|
|
|
assert.equal(migratedData.meta.version, stubMigrations[2].version)
|
|
|
|
})
|
2018-04-06 02:49:50 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should match the last version in live migrations', async function () {
|
|
|
|
const migrator = new Migrator({ migrations: liveMigrations })
|
|
|
|
const migratedData = await migrator.migrateData(firstTimeState)
|
|
|
|
const last = liveMigrations.length - 1
|
|
|
|
assert.equal(migratedData.meta.version, liveMigrations[last].version)
|
|
|
|
})
|
2018-04-06 02:49:50 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should emit an error', async function () {
|
|
|
|
const migrator = new Migrator({
|
|
|
|
migrations: [{
|
|
|
|
version: 1,
|
|
|
|
async migrate () {
|
|
|
|
throw new Error('test')
|
|
|
|
},
|
|
|
|
}],
|
|
|
|
})
|
|
|
|
await assert.rejects(migrator.migrateData({ meta: { version: 0 } }))
|
2020-01-15 22:54:28 +01:00
|
|
|
})
|
2018-04-06 04:28:53 +02:00
|
|
|
})
|
2017-05-11 19:47:58 +02:00
|
|
|
})
|