Skip to content

Instantly share code, notes, and snippets.

@tzkmx
Created August 20, 2020 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzkmx/abe0e8aae0c3c505204e91cb4ac9b094 to your computer and use it in GitHub Desktop.
Save tzkmx/abe0e8aae0c3c505204e91cb4ac9b094 to your computer and use it in GitHub Desktop.
Sequelize Migration With Updates
'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