Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeonghwan-kim/5c6eccad7500c48a17ba to your computer and use it in GitHub Desktop.
Save jeonghwan-kim/5c6eccad7500c48a17ba to your computer and use it in GitHub Desktop.
module.exports = {
up: function (queryInterface, Sequelize) {
return [
queryInterface.addColumn('User', 'name', {
type: Sequelize.STRING
}),
queryInterface.addColumn('User', 'nickname', {
type: Sequelize.STRING,
})
];
},
down: function (queryInterface, Sequelize) {
return [
queryInterface.removeColumn('Challenges', 'name'),
queryInterface.removeColumn('Challenges', 'nickname')
];
}
};
@pathum-kalhan
Copy link

Thnaks a lot. Save my day!

@s1moe2
Copy link

s1moe2 commented Nov 20, 2018

I would not consider this exactly right because if one of the operations (addColumn) fails, you'll end up with Unhandled rejection, possibly some of the alterations made to the database (while other fail) and the migration will be inserted into the SequelizeMeta table as completed.
Ideally, you should always use transactions (to rollback on failure) and to keep your migrations/database consistent.
Here's how I do it: https://gist.github.com/s1moe2/38e6b065f85237ebe48cb9ecc9d10036

@flowck
Copy link

flowck commented Mar 10, 2019

All issues related to Promises can be solved using ES6 aync/await and wrapping the queryInterface operations on a try/catch block.

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);
    }
  }
};

@Mohit21GoJs
Copy link

Mohit21GoJs commented Apr 21, 2019

If you get (or wrapper) didn't return a promise error then you can try this gist

@AmberPoison
Copy link

"use strict";

module.exports = {
  up: async (queryInterface, Sequelize) => {
    let transaction = await queryInterface.sequelize.transaction();
    try {
      await queryInterface.addColumn(
        "TicketActions",
        "title",
        {
          type: Sequelize.STRING,
        },
        { transaction },
      );
      await queryInterface.addColumn(
        "TicketActions",
        "uploadPath",
        {
          type: Sequelize.STRING,
        },
        { transaction },
      );
      await queryInterface.addColumn(
        "TicketActions",
        "description",
        {
          type: Sequelize.STRING,
        },
        { transaction },
      );
      await transaction.commit();
      return Promise.resolve();
    } catch (err) {
      if (transaction) {
        await transaction.rollback();
      }
      return Promise.reject(err);
    }
  },

  down: async (queryInterface, Sequelize) => {
    let transaction = await queryInterface.sequelize.transaction();
    try {
      await queryInterface.removeColumn("TicketActions", "title", { transaction });
      await queryInterface.removeColumn("TicketActions", "uploadPath", { transaction });
      await queryInterface.removeColumn("TicketActions", "description", { transaction });
      await transaction.commit();
      return Promise.resolve();
    } catch (err) {
      if (transaction) {
        await transaction.rollback();
      }
      return Promise.reject(err);
    }
  },
};

Use transaction in case 1 query is failed.

@Andrew1431
Copy link

Aren't all migrations run in transactions?

@tawfiknasser
Copy link

migrate function should return promise so add Promise.all

module.exports = {
  up: function (queryInterface, Sequelize) {
    return Promise.all([
      queryInterface.addColumn('User', 'name', {
        type: Sequelize.STRING
      }),
      queryInterface.addColumn('User', 'nickname', {
        type: Sequelize.STRING,
      })
    ]);
  },

  down: function (queryInterface, Sequelize) {
    return Promise.all([
      queryInterface.removeColumn('User', 'name'),
      queryInterface.removeColumn('User', 'nickname')
    ]);
  }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment