2

Question on sequelize-cli running db:migrate to create/alter tables on MySQL. Running migration as per the document.

I was seeing the sql (raw query) as part of the console output from sequelize-cli as shown below:

npx sequelize-cli db:migrate --config "dbconfig.json" --env "localdb"
npx: installed 106 in 17.63s

Sequelize CLI [Node: 10.13.0, CLI: 5.4.0, ORM: 4.37.10]
:
:
== 20190417134836-mstprices: migrating =======
Executing (default): CREATE TABLE IF NOT EXISTS `mstprices` (`id` INTEGER NOT NULL auto_increment , `units` VARCHAR(16), `price` DECIMAL(10,3), `siteid` INTEGER, `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;
Executing (default): CREATE TABLE IF NOT EXISTS `SequelizeMeta` (`name` VARCHAR(255) NOT NULL UNIQUE , PRIMARY KEY (`name`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;
Executing (default): SHOW INDEX FROM `SequelizeMeta`
Executing (default): INSERT INTO `SequelizeMeta` (`name`) VALUES ('20190417134836-mstprices.js');
== 20190417134836-mstprices: migrated (0.182s)

I could grab these SQLs (CREATE TABLE ..) to run on production later

Now that I'm using a latest version and trying the same db:migrate to create some other table, for which I do not see the raw query displayed.

npx sequelize-cli db:migrate --config "dbconfig.json" --env "localdb"

Sequelize CLI [Node: 10.16.0, CLI: 5.5.1, ORM: 4.44.3]

Loaded configuration file "dbconfig.json".
Using environment "localdb".
sequelize deprecated String based operators are now deprecated. Please use Symbol based operators for better security, read more at http://docs.sequelizejs.com/manual/tutorial/querying.html#operators node_modules\sequelize\lib\sequelize.js:245:13
== 20191210064127-create-tbl-service-time-log: migrating =======
== 20191210064127-create-tbl-service-time-log: migrated (0.185s)

Is that some setting or sequelize-cli stopped printing the raw query on the console?

How do I get the SQLs that sequelize-cli executes on the db?

nbs
  • 311
  • 1
  • 12

1 Answers1

0

From what I found while researching this question is: when you query entity Models in your migration files then you do have the SQL query dumped into the stdout. If you use queryInterface.XXXX or queryInterface.sequilize.query(XXXX) you will not have the SQL dumped to stdout.

And the correct choice for the migration files should be using queryInterface instead of the entity Models, as your models within some time may be changed and that will break your old migration files, you will start getting errors that some fields and/or references are not exists. queryInterface in turn doesn't not rely on Models, and just execute pure sql queries

Anton
  • 417
  • 3
  • 9