kawadhiya21 / mysql-migrations

A tool to use with mysql package to maintain migrations
MIT License
31 stars 25 forks source link

Cannot create custom migration with mysql pool #12

Open nikolay-hristov opened 5 years ago

nikolay-hristov commented 5 years ago

I'm getting an error that conn.query is not a function.

This is my migration init:

let pool = mysql.createPool({
    connectionLimit : config.MYSQL_CONN_POOL,
    host     : config.MYSQL_HOST,
    user     : config.MYSQL_USER,
    password : config.MYSQL_PASSWORD,
    database : config.MYSQL_DATABASE
  });
migration.init(pool, __dirname + '/migrations');

This is my migration code:

module.exports = {
  'up' :  function (conn, cb) {
    conn.query (`ALTER TABLE progress DROP FOREIGN KEY progress_player_id;
    ALTER TABLE progress 
      ADD CONSTRAINT progress_player_id
        FOREIGN KEY (player_id)
        REFERENCES player (id)
        ON DELETE CASCADE
        ON UPDATE CASCADE;`, function (err, res) {
          cb();
    });
  },
  'down' : function (conn, cb) {
    conn.query (`ALTER TABLE progress DROP FOREIGN KEY progress_player_id;ALTER TABLE progress 
    ADD CONSTRAINT progress_player_id
      FOREIGN KEY (player_id)
      REFERENCES player (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;`, function (err, res) {
          cb();
    });
  }
}
rozhnev commented 5 years ago

Hi, You must add requirements before pool init: ` const mysql = require('mysql'); const migration = require('mysql-migrations');

let pool = mysql.createPool({ connectionLimit : config.MYSQL_CONN_POOL, host : config.MYSQL_HOST, user : config.MYSQL_USER, password : config.MYSQL_PASSWORD, database : config.MYSQL_DATABASE }); migration.init(pool, __dirname + '/migrations'); `

nikolay-hristov commented 5 years ago

Hi @rozhnev,

of course, I just didn't post them here, these are my requires

const pool = require('./storage/adapters/mysql_pool.js');
const config = require('./config.js');
const migration = require('mysql-migrations');

Note: My pool comes from another file, I concatenated the two for this issue

nikolay-hristov commented 5 years ago

P.S. Normal migrations work, but custom ones return the error mentioned in the beginning

okzapradhana commented 3 years ago

I'm getting an error that conn.query is not a function.

This is my migration init:

let pool = mysql.createPool({
    connectionLimit : config.MYSQL_CONN_POOL,
    host     : config.MYSQL_HOST,
    user     : config.MYSQL_USER,
    password : config.MYSQL_PASSWORD,
    database : config.MYSQL_DATABASE
  });
migration.init(pool, __dirname + '/migrations');

This is my migration code:

module.exports = {
  'up' :  function (conn, cb) {
    conn.query (`ALTER TABLE progress DROP FOREIGN KEY progress_player_id;
    ALTER TABLE progress 
      ADD CONSTRAINT progress_player_id
        FOREIGN KEY (player_id)
        REFERENCES player (id)
        ON DELETE CASCADE
        ON UPDATE CASCADE;`, function (err, res) {
          cb();
    });
  },
  'down' : function (conn, cb) {
    conn.query (`ALTER TABLE progress DROP FOREIGN KEY progress_player_id;ALTER TABLE progress 
    ADD CONSTRAINT progress_player_id
      FOREIGN KEY (player_id)
      REFERENCES player (id)
      ON DELETE NO ACTION
      ON UPDATE NO ACTION;`, function (err, res) {
          cb();
    });
  }
}

I also faced this issue when using custom migration with function conn.query() is not a function , any workaround for this one?

abubakarakram167 commented 2 years ago

Any luck with this issue @nikolay-hristov @rozhnev I am also facing this issue too.