aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/migrator/index.js
blob: 85c2717ea33d2c16b65623877e5ed1e9af4d6e03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const EventEmitter = require('events')

class Migrator extends EventEmitter {

  constructor (opts = {}) {
    super()
    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
  async migrateData (versionedData = this.generateInitialState()) {
    // get all migrations that have not yet been run
    const pendingMigrations = this.migrations.filter(migrationIsPending)

    // perform each migration
    for (const index in pendingMigrations) {
      const migration = pendingMigrations[index]
      try {
        // attempt migration and validate
        const migratedData = await migration.migrate(versionedData)
        if (!migratedData.data) throw new Error('Migrator - migration returned empty data')
        if (migratedData.version !== undefined && migratedData.meta.version !== migration.version) throw new Error('Migrator - Migration did not update version number correctly')
        // accept the migration as good
        versionedData = migratedData
      } catch (err) {
        // rewrite error message to add context without clobbering stack
        const originalErrorMessage = err.message
        err.message = `MetaMask Migration Error #${migration.version}: ${originalErrorMessage}`
        console.warn(err.stack)
        // emit error instead of throw so as to not break the run (gracefully fail)
        this.emit('error', err)
        // stop migrating and use state as is
        return versionedData
      }
    }

    return versionedData

    // migration is "pending" if it has a higher
    // version number than currentVersion
    function migrationIsPending (migration) {
      return migration.version > versionedData.meta.version
    }
  }

  generateInitialState (initState) {
    return {
      meta: {
        version: this.defaultVersion,
      },
      data: initState,
    }
  }

}

module.exports = Migrator