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

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() {
// 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)
}
......
......@@ -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) {
......
......@@ -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
......
......@@ -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
......
......@@ -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() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!