From 3dfa57ade24b5de42a5b31de188c7feb115cc57c Mon Sep 17 00:00:00 2001 From: Daniel Durante Date: Tue, 24 Sep 2013 13:01:26 -0400 Subject: [PATCH 1/2] This commit allows you to disable logging for .sync() methods on Sequelize and DAOFactories. This closes https://github.com/sequelize/sequelize/issues/763 --- lib/dao-factory.js | 1 + lib/query-chainer.js | 4 ++++ lib/query-interface.js | 26 ++++++++++++++++---------- lib/sequelize.js | 2 ++ test/sequelize.test.js | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 55 insertions(+), 10 deletions(-) diff --git a/lib/dao-factory.js b/lib/dao-factory.js index 4e8a28649524..9bd28f3a79a4 100644 --- a/lib/dao-factory.js +++ b/lib/dao-factory.js @@ -209,6 +209,7 @@ module.exports = (function() { // Only Postgres' QueryGenerator.dropTableQuery() will add schema manually var isPostgres = this.options.dialect === "postgres" || (!!this.daoFactoryManager && this.daoFactoryManager.sequelize.options.dialect === "postgres") , tableName = isPostgres ? this.tableName : this.getTableName() + return this.QueryInterface.dropTable(tableName, options) } diff --git a/lib/query-chainer.js b/lib/query-chainer.js index f55fc53419ac..70ad14fc1904 100644 --- a/lib/query-chainer.js +++ b/lib/query-chainer.js @@ -75,6 +75,10 @@ module.exports = (function() { if (options.skipOnError && (self.fails.length > 0)) { onError('Skipped due to earlier error!') } else { + if (typeof serial.options === "object" && Object.keys(serial.options).length > 0 && serial.method === "queryAndEmit") { + serial.params.push(serial.options) + } + var emitter = serial.klass[serial.method].apply(serial.klass, serial.params) emitter.success(function(result) { diff --git a/lib/query-interface.js b/lib/query-interface.js index 0155991434ee..2c3a234a40ef 100644 --- a/lib/query-interface.js +++ b/lib/query-interface.js @@ -79,6 +79,10 @@ module.exports = (function() { } } + options = Utils._.extend({ + logging: this.sequelize.options.logging + }, options || {}) + return new Utils.CustomEventEmitter(function(emitter) { // Postgres requires a special SQL command for enums if (self.sequelize.options.dialect === "postgres") { @@ -90,7 +94,7 @@ module.exports = (function() { for (i = 0; i < keyLen; i++) { if (attributes[keys[i]].toString().match(/^ENUM\(/)) { sql = self.QueryGenerator.pgListEnums(getTableName, keys[i], options) - chainer.add(self.sequelize.query(sql, null, { plain: true, raw: true, type: 'SELECT' })) + chainer.add(self.sequelize.query(sql, null, { plain: true, raw: true, type: 'SELECT', logging: options.logging })) } } @@ -157,6 +161,7 @@ module.exports = (function() { attributes = self.QueryGenerator.attributesToSQL(attributeHashes) sql = self.QueryGenerator.createTableQuery(tableName, attributes, options) + emitter.logging = options.logging queryAndEmit.call(self, sql, 'createTable', emitter).success(function(results) { self.emit('createTable', null) emitter.emit('success', results) @@ -181,7 +186,7 @@ module.exports = (function() { return new Utils.CustomEventEmitter(function(emitter) { var chainer = new Utils.QueryChainer() - chainer.add(self, 'queryAndEmit', [sql]) + chainer.add(self, 'queryAndEmit', [sql, 'dropTable'], options) // Since postgres has a special case for enums, we should drop the related // enum type within the table and attribute @@ -202,7 +207,7 @@ module.exports = (function() { for (i = 0; i < keyLen; i++) { if (daoTable.rawAttributes[keys[i]].type && daoTable.rawAttributes[keys[i]].type === "ENUM") { - chainer.add(self.sequelize, 'query', [self.QueryGenerator.pgEnumDrop(getTableName, keys[i]), null, {raw: true}]) + chainer.add(self.sequelize, 'query', [self.QueryGenerator.pgEnumDrop(getTableName, keys[i]), null, {logging: options.logging, raw: true}]) } } } @@ -604,10 +609,10 @@ module.exports = (function() { }).run() } - QueryInterface.prototype.enableForeignKeyConstraints = function() { + QueryInterface.prototype.enableForeignKeyConstraints = function(options) { var sql = this.QueryGenerator.enableForeignKeyConstraintsQuery() if(sql) { - return queryAndEmit.call(this, sql, 'enableForeignKeyConstraints') + return queryAndEmit.call(this, sql, 'enableForeignKeyConstraints', options) } else { return new Utils.CustomEventEmitter(function(emitter) { this.emit('enableForeignKeyConstraints', null) @@ -616,10 +621,10 @@ module.exports = (function() { } } - QueryInterface.prototype.disableForeignKeyConstraints = function() { + QueryInterface.prototype.disableForeignKeyConstraints = function(options) { var sql = this.QueryGenerator.disableForeignKeyConstraintsQuery() if(sql){ - return queryAndEmit.call(this, sql, 'disableForeignKeyConstraints') + return queryAndEmit.call(this, sql, 'disableForeignKeyConstraints', options) } else { return new Utils.CustomEventEmitter(function(emitter) { this.emit('disableForeignKeyConstraints', null) @@ -669,7 +674,8 @@ module.exports = (function() { var queryAndEmit = QueryInterface.prototype.queryAndEmit = function(sqlOrQueryParams, methodName, options, emitter) { options = Utils._.extend({ success: function(){}, - error: function(){} + error: function(){}, + logging: this.sequelize.options.logging }, options || {}) var execQuery = function(emitter) { @@ -681,12 +687,12 @@ module.exports = (function() { } if (sqlOrQueryParams.length === 2) { - sqlOrQueryParams.push({}) + sqlOrQueryParams.push(typeof options === "object" ? options : {}) } query = this.sequelize.query.apply(this.sequelize, sqlOrQueryParams) } else { - query = this.sequelize.query(sqlOrQueryParams, null, {}) + query = this.sequelize.query(sqlOrQueryParams, null, options) } // append the query for better testing diff --git a/lib/sequelize.js b/lib/sequelize.js index 16999094676a..d950d928900b 100644 --- a/lib/sequelize.js +++ b/lib/sequelize.js @@ -302,6 +302,8 @@ module.exports = (function() { options = Utils._.extend({}, this.options.sync, options) } + options.logging = options.logging === undefined ? false : Boolean(options.logging) + var chainer = new Utils.QueryChainer() // Topologically sort by foreign key constraints to give us an appropriate diff --git a/test/sequelize.test.js b/test/sequelize.test.js index 3ed1f952dffa..b300a3d731c2 100644 --- a/test/sequelize.test.js +++ b/test/sequelize.test.js @@ -7,6 +7,7 @@ var chai = require('chai') , Sequelize = require(__dirname + '/../index') , config = require(__dirname + "/config/config") , moment = require('moment') + , sinon = require('sinon') chai.Assertion.includeStack = true @@ -360,6 +361,37 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () { done() }) }) + + describe("doesn't emit logging when explicitly saying not to", function() { + afterEach(function(done) { + this.sequelize.options.logging = false + done() + }) + + beforeEach(function(done) { + this.spy = sinon.spy() + var self = this + this.sequelize.options.logging = function() { self.spy() } + this.User = this.sequelize.define('UserTest', { username: DataTypes.STRING }) + done() + }) + + it('through Sequelize.sync()', function(done) { + var self = this + this.sequelize.sync({ force: true, logging: false }).success(function() { + expect(self.spy.notCalled).to.be.true + done() + }) + }) + + it('through DAOFactory.sync()', function(done) { + var self = this + this.User.sync({ force: true, logging: false }).success(function() { + expect(self.spy.notCalled).to.be.true + done() + }) + }) + }) }) describe('drop should work', function() { From feb41513c18dbdc59d3f58f95a36c2953642b1ba Mon Sep 17 00:00:00 2001 From: Daniel Durante Date: Mon, 28 Oct 2013 23:31:46 -0400 Subject: [PATCH 2/2] Fixed console.log options for postgres. --- lib/dao-factory.js | 2 +- lib/dialects/postgres/connector-manager.js | 2 +- lib/dialects/postgres/query.js | 2 +- lib/query-interface.js | 11 ++++++----- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/dao-factory.js b/lib/dao-factory.js index 029b1b7bf86a..fa0fca4b6e49 100644 --- a/lib/dao-factory.js +++ b/lib/dao-factory.js @@ -1021,7 +1021,7 @@ module.exports = (function() { } DAOFactory.prototype.__setSqlDialect = function() { - var dialect = this.daoFactoryManager.sequelize.options.dialect + var dialect = this.daoFactoryManager.sequelize.options.dialect this.__sql = sql.setDialect(dialect === 'mariadb' ? 'mysql' : dialect) } diff --git a/lib/dialects/postgres/connector-manager.js b/lib/dialects/postgres/connector-manager.js index 96709aac5b01..d44c8d0e715e 100644 --- a/lib/dialects/postgres/connector-manager.js +++ b/lib/dialects/postgres/connector-manager.js @@ -153,4 +153,4 @@ module.exports = (function() { } return ConnectorManager -})() \ No newline at end of file +})() diff --git a/lib/dialects/postgres/query.js b/lib/dialects/postgres/query.js index 61f0854e3f77..49a21ff9d51d 100644 --- a/lib/dialects/postgres/query.js +++ b/lib/dialects/postgres/query.js @@ -162,4 +162,4 @@ module.exports = (function() { } return Query -})() \ No newline at end of file +})() diff --git a/lib/query-interface.js b/lib/query-interface.js index 968d8995edea..c7e9b815b341 100644 --- a/lib/query-interface.js +++ b/lib/query-interface.js @@ -111,7 +111,7 @@ module.exports = (function() { // If the enum type doesn't exist then create it if (!results[enumIdx]) { sql = self.QueryGenerator.pgEnum(getTableName, keys[i], attributes[keys[i]], options) - chainer2.add(self.sequelize.query(sql, null, { raw: true })) + chainer2.add(self.sequelize.query(sql, null, { raw: true, logging: options.logging })) } else if (!!results[enumIdx] && !!daoTable) { var enumVals = self.QueryGenerator.fromArray(results[enumIdx].enum_value) @@ -141,7 +141,7 @@ module.exports = (function() { sql = self.QueryGenerator.createTableQuery(tableName, attributes, options) chainer2.run().success(function() { - queryAndEmit.call(self, sql, 'createTable') + queryAndEmit.call(self, sql, 'createTable', options) .success(function(res) { self.emit('createTable', null) emitter.emit('success', res) @@ -150,7 +150,9 @@ module.exports = (function() { self.emit('createTable', err) emitter.emit('error', err) }) - .on('sql', function(sql) { emitter.emit('sql', sql) }) + .on('sql', function(sql) { + emitter.emit('sql', sql) + }) }).error(function(err) { emitter.emit('error', err) }).on('sql', function(sql) { @@ -161,8 +163,7 @@ module.exports = (function() { attributes = self.QueryGenerator.attributesToSQL(attributeHashes) sql = self.QueryGenerator.createTableQuery(tableName, attributes, options) - emitter.logging = options.logging - queryAndEmit.call(self, sql, 'createTable', emitter).success(function(results) { + queryAndEmit.call(self, sql, 'createTable', options).success(function(results) { self.emit('createTable', null) emitter.emit('success', results) }).error(function(err) {