admin 管理员组文章数量: 1086019
In Sequelize.js, i have created an example migration file:
module.exports = {
up: function(migration, DataTypes, done) {
// add altering mands here, calling 'done' when finished
migration.createTable('Users', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
createdAt: {
type: DataTypes.DATE
},
updatedAt: {
type: DataTypes.DATE
},
firstname: DataTypes.STRING,
lastname: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.STRING,
});
done()
},
down: function(migration, DataTypes, done) {
// add reverting mands here, calling 'done' when finished
done()
}
}
Could someone explain the use cases and possible implementation of both up and down functionality?
Thank you!
In Sequelize.js, i have created an example migration file:
module.exports = {
up: function(migration, DataTypes, done) {
// add altering mands here, calling 'done' when finished
migration.createTable('Users', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
createdAt: {
type: DataTypes.DATE
},
updatedAt: {
type: DataTypes.DATE
},
firstname: DataTypes.STRING,
lastname: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.STRING,
});
done()
},
down: function(migration, DataTypes, done) {
// add reverting mands here, calling 'done' when finished
done()
}
}
Could someone explain the use cases and possible implementation of both up and down functionality?
Thank you!
Share Improve this question asked Feb 28, 2014 at 0:45 alotofquestionsalotofquestions 1642 gold badges2 silver badges12 bronze badges 3- If you not see cases in your project, don't use this function. Keep it short and simple. For example: create dbsync script and run it when you update the model. – Sergey Karasev Commented Mar 3, 2014 at 8:52
- Hi, I am unaware of possible use cases. Sequelize Documentation does not explain this thoroughly. Could you show me an example? Thank you! – alotofquestions Commented Mar 3, 2014 at 17:24
- What's your question exactly? I don't understand it quite well.... If you mean what's up and what's down: - up: all mands will be executed when running sequelize db:migrate - down: all mands will be executed when running sequelize db:migrate:undo. Sequelize also says the development environment is default, but I experienced problems with this. So I have to execute all mands with --env development at the end. – Erik van de Ven Commented Oct 21, 2014 at 9:44
1 Answer
Reset to default 6You got to see the thing like if your database have 2 different "states", before migration and after migration.
Before you start any migration you don't have any Users
table, so lets say that your database is in "state 1".
When you run the up migrations ( sequelize db:migrate
) you pass your database to "state 2" ( now you have the Users
table in your database ).
If anything went wrong or you changed your mind about this migration, you can go back to "state 1" again by running the down functionality ( sequelize db:migrate:undo
) doing the opposite of the up functionality.
In this case its simple, if you are creating a Users
, to reverse you have only to delete it and you go back to "state 1" again.
module.exports = {
up: function(migration, DataTypes, done) {
// add altering mands here, calling 'done' when finished
migration.createTable( 'Users', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
createdAt: {
type: DataTypes.DATE
},
updatedAt: {
type: DataTypes.DATE
},
firstname: DataTypes.STRING,
lastname: DataTypes.STRING,
email: DataTypes.STRING,
password: DataTypes.STRING,
})
.nodeify( done );
},
down: function(migration, DataTypes, done) {
// add reverting mands here, calling 'done' when finished
migration.dropTable('Users')
.nodeify( done );
}
};
Per example, if you were going to delete the Users
table instead of creating it, you just have to exchange the up and down code.
本文标签: javascriptHow do I implement sequelize migration down functionality for databasesStack Overflow
版权声明:本文标题:javascript - How do I implement sequelize migration down functionality for databases? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744051253a2525058.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论