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

Commit 3c29af31 by Jan Aagaard Meier

Merge pull request #937 from durango/sync-logging

This commit allows you to disable logging for .sync() methods on Sequeli...
2 parents 58a74fe9 feb41513
...@@ -212,6 +212,7 @@ module.exports = (function() { ...@@ -212,6 +212,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 }))
} }
} }
...@@ -107,7 +111,7 @@ module.exports = (function() { ...@@ -107,7 +111,7 @@ module.exports = (function() {
// If the enum type doesn't exist then create it // If the enum type doesn't exist then create it
if (!results[enumIdx]) { if (!results[enumIdx]) {
sql = self.QueryGenerator.pgEnum(getTableName, keys[i], attributes[keys[i]], options) 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) { else if (!!results[enumIdx] && !!daoTable) {
var enumVals = self.QueryGenerator.fromArray(results[enumIdx].enum_value) var enumVals = self.QueryGenerator.fromArray(results[enumIdx].enum_value)
...@@ -137,7 +141,7 @@ module.exports = (function() { ...@@ -137,7 +141,7 @@ module.exports = (function() {
sql = self.QueryGenerator.createTableQuery(tableName, attributes, options) sql = self.QueryGenerator.createTableQuery(tableName, attributes, options)
chainer2.run().success(function() { chainer2.run().success(function() {
queryAndEmit.call(self, sql, 'createTable') queryAndEmit.call(self, sql, 'createTable', options)
.success(function(res) { .success(function(res) {
self.emit('createTable', null) self.emit('createTable', null)
emitter.emit('success', res) emitter.emit('success', res)
...@@ -146,7 +150,9 @@ module.exports = (function() { ...@@ -146,7 +150,9 @@ module.exports = (function() {
self.emit('createTable', err) self.emit('createTable', err)
emitter.emit('error', err) emitter.emit('error', err)
}) })
.on('sql', function(sql) { emitter.emit('sql', sql) }) .on('sql', function(sql) {
emitter.emit('sql', sql)
})
}).error(function(err) { }).error(function(err) {
emitter.emit('error', err) emitter.emit('error', err)
}).on('sql', function(sql) { }).on('sql', function(sql) {
...@@ -157,7 +163,7 @@ module.exports = (function() { ...@@ -157,7 +163,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)
queryAndEmit.call(self, sql, 'createTable', emitter).success(function(results) { queryAndEmit.call(self, sql, 'createTable', options).success(function(results) {
self.emit('createTable', null) self.emit('createTable', null)
emitter.emit('success', results) emitter.emit('success', results)
}).error(function(err) { }).error(function(err) {
...@@ -181,7 +187,7 @@ module.exports = (function() { ...@@ -181,7 +187,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 +208,7 @@ module.exports = (function() { ...@@ -202,7 +208,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}])
} }
} }
} }
...@@ -812,7 +818,8 @@ module.exports = (function() { ...@@ -812,7 +818,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) {
...@@ -824,12 +831,12 @@ module.exports = (function() { ...@@ -824,12 +831,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
......
...@@ -329,6 +329,8 @@ module.exports = (function() { ...@@ -329,6 +329,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!