Multiple migration statements in one migration file
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);
}
}
};
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...
]);
Using Promise.all
with transactions (safer migration) :
module.exports = {
up: async (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction(t => {
return Promise.all([
queryInterface.changeColumn('users', 'name',
{ type: Sequelize.STRING },
{ transaction: t }
),
queryInterface.changeColumn('users', 'address',
{ type: Sequelize.STRING },
{ transaction: t }
),
queryInterface.changeColumn('users', 'city',
{ type: Sequelize.STRING },
{ transaction: t }
)
]);
});
},
down: async (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction((t) => {
return Promise.all([
queryInterface.removeColumn('users', 'name', { transaction: t }),
queryInterface.removeColumn('users', 'address', { transaction: t }),
queryInterface.removeColumn('users', 'city', { transaction: t })
])
})
}
};
Using Promise.all
without transactions would cause issues if some of the queries are rejected. It is safe to use transactions so that all operations would be executed successfully or none of the changes would be made.