I am using sequelize js
and developed a node js application
, which is deployed to production and have live DB.
while in development mode, if I need to alter
the DB
, I used to do it using Sequelize.sync({ force: true })
and it worked well.
But now, after development is done I want to alter a table and add a column to it.
I searched many posts but, didn't get an exact example on how to run these migrations.
I tried to use Umzug
and run a migration, but it is throwing me errors.
Here is the code I tried,
migrations/barmigration.js:
var Sequelize = require('sequelize');
"use strict";
module.exports = {
up: function(migration, DataTypes) {
return [
migration.addColumn(
'Bars',
'PrinterId',
Sequelize.STRING
),
migration.addColumn(
'Bars',
'PrinterStatus',
Sequelize.STRING
)]
},
down: function(migration, DataTypes) {
return
[
migration.removeColumn('Bars', 'PrinterStatus'),
migration.removeColumn('Bars', 'PrinterId')
]
}
};
Here is the umzug configuration
:
var Umzug = require('umzug');
var sequelize = require('sequelize');
var umzug = new Umzug({
// storage: 'sequelize',
model: 'Bar',
storageOptions: {
sequelize: sequelize,
},
migrations: {
path: './migrations',
pattern: /\.js$/
}
});
// umzug.up().then(function(migrations) {
// console.log('Migration complete!');
// });
umzug.down().then(function(migrations) {
console.log('Migration complete!');
});
When I run that file, I am getting an error in up function
, at this position return migration.addColumn
Error:
Unhandled rejection TypeError: Cannot read property 'addColumn' of undefined
So, the parameter migration seems to be undefined
. Pls help me out.
You need to define the
params
property insidemigrations
attribute of the object passed to the constructor ofUmzug
- it defines the parameters that are passed to theup
anddown
functions. I suppose that the configuration object should be as followsWith above definition the
migration
parameter would now become thesequelize.getQueryInterface()
, so simplyqueryInterface
, and theDataTypes
parameter is theSequelize
itself.The
model
attribute is used to define a Sequelize migrations model, you do not need to define it if you are satisfied with the defaultSequelizeMeta
table creation. Here is how theUmzug
constructor object should look like, and here is how thestorageOptions
can look like.EDIT
In separate file you need to create a Sequelize instance (further used to define models etc.) and export it. Let's assume files tree as below
So in the
database.js
we create a Sequelize instance and export it.Then, in the
Umzug
configuration file