aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/migrator/index.js
blob: 203b2ddc904d2f2931e642035dc8d695ccaf1ffe (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
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) {
        // emit error instead of throw so as to not break the run (gracefully fail)
        const error = new Error(`MetaMask Migration Error #${version}:\n${err.stack}`)
        this.emit('error', error)
        // 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
2010-12-045-5/+5 * Reset hideo@lastamericanempire.com due to maintainer-timeouts and nolinimon2010-12-021-1/+1 * Reset anray@FreeBSD.org due to maintainer-timeouts and no response to email.linimon2010-12-023-3/+3 * - Update to 0.5.6swills2010-11-273-5/+10 * Make fetchable again.thierry2010-10-231-1/+1 * Punt autoconf267->autoconf268ade2010-10-162-2/+2 * - Update to 2.13tdb2010-10-162-4/+4 * - update to 2.20leeym2010-10-152-17/+9 * - add RC scriptswills2010-10-114-1/+109 * - Chease databases/py-MySQLdb updatelwhsu2010-10-091-1/+1 * Migration of BROKEN ports from automake{19,110}->111 which will mostade2010-10-071-1/+1 * - add missing SQLite dependencyswills2010-10-061-4/+10 * Round one migration of ports from automake{19,110} to automake111ade2010-10-062-2/+2 * Mark BROKEN: checksum mismatcherwin2010-10-011-0/+2 * - Switch MASTER_SITES to local to make it fetchable againfluffy2010-09-301-2/+5 * - fix crash on png filesdinoex2010-09-273-26/+56 * Migrate from devel/automake15 to devel/automake19ade2010-09-241-1/+1 * - only 13% of the p5- ports embed @comment $FreeBSD$:pgollucci2010-09-242-2/+0 * Autotools update. Read ports/UPDATING 20100915 for details.ade2010-09-163-4/+5 * - Update to 0.5.4swills2010-09-052-4/+4 * - Update to trn 4.0.b77johans2010-09-045-56/+38 * - Update to 0.5.3pav2010-08-275-342/+312 * - Update to 10.1.0lwhsu2010-08-242-5/+5 * - Bump PORTREVISION after libxul updatebeat2010-08-081-1/+1 * - Add OPTIONS instead of knobs for news/slrnjohans2010-07-301-0/+6 * Add LICENSE* informationlioux2010-06-131-0/+2 * Switch MAINTAINER line to FreeBSD address.mandree2010-06-131-1/+1 * Upgrade to new upstream release 1.11.8mandree2010-06-122-6/+6 * - use REINPLACE_CMDdinoex2010-06-111-3/+2 * - add LICENSE_FILEdinoex2010-06-101-0/+1 * - fix build with LICENSEdinoex2010-06-101-0/+1 * - Update to 2.18pgollucci2010-06-102-6/+11 * LICENSE GPLv2dinoex2010-06-062-1/+3 * - note USERS does not work heredinoex2010-06-051-0/+2 * LICENSE BSDdinoex2010-06-041-0/+1 * - Update to recent cleanfeed release by Steve Crook (2. June)johans2010-06-044-15/+36 * Bounce PORTREVISION for gettext-related ports. Have fun, ya'll.ade2010-05-317-7/+7 * - drop USE_GNUSTEP_PREFIXdinoex2010-05-302-19/+18 * - fix build for gnustep-base 1.21.0dinoex2010-05-242-0/+20 * Add patch forgotten in previous commit. No functional change:johans2010-05-141-0/+416 * Remove unnecessary dependency on autoconfjohans2010-05-131-1/+1 * - Update to 0.7.0sylvio2010-05-022-5/+16 * - honor CCdinoex2010-04-101-1/+1 * New port py-pynzb version 0.1.0: A unified API for parsing NZB fileslioux2010-04-075-0/+72 * Chase the ftp/curl shlib version bump.roam2010-04-031-2/+2 * Mark unconditionatlly BROKEN: does not configureerwin2010-04-031-7/+3 * Update to 10.00-beta-3johans2010-03-282-4/+4 * - update to 1.4.1dinoex2010-03-2812-12/+13 * Begin the process of deprecating sysutils/rc_subr bydougb2010-03-273-3/+3 * Update my mail address to @FreeBSD.orgjohans2010-02-246-6/+6 * Fix the build w/ new GCC.mezz2010-02-221-0/+54 * - Update to 9.0.0lwhsu2010-02-072-4/+4 * - update to jpeg-8dinoex2010-02-0511-11/+12 * - Remove unneeded dependencies which is in perl-5.8.9 distkuriyama2010-01-262-3/+2 * - Removed Python 2.5 requirement introduced by recent commit, since theglarkin2010-01-261-18/+2 * - Add cpan site to WWWehaupt2010-01-232-0/+4 * - Added checks to Makefile to restrict installation with Python 2.5 only [1]glarkin2010-01-193-7/+46 * With portmgr hat on, reset the maintainership of skv@ for ports thatlinimon2010-01-101-1/+1 * Reset vs@FreeBSD.org at his request due to lack of time.linimon2010-01-031-1/+1 * - Update to 2.14miwi2009-12-262-4/+4 * For ports maintained by ports@FreeBSD.org, remove names and/ordougb2009-12-215-16/+0 * - add path for inn 2.5.0dinoex2009-11-291-1/+1 * - Make perl5 dependency mandatory [1]fluffy2009-11-271-13/+7 * - Remove obsolete gecko providersbeat2009-11-271-3/+2 * Reset clement@FreeBSD.org due to long absence from FreeBSD.linimon2009-11-122-2/+2 * Attempt to fix pkg-plisterwin2009-11-112-1/+2 * Reset hoek@ who has been inactive for quite some time.linimon2009-11-101-1/+1 * Reset hoek@ who has been inactive for quite some time. Also note thatlinimon2009-11-101-2/+2 * - Update to 0.4.12beat2009-10-272-4/+4 * Mark BROKEN: does not builderwin2009-10-251-0/+2 * Use USERS and GROUPS.fjoe2009-10-222-73/+2 * - Update to 1.5.3anray2009-10-156-58/+89 * - Update to 0.5.1miwi2009-09-234-163/+249 * - Retire MASTER_SITE_SOURCEFORGE_EXTENDED, it's no longer needed - all mirror...amdmi32009-09-021-6/+2 * Reset chinsan@FreeBSD.org due to numerous maintainer-timeouts and nolinimon2009-08-291-1/+1 * - Fix INDEXmiwi2009-08-231-3/+1 * - Fix broken makefile introduced with translation to new SF File Release Systemamdmi32009-08-221-3/+2 * - Fix broken makefiles introduced with translation to new SF File Release Systemamdmi32009-08-221-4/+0 * - Switch SourceForge ports to the new File Release System: categories startin...amdmi32009-08-2236-37/+42 * Update my MAINTAINER's email to @FreeBSD.orgfluffy2009-08-111-1/+1 * -Repocopy devel/libtool15 -> libtool22 and libltdl15 -> libltdl22.mezz2009-08-033-3/+2 * Reset alexbl@FreeBSD.org due to maintainer-timeouts and no repsonselinimon2009-08-021-1/+1 * - bump all port that indirectly depends on libjpeg and have not yet been bump...dinoex2009-07-318-6/+8 * - Update to 2.4.6miwi2009-07-293-48/+8 * - lang/perl5.6 is dead, remove PERL_LEVEL/PERL_VERSION < 500801 checkspgollucci2009-07-232-15/+2 * With regret, return these to the pool.shaun2009-07-221-1/+1 * - Update to 1.04clsung2009-07-212-4/+4 * - update to jpeg7dinoex2009-07-181-2/+2 * Fix a few "bad example" problems in the rc.d scripts that have beendougb2009-07-161-3/+3 * - fix NOPORTSDOCdinoex2009-07-061-7/+8 * - fix build with custom LOCALBASEdinoex2009-07-053-25/+22 * - Update to version 10.00 (beta 1)johans2009-06-164-24/+24 * Remove news/inn-current as it has been marked BROKEN 2 years and 4 months ago.erwin2009-06-144-43/+0 * Convert most of remaining ports that depend on xorg-libraries toamdmi32009-06-091-1/+2 * - Update to 0.4.11amdmi32009-05-282-7/+7 * - Update to 0.4.9miwi2009-04-242-4/+4 * - Update to 0.6.0miwi2009-04-236-96/+4 * - Update to DISTVERSION= 1.11.7miwi2009-04-233-15/+17 * - Introduce OPTIONSjohans2009-04-103-50/+29 * Correct typo to fix plist ith -DNOPORTDOCSitetcu2009-04-051-1/+1 * - MAKE_JOBS_UNSAFEdinoex2009-03-282-2/+6 * Mark MAKE_JOBS_SAFEehaupt2009-03-271-0/+2 * - Update to 0.4.8amdmi32009-03-272-4/+4 * - Respect NOPORTDOCSjohans2009-03-242-29/+34 * - Update WWWpav2009-03-231-1/+1 * - add LICENSE:dinoex2009-03-201-0/+2 * LottaNZB is a graphical usenet frontend utilizing hellanzbmiwi2009-03-175-0/+426 * Free most of my ports, since they aren't being maintained to ashaun2009-03-142-2/+2 * - Update to 0.4.7rafan2009-02-284-18/+14 * Update to 2.10skv2009-02-232-4/+4 * - add LICENSE:dinoex2009-02-161-0/+2 * 2009-02-09 audio/xmms-imms: does not configure or buildmiwi2009-02-147-202/+0 * Take up maintainershipjohans2009-02-134-5/+6 * - Update to 1.2.6nivit2009-02-112-4/+4 * - Update to 2.12.tdb2009-02-072-4/+4 * - Update download locationspgollucci2009-02-011-0/+1 * - Update py-twisted-* to 8.2.0lwhsu2009-01-312-5/+5 * Bump the version of the curl shared library after the ftp/curl updateroam2009-01-231-2/+2 * - Remove conditional checks for FreeBSD 5.x and olderpav2009-01-072-8/+3 * - Update to 0.4.6miwi2008-12-232-4/+4