aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/migrator
diff options
context:
space:
mode:
authorkumavis <aaron@kumavis.me>2017-01-12 17:17:05 +0800
committerkumavis <aaron@kumavis.me>2017-01-12 17:17:05 +0800
commitb33c51c0a6c7c8a7b0c0a9a6ca101f874f2db3d1 (patch)
tree7707ff23ae225ee2dd4eef17bfd67c688a817b43 /app/scripts/lib/migrator
parent3bc996878b467e1fa5fd63656bd465377daa137d (diff)
downloadtangerine-wallet-browser-b33c51c0a6c7c8a7b0c0a9a6ca101f874f2db3d1.tar
tangerine-wallet-browser-b33c51c0a6c7c8a7b0c0a9a6ca101f874f2db3d1.tar.gz
tangerine-wallet-browser-b33c51c0a6c7c8a7b0c0a9a6ca101f874f2db3d1.tar.bz2
tangerine-wallet-browser-b33c51c0a6c7c8a7b0c0a9a6ca101f874f2db3d1.tar.lz
tangerine-wallet-browser-b33c51c0a6c7c8a7b0c0a9a6ca101f874f2db3d1.tar.xz
tangerine-wallet-browser-b33c51c0a6c7c8a7b0c0a9a6ca101f874f2db3d1.tar.zst
tangerine-wallet-browser-b33c51c0a6c7c8a7b0c0a9a6ca101f874f2db3d1.zip
migrations - introduce promise-based migrator
Diffstat (limited to 'app/scripts/lib/migrator')
-rw-r--r--app/scripts/lib/migrator/index.js31
1 files changed, 31 insertions, 0 deletions
diff --git a/app/scripts/lib/migrator/index.js b/app/scripts/lib/migrator/index.js
new file mode 100644
index 000000000..02d8c2335
--- /dev/null
+++ b/app/scripts/lib/migrator/index.js
@@ -0,0 +1,31 @@
+const asyncQ = require('async-q')
+
+class Migrator {
+
+ constructor (opts = {}) {
+ let migrations = opts.migrations || []
+ this.migrations = migrations.sort((a, b) => a.version - b.version)
+ let 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 (meta = { version: this.defaultVersion }) {
+ let remaining = this.migrations.filter(migrationIsPending)
+
+ return (
+ asyncQ.eachSeries(remaining, (migration) => migration.migrate(meta))
+ .then(() => meta)
+ )
+
+ // migration is "pending" if hit has a higher
+ // version number than currentVersion
+ function migrationIsPending(migration) {
+ return migration.version > meta.version
+ }
+ }
+
+}
+
+module.exports = Migrator