nodejs npm-migration通过一次迁移创建多个表

时间:2017-09-07 19:23:13

标签: javascript mysql node.js migration

我是nodejs的npm-migration新手,我尝试使用一次迁移创建多个表,而不是创建所有表的多次迁移。 我试过这个:

"up": company_type_up + company_up,
"down": company_down + company_type_down,

还有:

module.exports = {
"up":
    function(pool, cb){
        pool.multipleStatements = true;
        var query =
            company_type_up +
            company_up;
        pool.query(query, function(err, res){
            cb();
        });
    },
"down":
    function(pool, cb){
        var query =
            company_down + company_type_down;
        pool.query(query, function(err, res){
            cb();
        });
    },
}

如果你想在这里看到我的陈述:

var company_type_up =
"CREATE TABLE company_type(" +
"_id VARCHAR(36) NOT NULL PRIMARY KEY, " +
"name VARCHAR(60), " +
"description VARCHAR(500) " +
"); ";

var company_type_down =
"DROP TABLE company_type; ";

var company_up =
"CREATE TABLE company(" +
"_id VARCHAR(36) NOT NULL PRIMARY KEY, " +
"name VARCHAR(60), " +
"description VARCHAR(500), " +
"country VARCHAR(100), " +
"state VARCHAR(100), " +
"city VARCHAR(100), " +
"address VARCHAR(300), " +
"phone1 VARCHAR(60), " +
"phone2 VARCHAR(60), " +
"email VARCHAR(100), " +
"tax_name VARCHAR(300), " +
"tax_number VARCHAR(200), " +
"firm_name VARCHAR(300)" +
"); ";

var company_down =
"DROP TABLE company; ";

1 个答案:

答案 0 :(得分:1)

好吧,我重构了我的代码,我有一个方法:

"up":
    function(pool, cb){
        pool.query(schema.company_type_up, function(err, res){if(err) console.log(err); });
        pool.query(schema.company_up, function(err, res){if(err) console.log(err);});

        cb();
    },
"down":
    function(pool, cb){
        pool.query(schema.company_down, function(err, res){if(err) console.log(err);});
        pool.query(schema.company_type_down, function(err, res){if(err) console.log(err);});

        cb();
    },

工作正常,但我确信有更好的方法可以做到这一点。