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')
];
}
};
@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