Last active
February 4, 2022 17:34
-
-
Save s1moe2/38e6b065f85237ebe48cb9ecc9d10036 to your computer and use it in GitHub Desktop.
Sequelize migration add/drop multiple columns (transacting)
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
// 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 }) | |
]) | |
}) | |
} | |
}; |
Thanks
Thank you !!
show!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works like a charm. Thank you!