Skip to content

Instantly share code, notes, and snippets.

@todgru
Last active August 7, 2017 16:50
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 todgru/23832db8695ea768d68e04df0a83f675 to your computer and use it in GitHub Desktop.
Save todgru/23832db8695ea768d68e04df0a83f675 to your computer and use it in GitHub Desktop.
sequelize migrations, make sure you handle the promises correctly!
"use strict";
module.exports = {
up: function(queryInterface, Sequelize) {
//
// queryInterface methods DO return promises. Either of the following with work
//
// return queryInterface.addColumn(...)
// .then(() => queryInterface.doSomething(...))
// etc.
// ... or this:
return Promise.resolve()
.then(() => queryInterface.addColumn("foo", "id", {
type: Sequelize.INTEGER,
autoIncrement: true,
})
)
.then(() => queryInterface.removeConstraint("foo", "foo_pkey"))
.then(() => queryInterface.addConstraint("foo", ["id", "fooId"], {
type: "primary key",
name: "foo_pkey",
})
)
.then(() => {
// this will also work
// multi line function
// ...doing work
})
.then(/* this notation will not wait for execution of function */)
.then(() => /* but this notation will :) */);
},
down: function(queryInterface, Sequelize) {
queryInterface.sequelize.query();
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment