Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple migration statements in one migration file

I am trying to execute multiple migration statements in a single migration file in order to make changes to multiple columns of same table in one go.

I want to know that whether I am doing it in a write way or not or is there a better and more appropriate way to do it:

Migration Code

module.exports = {
    up: function(queryInterface, Sequelize, done) {

        queryInterface.changeColumn('users', 'name', {
            type: Sequelize.STRING,
            allowNull: false,
            require: true,
            unique: true
        }).success(function() {
            queryInterface.changeColumn('users', 'address', {
                type: Sequelize.STRING,
                allowNull: false,
                require: true,
                unique: true
            }).success(function() {
                queryInterface.changeColumn('users', 'city', {
                    type: Sequelize.STRING,
                    allowNull: false,
                    require: true,
                    unique: true
                }).success(function() {
                    queryInterface.changeColumn('users', 'state', {
                        type: Sequelize.STRING,
                        allowNull: false,
                        require: true,
                        defaultValue: "ncjnbcb"
                    });
                    done();
                });
            });
        });
    }
};

But I face an error which says:

TypeError: undefined is not a function

Since i couldn't find any way of debugging error in migrations, it will be great if someone helps me out in resolving it or if possible, tell about the way as of how can we figure out the errors in a migration.

like image 302
Prerna Jain Avatar asked Nov 03 '15 15:11

Prerna Jain


People also ask

How do I get rid of migrations?

Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.

What is node migration?

Node migrations are much like term migrations, in terms of mapping a source type to a destination type. source_type: The unique machine name of the legacy node type. destination_type: The unique machine name of the destination node type.


2 Answers

Your TypeError is probably because you're not returning anything. The docs say that each migration function should return a Promise. No mention of a done callback.

To that end, try the following:

return Promise.all([
  queryInterface.changeColumn..., 
  queryInterface.changeColumn...
]);
like image 167
aidan Avatar answered Sep 20 '22 19:09

aidan


module.exports = {
  up: async (queryInterface, Sequelize) => {
    try {
      await queryInterface.addColumn('User', 'name', {
        type: Sequelize.STRING
      });
      await queryInterface.addColumn('User', 'nickname', {
        type: Sequelize.STRING
      });
      return Promise.resolve();
    } catch (e) {
      return Promise.reject(e);
    }
  },

  down: async (queryInterface, Sequelize) => {
    try {
      await queryInterface.removeColumn('Challenges', 'name');
      await queryInterface.removeColumn('Challenges', 'nickname');
      return Promise.resolve();
    } catch (e) {
      return Promise.reject(e);
    }
  }
};
like image 26
Firmino Changani Avatar answered Sep 19 '22 19:09

Firmino Changani