最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I implement sequelize migration down functionality for databases? - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 6

You 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.

发布评论

评论列表(0)

  1. 暂无评论