Search code examples
webpacksequelize.jsumzugelectron-vue

How to pass an array of migration instances in umzug config


I'm using umzug to run the migrations in my electron-vue app, because of the webpack I can't use the path like

migrations: {
    path: __dirname + '/../migrations',
    pattern: /\.js$/
}

So I heard I can pass migrations instances, but I don't know how I do that.

So far I have this:

// sequelize initialization, etc...

let migrations = [
    require('../migrations/20180427160552-create-user'),
    require('../migrations/20180515205633-create-visits'),
    require('../migrations/20180515205633-create-vehicles')
];

var Umzug = require('umzug');
var umzug = new Umzug({
    storage: 'sequelize',
    storageOptions: {
    sequelize: sequelize
    },
    migrations: migrations
});
umzug.up().then(function (migrations) { });

Solution

  • You can use customResolver

    async function checkMigrations() {
      const umzug = new Umzug({
        storage: 'sequelize',
        storageOptions: {
          sequelize,
        },
        migrations: {
          params: [
            sequelize.getQueryInterface(),
            Sequelize,
            function() {
              throw new Error('Migration tried to use old style "done" callback.');
            }
          ],
          path: 'path/to/migrations',
          pattern: /\.js$/,
          customResolver: function(migrationFile) {
            return require(`./migrations/${path.basename(migrationFile, '.js')}`);
          }
        },
        logging: function() {
          console.log.apply(null, arguments);
        },
      });
    
      return umzug.up();
    }
    

    The point is to use "prefix" folder as literal, but not as variable. This will not work:

    customResolver: function(migrationFile) {
        return require(`${some-path}/${some-file}`);
    }
    

    But this will:

    customResolver: function(migrationFile) {
        return require(`/absolute-or-relative-prefix-folder/${filename-based-on-func-argument}`);
    }