aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-05-12 09:15:59 +0800
committerkumavis <aaron@kumavis.me>2017-05-12 09:15:59 +0800
commit70a328e028230f8bffbd88a28d961fde6a4b819f (patch)
tree8aa33ecf9b63b504cddab583816fa046dfc10564
parent8421cf9ccea80e4958a1a70d068bbffa57577390 (diff)
downloadtangerine-wallet-browser-70a328e028230f8bffbd88a28d961fde6a4b819f.tar
tangerine-wallet-browser-70a328e028230f8bffbd88a28d961fde6a4b819f.tar.gz
tangerine-wallet-browser-70a328e028230f8bffbd88a28d961fde6a4b819f.tar.bz2
tangerine-wallet-browser-70a328e028230f8bffbd88a28d961fde6a4b819f.tar.lz
tangerine-wallet-browser-70a328e028230f8bffbd88a28d961fde6a4b819f.tar.xz
tangerine-wallet-browser-70a328e028230f8bffbd88a28d961fde6a4b819f.tar.zst
tangerine-wallet-browser-70a328e028230f8bffbd88a28d961fde6a4b819f.zip
migrator - cleaner migration runner with es7
-rw-r--r--app/scripts/lib/migrator/index.js38
1 files changed, 13 insertions, 25 deletions
diff --git a/app/scripts/lib/migrator/index.js b/app/scripts/lib/migrator/index.js
index 0bf88dbec..ed07a0c60 100644
--- a/app/scripts/lib/migrator/index.js
+++ b/app/scripts/lib/migrator/index.js
@@ -1,47 +1,35 @@
-const asyncQ = require('async-q')
-
class Migrator {
constructor (opts = {}) {
const migrations = opts.migrations || []
+ // sort migrations by version
this.migrations = migrations.sort((a, b) => a.version - b.version)
+ // grab migration with highest version
const lastMigration = this.migrations.slice(-1)[0]
// use specified defaultVersion or highest migration version
this.defaultVersion = opts.defaultVersion || (lastMigration && lastMigration.version) || 0
}
// run all pending migrations on meta in place
- migrateData (versionedData = this.generateInitialState()) {
- const remaining = this.migrations.filter(migrationIsPending)
- if (remaining.length === 0) return versionedData
-
- const migrations = remaining.map((migration, i) => {
- if (i === 0) return this.runMigration.bind(this, migration, versionedData)
- return this.runMigration.bind(this, migration)
- })
+ async migrateData (versionedData = this.generateInitialState()) {
+ const pendingMigrations = this.migrations.filter(migrationIsPending)
+
+ for (let index in pendingMigrations) {
+ let migration = pendingMigrations[index]
+ versionedData = await migration.migrate(versionedData)
+ if (!versionedData.data) throw new Error('Migrator - migration returned empty data')
+ if (versionedData.version !== undefined && migratedData.meta.version !== migration.version) throw new Error('Migrator - Migration did not update version number correctly')
+ }
- return (
- asyncQ.waterfall(migrations)
- .then((migratedData) => Promise.resolve(migratedData))
- )
+ return versionedData
- // migration is "pending" if hit has a higher
+ // migration is "pending" if it has a higher
// version number than currentVersion
function migrationIsPending (migration) {
return migration.version > versionedData.meta.version
}
}
- runMigration (migration, versionedData) {
- return migration.migrate(versionedData)
- .then((migratedData) => {
- if (!migratedData.data) return Promise.reject(new Error('Migrator - migration returned empty data'))
- if (migration.version !== undefined && migratedData.meta.version !== migration.version) return Promise.reject(new Error('Migrator - Migration did not update version number correctly'))
-
- return Promise.resolve(migratedData)
- })
- }
-
generateInitialState (initState) {
return {
meta: {