3

I am using sequelize js and developed a node js application, which is deployed to production and have live DB.

while in development mode, if I need to alter the DB, I used to do it using Sequelize.sync({ force: true }) and it worked well.

But now, after development is done I want to alter a table and add a column to it.

I searched many posts but, didn't get an exact example on how to run these migrations.

I tried to use Umzug and run a migration, but it is throwing me errors.

Here is the code I tried,

migrations/barmigration.js:

  var Sequelize = require('sequelize');

"use strict";

module.exports = {

    up: function(migration, DataTypes) {
      return [
      migration.addColumn(
        'Bars',
        'PrinterId',
        Sequelize.STRING
      ),
      migration.addColumn(
        'Bars',
        'PrinterStatus',
        Sequelize.STRING
      )]

    },

    down: function(migration, DataTypes) {
        return
         [
            migration.removeColumn('Bars', 'PrinterStatus'),
            migration.removeColumn('Bars', 'PrinterId')
        ]
    }

};

Here is the umzug configuration:

 var Umzug = require('umzug');
    var sequelize = require('sequelize');

    var umzug = new Umzug({

        // storage: 'sequelize',
        model: 'Bar',


        storageOptions: {
            sequelize: sequelize,
        },

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

    });

    //  umzug.up().then(function(migrations)  {
    //    console.log('Migration complete!');
   //     });

    umzug.down().then(function(migrations)  {
      console.log('Migration complete!');
    });

When I run that file, I am getting an error in up function, at this position return migration.addColumn

Error:

Unhandled rejection TypeError: Cannot read property 'addColumn' of undefined

So, the parameter migration seems to be undefined. Pls help me out.

Sravan
  • 18,467
  • 3
  • 30
  • 54

1 Answers1

14

You need to define the params property inside migrations attribute of the object passed to the constructor of Umzug - it defines the parameters that are passed to the up and down functions. I suppose that the configuration object should be as follows

{
    storage: 'sequelize',
    storageOptions: {
        sequelize: sequelize // here should be a sequelize instance, not the Sequelize module
    },
    migrations: {
        params: [
            sequelize.getQueryInterface(),
            Sequelize // Sequelize constructor - the required module
        ],
        path: './migrations',
        pattern: /\.js$/
    }
}

With above definition the migration parameter would now become the sequelize.getQueryInterface(), so simply queryInterface, and the DataTypes parameter is the Sequelize itself.

The model attribute is used to define a Sequelize migrations model, you do not need to define it if you are satisfied with the default SequelizeMeta table creation. Here is how the Umzug constructor object should look like, and here is how the storageOptions can look like.

EDIT

In separate file you need to create a Sequelize instance (further used to define models etc.) and export it. Let's assume files tree as below

 - db
     - database.js
     - umzug.js

So in the database.js we create a Sequelize instance and export it.

 // database.js
 const Sequelize = require('sequelize');

 const db = {
     sequelize: new Sequelize(connectionString, options),
     Sequelize: Sequelize
 };

 module.exports = db;

Then, in the Umzug configuration file

// umzug.js
const db = require('./database');

// here comes the configuration and initialization of Umzug instance with use of db object
// db.sequelize -> sequelize instance
// db.Sequelize -> sequelize constructor (class)
piotrbienias
  • 7,201
  • 1
  • 28
  • 33
  • thnx for your response. but I am getting error, `TypeError: sequelize.getQueryInterface is not a function`. I required `sequelize` in the file. – Sravan Mar 16 '17 at 06:48
  • `sequelize.getQueryInterface` must be run on Sequelize instance, not on the constructor itself. Did you create a sequelize instance in separate file and required it in this one? – piotrbienias Mar 16 '17 at 06:51
  • yes, I have an instance of `sequelize` and exported that instance. – Sravan Mar 16 '17 at 06:56
  • Then you must be doing something wrong. You would have to update the question with your new code version. – piotrbienias Mar 16 '17 at 08:38
  • I have edited the answer, maybe now it will help you. – piotrbienias Mar 16 '17 at 10:07
  • thnx for your update, now I got my migration working. but not completely. When I call `up()` method it is working fine. but, when I call `down()`, strangely it is not working. updated my latest code in the question. – Sravan Mar 16 '17 at 11:42
  • Not working in what way? What happens when you call `down()`? – piotrbienias Mar 16 '17 at 12:46
  • the migration method gets called, but `drop query` is not getting called. – Sravan Mar 16 '17 at 12:49
  • I have tested above configuration and everything works fine, source of the problem with not removing columns must be somewhere else... – piotrbienias Mar 16 '17 at 14:42
  • thank you. It worked now and strangely I didn't change any code. – Sravan Mar 17 '17 at 06:03