Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This commit allows you to disable logging for .sync() methods on Sequeli... #937

Merged
merged 5 commits into from Nov 3, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/dao-factory.js
Expand Up @@ -212,6 +212,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)
}

Expand Down Expand Up @@ -1020,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)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/dialects/postgres/connector-manager.js
Expand Up @@ -153,4 +153,4 @@ module.exports = (function() {
}

return ConnectorManager
})()
})()
2 changes: 1 addition & 1 deletion lib/dialects/postgres/query.js
Expand Up @@ -162,4 +162,4 @@ module.exports = (function() {
}

return Query
})()
})()
4 changes: 4 additions & 0 deletions lib/query-chainer.js
Expand Up @@ -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) {
Expand Down
27 changes: 17 additions & 10 deletions lib/query-interface.js
Expand Up @@ -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") {
Expand All @@ -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 }))
}
}

Expand All @@ -107,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)
Expand Down Expand Up @@ -137,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)
Expand All @@ -146,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) {
Expand All @@ -157,7 +163,7 @@ module.exports = (function() {
attributes = self.QueryGenerator.attributesToSQL(attributeHashes)
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)
emitter.emit('success', results)
}).error(function(err) {
Expand All @@ -181,7 +187,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
Expand All @@ -202,7 +208,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}])
}
}
}
Expand Down Expand Up @@ -812,7 +818,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) {
Expand All @@ -824,12 +831,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
Expand Down
2 changes: 2 additions & 0 deletions lib/sequelize.js
Expand Up @@ -329,6 +329,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
Expand Down
32 changes: 32 additions & 0 deletions test/sequelize.test.js
Expand Up @@ -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

Expand Down Expand Up @@ -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() {
Expand Down