Sequelize.js: how to use migrations and sync

后端 未结 11 1443
一向
一向 2020-12-02 03:20

I\'m close to having my project ready to launch. I have big plans for after launch and the database structure is going to change -- new columns in existing tables as well as

11条回答
  •  醉酒成梦
    2020-12-02 04:03

    Now with the new sequelize migration is very simple.

    This is a example what you can do.

        'use strict';
    
        var Promise = require('bluebird'),
            fs = require('fs');
    
        module.exports = {
            up: function (queryInterface, Sequelize) {
    
                return Promise
                    .resolve()
                    .then(function() {
                        return fs.readFileSync(__dirname + '/../initial-db.sql', 'utf-8');
                    })
                    .then(function (initialSchema) {
                        return queryInterface.sequelize.query(initialSchema);
                    })
            },
    
            down: function (queryInterface, Sequelize) {
                return Promise
                    .resolve()
                    .then(function() {
                        return fs.readFileSync(__dirname + '/../drop-initial-db.sql', 'utf-8');
                    })
                    .then(function (dropSql) {
                        return queryInterface.sequelize.query(dropSql);
                    });
            }
        };
    

    Remember you have to set:

    "dialectOptions": { "multipleStatements": true }

    on database config.

提交回复
热议问题