Created
January 11, 2016 12:33
-
-
Save jeonghwan-kim/5c6eccad7500c48a17ba to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | |
]; | |
} | |
}; |
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);
}
}
};
If you get (or wrapper) didn't return a promise
error then you can try this gist
"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.
Aren't all migrations run in transactions?
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
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 theSequelizeMeta
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