Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging Sequelize Migrations

Tags:

sequelize.js

Just getting started with Sequelize and trying to get migrations working and would like to see what SQL the migrations are actually running against the DB.

I found a github comment which suggests how to pipe the logging to the debug module, but where do I put the "options".

options: {
  logging: debug('sequelize')
}
like image 557
Brad Robinson Avatar asked Nov 26 '15 22:11

Brad Robinson


People also ask

How do I log a Sequelize query?

Log SQL Query to the Console Here's how: Sequelize supports the logging option when composing a query object. This configuration overrides the default logging setup in your SQL connector. The logging option expects a log function, like console. log which receives the generates SQL statement.


3 Answers

Enable "logging":true in config conection data base (file data_base.json).

Example

{
    "username": "postgres",
    "password": "postgres",
    "database": "agencias",
    "host": "127.0.0.1",
    "dialect": "postgres",
    "timezone": "America/La_Paz",

    "migrationStorage": "sequelize",
    "migrationStorageTableName": "sequelize_migration",
    "logging":true,
    "pool": {
        "max": 15,
        "min": 0,
        "idle": 10000
    }
}

Execute in the project

sequelize db:migrate --migrations-path src/migrations --config data_base.json
like image 181
Ronald Coarite Avatar answered Sep 22 '22 19:09

Ronald Coarite


There are a few possible places to add this for sequelize. In the .sequelizerc if you want to see all logging for the sequelize CLI (See documentation) . If you just want to see it for specific queries in your up/down migrations, then you can pass it in for each queryInterface call as part of options.

For example, for createTable you can do:

queryInterface.createTable('myNewTable', {
        'my_column': Sequelize.STRING
    }, {
        logging: console.log
});
like image 42
javaguy01 Avatar answered Sep 22 '22 19:09

javaguy01


Sequelize CLI passes the configuration defined in config/config.js to the sequelize constructor. Use the following to log SQL queries when running sequelize db:migrate

'use strict';

module.exports = {
    development: {
        host: process.env.MSSQL_SERVER || 'localhost',
        username: process.env.MSSQL_USER || 'MSSQL_USER',
        password: process.env.MSSQL_USER_PASS || 'MSSQL_PASS',
        database: process.env.MSSQL_DB || 'DATABASE_NAME',
        dialect: process.env.DIALECT || 'mssql',
        logging: console.log
    }
};
like image 26
wdphd Avatar answered Sep 25 '22 19:09

wdphd