0

I am new to npm-migration for nodejs and I am trying to create multiple tables with one migration instead of having multiple migrations for creating all my tables. I have tried this:

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

Also this:

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();
        });
    },
}

If you want to see my statements here you are:

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; ";

2 Answers 2

1

Well I refactored my code and I have this a approach:

"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();
    },

Is working, but I am sure there is a better way for doing this.

Sign up to request clarification or add additional context in comments.

Comments

0

There is no one migration that can do that but you can use one command line with && .

For instance:

npx db-migrate create first-table --sql-file && db-migrate create second-table --sql-file && db-migrate create third-table --sql-file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.