Created
August 20, 2020 00:09
-
-
Save tzkmx/abe0e8aae0c3c505204e91cb4ac9b094 to your computer and use it in GitHub Desktop.
Sequelize Migration With Updates
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
'use strict'; | |
const tags = { | |
Negocios: 'business', | |
Social: 'social', | |
Familiar: 'familiar', | |
Personal: 'personal' | |
}; | |
module.exports = { | |
up(queryInterface, Sequelize) { | |
return queryInterface.sequelize.transaction(async t => { | |
await queryInterface.addColumn('Types', 'tag', { | |
type: Sequelize.STRING(90), | |
required: false, | |
allowNull: true | |
}, { transaction: t }), | |
for await (const query of runUpdates( | |
queryInterface, | |
'ServiceTypes', | |
tags, | |
t | |
)) {} | |
}); | |
}, | |
down(queryInterface) { | |
return queryInterface.removeColumn('Types', 'tag'); | |
} | |
}; | |
function* runUpdates(queryInterface, table, updates, transaction) { | |
for (const name in updates) { | |
const tag = updates[name]; | |
yield queryInterface.sequelize.query( | |
`UPDATE "public"."${table}" SET tag='${tag}' WHERE name='${name}'`, | |
{ transaction, logging: console.log } | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment