不要怂,就是干,撸起袖子干!

Commit 3dfa57ad by Daniel Durante

This commit allows you to disable logging for .sync() methods on Sequelize and D…

…AOFactories. This closes https://github.com/sequelize/sequelize/issues/763
1 parent abd0f717
...@@ -209,6 +209,7 @@ module.exports = (function() { ...@@ -209,6 +209,7 @@ module.exports = (function() {
// Only Postgres' QueryGenerator.dropTableQuery() will add schema manually // Only Postgres' QueryGenerator.dropTableQuery() will add schema manually
var isPostgres = this.options.dialect === "postgres" || (!!this.daoFactoryManager && this.daoFactoryManager.sequelize.options.dialect === "postgres") var isPostgres = this.options.dialect === "postgres" || (!!this.daoFactoryManager && this.daoFactoryManager.sequelize.options.dialect === "postgres")
, tableName = isPostgres ? this.tableName : this.getTableName() , tableName = isPostgres ? this.tableName : this.getTableName()
return this.QueryInterface.dropTable(tableName, options) return this.QueryInterface.dropTable(tableName, options)
} }
......
...@@ -75,6 +75,10 @@ module.exports = (function() { ...@@ -75,6 +75,10 @@ module.exports = (function() {
if (options.skipOnError && (self.fails.length > 0)) { if (options.skipOnError && (self.fails.length > 0)) {
onError('Skipped due to earlier error!') onError('Skipped due to earlier error!')
} else { } 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) var emitter = serial.klass[serial.method].apply(serial.klass, serial.params)
emitter.success(function(result) { emitter.success(function(result) {
......
...@@ -79,6 +79,10 @@ module.exports = (function() { ...@@ -79,6 +79,10 @@ module.exports = (function() {
} }
} }
options = Utils._.extend({
logging: this.sequelize.options.logging
}, options || {})
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
// Postgres requires a special SQL command for enums // Postgres requires a special SQL command for enums
if (self.sequelize.options.dialect === "postgres") { if (self.sequelize.options.dialect === "postgres") {
...@@ -90,7 +94,7 @@ module.exports = (function() { ...@@ -90,7 +94,7 @@ module.exports = (function() {
for (i = 0; i < keyLen; i++) { for (i = 0; i < keyLen; i++) {
if (attributes[keys[i]].toString().match(/^ENUM\(/)) { if (attributes[keys[i]].toString().match(/^ENUM\(/)) {
sql = self.QueryGenerator.pgListEnums(getTableName, keys[i], options) 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() { ...@@ -157,6 +161,7 @@ module.exports = (function() {
attributes = self.QueryGenerator.attributesToSQL(attributeHashes) attributes = self.QueryGenerator.attributesToSQL(attributeHashes)
sql = self.QueryGenerator.createTableQuery(tableName, attributes, options) 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', emitter).success(function(results) {
self.emit('createTable', null) self.emit('createTable', null)
emitter.emit('success', results) emitter.emit('success', results)
...@@ -181,7 +186,7 @@ module.exports = (function() { ...@@ -181,7 +186,7 @@ module.exports = (function() {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
var chainer = new Utils.QueryChainer() 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 // Since postgres has a special case for enums, we should drop the related
// enum type within the table and attribute // enum type within the table and attribute
...@@ -202,7 +207,7 @@ module.exports = (function() { ...@@ -202,7 +207,7 @@ module.exports = (function() {
for (i = 0; i < keyLen; i++) { for (i = 0; i < keyLen; i++) {
if (daoTable.rawAttributes[keys[i]].type && daoTable.rawAttributes[keys[i]].type === "ENUM") { 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() { ...@@ -604,10 +609,10 @@ module.exports = (function() {
}).run() }).run()
} }
QueryInterface.prototype.enableForeignKeyConstraints = function() { QueryInterface.prototype.enableForeignKeyConstraints = function(options) {
var sql = this.QueryGenerator.enableForeignKeyConstraintsQuery() var sql = this.QueryGenerator.enableForeignKeyConstraintsQuery()
if(sql) { if(sql) {
return queryAndEmit.call(this, sql, 'enableForeignKeyConstraints') return queryAndEmit.call(this, sql, 'enableForeignKeyConstraints', options)
} else { } else {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
this.emit('enableForeignKeyConstraints', null) this.emit('enableForeignKeyConstraints', null)
...@@ -616,10 +621,10 @@ module.exports = (function() { ...@@ -616,10 +621,10 @@ module.exports = (function() {
} }
} }
QueryInterface.prototype.disableForeignKeyConstraints = function() { QueryInterface.prototype.disableForeignKeyConstraints = function(options) {
var sql = this.QueryGenerator.disableForeignKeyConstraintsQuery() var sql = this.QueryGenerator.disableForeignKeyConstraintsQuery()
if(sql){ if(sql){
return queryAndEmit.call(this, sql, 'disableForeignKeyConstraints') return queryAndEmit.call(this, sql, 'disableForeignKeyConstraints', options)
} else { } else {
return new Utils.CustomEventEmitter(function(emitter) { return new Utils.CustomEventEmitter(function(emitter) {
this.emit('disableForeignKeyConstraints', null) this.emit('disableForeignKeyConstraints', null)
...@@ -669,7 +674,8 @@ module.exports = (function() { ...@@ -669,7 +674,8 @@ module.exports = (function() {
var queryAndEmit = QueryInterface.prototype.queryAndEmit = function(sqlOrQueryParams, methodName, options, emitter) { var queryAndEmit = QueryInterface.prototype.queryAndEmit = function(sqlOrQueryParams, methodName, options, emitter) {
options = Utils._.extend({ options = Utils._.extend({
success: function(){}, success: function(){},
error: function(){} error: function(){},
logging: this.sequelize.options.logging
}, options || {}) }, options || {})
var execQuery = function(emitter) { var execQuery = function(emitter) {
...@@ -681,12 +687,12 @@ module.exports = (function() { ...@@ -681,12 +687,12 @@ module.exports = (function() {
} }
if (sqlOrQueryParams.length === 2) { if (sqlOrQueryParams.length === 2) {
sqlOrQueryParams.push({}) sqlOrQueryParams.push(typeof options === "object" ? options : {})
} }
query = this.sequelize.query.apply(this.sequelize, sqlOrQueryParams) query = this.sequelize.query.apply(this.sequelize, sqlOrQueryParams)
} else { } else {
query = this.sequelize.query(sqlOrQueryParams, null, {}) query = this.sequelize.query(sqlOrQueryParams, null, options)
} }
// append the query for better testing // append the query for better testing
......
...@@ -302,6 +302,8 @@ module.exports = (function() { ...@@ -302,6 +302,8 @@ module.exports = (function() {
options = Utils._.extend({}, this.options.sync, options) options = Utils._.extend({}, this.options.sync, options)
} }
options.logging = options.logging === undefined ? false : Boolean(options.logging)
var chainer = new Utils.QueryChainer() var chainer = new Utils.QueryChainer()
// Topologically sort by foreign key constraints to give us an appropriate // Topologically sort by foreign key constraints to give us an appropriate
......
...@@ -7,6 +7,7 @@ var chai = require('chai') ...@@ -7,6 +7,7 @@ var chai = require('chai')
, Sequelize = require(__dirname + '/../index') , Sequelize = require(__dirname + '/../index')
, config = require(__dirname + "/config/config") , config = require(__dirname + "/config/config")
, moment = require('moment') , moment = require('moment')
, sinon = require('sinon')
chai.Assertion.includeStack = true chai.Assertion.includeStack = true
...@@ -360,6 +361,37 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () { ...@@ -360,6 +361,37 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
done() 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() { describe('drop should work', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!