Skip to content

Instantly share code, notes, and snippets.

@repodevs
Created June 25, 2019 08:31
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 repodevs/783f5e483ed035625a1536267f543aab to your computer and use it in GitHub Desktop.
Save repodevs/783f5e483ed035625a1536267f543aab to your computer and use it in GitHub Desktop.
Sequelize add or remove enum values
module.exports = {
up: (queryInterface, DataTypes) => {
return queryInterface.sequelize.query("ALTER TYPE enum_students_id_card_type ADD VALUE 'driving_license';");
},
down: (queryInterface, DataTypes) => {
// FIXME: Removing enum value is not supported by PostgreSQL,
// but there is any tricky way to do this.
// ref: https://stackoverflow.com/a/46244969, https://stackoverflow.com/a/25812436
return queryInterface.sequelize.query("UPDATE students SET id_card_type = 'ktp' WHERE id_card_type = 'driving_license';")
.then(() => {
const query = `DELETE FROM pg_enum
WHERE enumlabel = 'driving_license'
AND enumtypid = (SELECT oid FROM pg_type WHERE typname = 'enum_students_id_card_type');`;
return queryInterface.sequelize.query(query);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment