Skip to content

Instantly share code, notes, and snippets.

@s1moe2
Last active February 4, 2022 17:34
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save s1moe2/38e6b065f85237ebe48cb9ecc9d10036 to your computer and use it in GitHub Desktop.
Save s1moe2/38e6b065f85237ebe48cb9ecc9d10036 to your computer and use it in GitHub Desktop.
Sequelize migration add/drop multiple columns (transacting)
// NOTE: MySQL does not support transactional DDL (https://dev.mysql.com/doc/internals/en/transactions-notes-on-ddl-and-normal-transaction.html)
// You should use this with a (cool) DBMS such as PostgreSQL.
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction((t) => {
return Promise.all([
queryInterface.addColumn('table_name', 'column_name1', {
type: Sequelize.STRING
}, { transaction: t }),
queryInterface.addColumn('table_name', 'column_name2', {
type: Sequelize.STRING,
}, { transaction: t })
])
})
},
down: (queryInterface, Sequelize) => {
return queryInterface.sequelize.transaction((t) => {
return Promise.all([
queryInterface.removeColumn('table_name', 'column_name1', { transaction: t }),
queryInterface.removeColumn('table_name', 'column_name2', { transaction: t })
])
})
}
};
@jfuenmayor96
Copy link

Works like a charm. Thank you!

@jsartisan
Copy link

Thanks

@manoellribeiro
Copy link

Thank you !!

@vitorgonzaga
Copy link

show!

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