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

Commit 0c114c36 by Jan Aagaard Meier

Removed all queryAndEmit calls from the regular queryinterface

1 parent 80503f1f
......@@ -5,6 +5,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell
- Sequelize now returns promises instead of its custom event emitter from most calls. This affects methods that return multiple values (like `findOrCreate` or `findOrInitialize`). If your current callbacks do not accept the 2nd success parameter you might be seeing an array as the first param. Either use `.spread()` for these methods or add another argument to your callback: `.success(instance)` -> `.success(instance, created)`.
- `.success()`/`.done()` and any other non promise methods are now deprecated (we will keep the codebase around for a few versions though). on('sql') persists for debugging purposes.
- Model association calls (belongsTo/hasOne/hasMany) are no longer chainable. (this is to support being able to pass association references to include rather than model/as combinations)
- `QueryInterface` no longer emits events
# v2.0.0-dev11
### Caution: This release contains many changes and is highly experimental
......
......@@ -102,7 +102,7 @@ Hooks.runHooks = function() {
})
if (fn) {
promise.spread(function () {
promise = promise.spread(function () {
fn.apply(self, [null].concat(Array.prototype.slice.apply(arguments)));
}, fn);
}
......
......@@ -648,17 +648,15 @@ module.exports = (function() {
// This semi awkward syntax where we can't return the chain directly but have to return the last .then() call is to allow sql proxying
return self.Model.runHooks(self.Model.options.hooks.beforeDestroy, self).then(function () {
var query
, identifier
var identifier
if (self.Model._timestampAttributes.deletedAt && options.force === false) {
self.dataValues[self.Model._timestampAttributes.deletedAt] = new Date()
query = self.save(options)
return self.save(options)
} else {
identifier = self.__options.hasPrimaryKeys ? self.primaryKeyValues : { id: self.id };
query = self.QueryInterface.delete(self, self.QueryInterface.QueryGenerator.addSchema(self.Model), identifier, options)
return self.QueryInterface.delete(self, self.QueryInterface.QueryGenerator.addSchema(self.Model), identifier, options)
}
return query;
}).then(function (results) {
return self.Model.runHooks(self.Model.options.hooks.afterDestroy, self).return(results);
});
......
......@@ -170,7 +170,7 @@ module.exports = (function() {
type: QueryTypes.SELECT
}, options || {})
return self.QueryInterface.queryAndEmit([result.toSql(), self, options], 'snafu')
return self.sequelize.query(result.toSql(), self, options)
}
return result
......@@ -393,7 +393,7 @@ module.exports = (function() {
if (options.force) {
return self.drop(options).then(function () {
return doQuery().return(self);
return doQuery().return(self)
});
} else {
return doQuery().return(this)
......@@ -1143,7 +1143,7 @@ module.exports = (function() {
}
if(this.sequelize.options.dialect === 'postgres' && options.ignoreDuplicates) {
return this.sequelize.Promise.reject(new Error('Postgres does not support the \'ignoreDuplicates\' option.'))
return Utils.Promise.reject(new Error('Postgres does not support the \'ignoreDuplicates\' option.'))
}
var self = this
......
......@@ -21,12 +21,12 @@ module.exports = (function() {
QueryInterface.prototype.createSchema = function(schema) {
var sql = this.QueryGenerator.createSchema(schema)
return queryAndEmit.call(this, sql, 'createSchema')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
}
QueryInterface.prototype.dropSchema = function(schema) {
var sql = this.QueryGenerator.dropSchema(schema)
return queryAndEmit.call(this, sql, 'dropSchema')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
}
QueryInterface.prototype.dropAllSchemas = function() {
......@@ -35,15 +35,9 @@ module.exports = (function() {
if (!this.QueryGenerator._dialect.supports.schemas) {
return this.sequelize.drop()
} else {
return new Utils.CustomEventEmitter(function(emitter) {
self.showAllSchemas().success(function(schemaNames) {
var chainer = new Utils.QueryChainer()
schemaNames.forEach(function(schemaName) {
chainer.add(self.dropSchema(schemaName))
})
chainer.run().proxy(emitter)
}).proxy(emitter, {events: ['sql', 'error']})
}).run()
return this.showAllSchemas().map(function (schemaName, index, length) {
return self.dropSchema(schemaName)
})
}
}
......@@ -55,16 +49,15 @@ module.exports = (function() {
raw: true
}, options || {})
return new Utils.CustomEventEmitter(function(emitter) {
var showSchemasSql = self.QueryGenerator.showSchemasQuery()
self.sequelize.query(showSchemasSql, null, options).success(function(schemaNames) {
self.emit('showAllSchemas', null)
emitter.emit('success', Utils._.flatten(Utils._.map(schemaNames, function(value){ return (!!value.schema_name ? value.schema_name : value) })))
}).error(function(err) {
self.emit('showAllSchemas', err)
emitter.emit('error', err)
})
}).run()
var showSchemasSql = self.QueryGenerator.showSchemasQuery()
return this.sequelize.query(showSchemasSql, null, options).then(function (schemaNames) {
return Utils._.flatten(
Utils._.map(schemaNames, function(value){
return (!!value.schema_name ? value.schema_name : value)
})
)
})
}
QueryInterface.prototype.createTable = function(tableName, attributes, options) {
......@@ -145,14 +138,14 @@ module.exports = (function() {
sql = self.QueryGenerator.createTableQuery(tableName, attributes, options)
return Promise.all(promises).then(function() {
return queryAndEmit.call(self, sql, 'createTable', options)
return self.sequelize.query(sql, null, options)
})
})
} else {
attributes = self.QueryGenerator.attributesToSQL(attributeHashes)
sql = self.QueryGenerator.createTableQuery(tableName, attributes, options)
return queryAndEmit.call(self, sql, 'createTable', options)
return self.sequelize.query(sql, null, options)
}
}
......@@ -164,19 +157,15 @@ module.exports = (function() {
var sql = this.QueryGenerator.dropTableQuery(tableName, options)
, self = this
return queryAndEmit.call(this, sql, 'dropTable', options).then(function () {
return this.sequelize.query(sql, null, options).then(function () {
var promises = []
// Since postgres has a special case for enums, we should drop the related
// enum type within the table and attribute
if (self.sequelize.options.dialect === "postgres") {
// Find the table that we're trying to drop
daoTable = self.sequelize.daoFactoryManager.daos.filter(function(dao) {
return dao.tableName === tableName
})
// Find the table that we're trying to drop
daoTable = self.sequelize.daoFactoryManager.getDAO(tableName, 'tableName')
// Just in case if we're trying to drop a non-existing table
daoTable = daoTable.length > 0 ? daoTable[0] : null
if (!!daoTable) {
var getTableName = (!options || !options.schema || options.schema === "public" ? '' : options.schema + '_') + tableName
......@@ -192,9 +181,7 @@ module.exports = (function() {
}
}
return Promise.all(promises).then(function (results) {
return results[0];
});
return Promise.all(promises).get(0);
})
}
......@@ -220,7 +207,6 @@ module.exports = (function() {
}
self.showAllTables().success(function(tableNames) {
self.getForeignKeysForTables(tableNames).success(function(foreignKeys) {
// add the foreign key removal query to the chainer
Object.keys(foreignKeys).forEach(function(tableName) {
foreignKeys[tableName].forEach(function(foreignKey) {
......@@ -248,37 +234,26 @@ module.exports = (function() {
QueryInterface.prototype.dropAllEnums = function(options) {
if (this.sequelize.getDialect() !== 'postgres') {
return new Utils.CustomEventEmitter(function (emitter) {
emitter.emit('success')
}).run()
return Utils.Promise.resolve()
}
options = options || {}
var self = this
, emitter = new Utils.CustomEventEmitter()
, chainer = new Utils.QueryChainer()
, sql = this.QueryGenerator.pgListEnums()
this.sequelize.query(sql, null, { plain: false, raw: true, type: QueryTypes.SELECT, logging: options.logging })
.proxy(emitter, {events: ['sql', 'error']})
.success(function (results) {
results.forEach(function (result) {
chainer.add(self.sequelize.query(
self.QueryGenerator.pgEnumDrop(null, null, self.QueryGenerator.pgEscapeAndQuote(result.enum_name)),
null,
{logging: options.logging, raw: true}
))
})
chainer.run().proxy(emitter)
})
return emitter
return this.sequelize.query(sql, null, { plain: false, raw: true, type: QueryTypes.SELECT, logging: options.logging }).map(function (result) {
return self.sequelize.query(
self.QueryGenerator.pgEnumDrop(null, null, self.QueryGenerator.pgEscapeAndQuote(result.enum_name)),
null,
{logging: options.logging, raw: true}
)
})
}
QueryInterface.prototype.renameTable = function(before, after) {
var sql = this.QueryGenerator.renameTableQuery(before, after)
return queryAndEmit.call(this, sql, 'renameTable')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
}
QueryInterface.prototype.showAllTables = function(options) {
......@@ -341,7 +316,8 @@ module.exports = (function() {
var options = this.QueryGenerator.attributesToSQL(attributes)
, sql = this.QueryGenerator.addColumnQuery(tableName, options)
return queryAndEmit.call(this, sql, 'addColumn')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
}
QueryInterface.prototype.removeColumn = function(tableName, attributeName) {
......@@ -350,7 +326,7 @@ module.exports = (function() {
return SQLiteQueryInterface.removeColumn.call(this, tableName, attributeName, null, queryAndEmit)
} else {
var sql = this.QueryGenerator.removeColumnQuery(tableName, attributeName)
return queryAndEmit.call(this, sql, 'removeColumn')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
}
}
......@@ -370,7 +346,7 @@ module.exports = (function() {
var options = this.QueryGenerator.attributesToSQL(attributes)
, sql = this.QueryGenerator.changeColumnQuery(tableName, options)
return queryAndEmit.call(this, sql, 'changeColumn')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
}
}
......@@ -400,59 +376,52 @@ module.exports = (function() {
attrNameBefore,
this.QueryGenerator.attributesToSQL(options)
)
return queryAndEmit.call(this, sql, 'renameColumn', {})
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
}
}.bind(this));
}
QueryInterface.prototype.addIndex = function(tableName, attributes, options) {
var sql = this.QueryGenerator.addIndexQuery(tableName, attributes, options)
return queryAndEmit.call(this, sql, 'addIndex')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
}
QueryInterface.prototype.showIndex = function(tableName, options) {
var sql = this.QueryGenerator.showIndexQuery(tableName, options)
return queryAndEmit.call(this, sql, 'showIndex')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
}
QueryInterface.prototype.getForeignKeysForTables = function(tableNames) {
var self = this
return new Utils.CustomEventEmitter(function(emitter) {
if (tableNames.length === 0) {
emitter.emit('success', {})
} else {
var chainer = new Utils.QueryChainer()
tableNames.forEach(function(tableName) {
var sql = self.QueryGenerator.getForeignKeysQuery(tableName, self.sequelize.config.database)
chainer.add(self.sequelize, 'query', [sql])
})
chainer.runSerially().proxy(emitter, {
skipEvents: ['success']
}).success(function(results) {
var result = {}
if (tableNames.length === 0) {
return Utils.Promise.resolve({})
}
tableNames.forEach(function(tableName, i) {
result[tableName] = Utils._.compact(results[i]).map(function(r) { return r.constraint_name })
})
return Utils.Promise.map(tableNames, function (tableName) {
return self.sequelize.query(self.QueryGenerator.getForeignKeysQuery(tableName, self.sequelize.config.database))
}).then(function (results) {
var result = {}
emitter.emit('success', result)
tableNames.forEach(function(tableName, i) {
result[tableName] = Utils._.compact(results[i]).map(function(r) {
return r.constraint_name
})
}
}).run()
})
return result
})
}
QueryInterface.prototype.removeIndex = function(tableName, indexNameOrAttributes) {
var sql = this.QueryGenerator.removeIndexQuery(tableName, indexNameOrAttributes)
return queryAndEmit.call(this, sql, "removeIndex")
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
}
QueryInterface.prototype.insert = function(dao, tableName, values, options) {
var sql = this.QueryGenerator.insertQuery(tableName, values, dao.Model.rawAttributes);
return queryAndEmit.call(this, [sql, dao, options], 'insert').then(function (result) {
return this.sequelize.query(sql, dao, options).then(function (result) {
result.isNewRecord = false;
return result;
});
......@@ -460,7 +429,7 @@ module.exports = (function() {
QueryInterface.prototype.bulkInsert = function(tableName, records, options, Model) {
var sql = this.QueryGenerator.bulkInsertQuery(tableName, records, options, Model.rawAttributes)
return queryAndEmit.call(this, [sql, null, options], 'bulkInsert')
return this.sequelize.query(sql, null, options)
}
QueryInterface.prototype.update = function(dao, tableName, values, identifier, options) {
......@@ -480,13 +449,13 @@ module.exports = (function() {
}
}
return this.queryAndEmit([sql, dao, options], 'update')
return this.sequelize.query(sql, dao, options)
}
QueryInterface.prototype.bulkUpdate = function(tableName, values, identifier, options, attributes) {
var sql = this.QueryGenerator.updateQuery(tableName, values, identifier, options, attributes)
return this.queryAndEmit([sql, null, options], 'bulkUpdate')
return this.sequelize.query(sql, null, options)
}
QueryInterface.prototype.delete = function(dao, tableName, identifier, options) {
......@@ -512,7 +481,7 @@ module.exports = (function() {
}
}
return new Promise(function (resolve, reject) {
return new Utils.Promise(function (resolve, reject) {
var tick = 0
var iterate = function(err, i) {
if (err) {
......@@ -561,14 +530,13 @@ module.exports = (function() {
resolve();
}
}).then(function () {
return self.queryAndEmit([sql, dao, options], 'delete');
return self.sequelize.query(sql, dao, options)
});
}
QueryInterface.prototype.bulkDelete = function(tableName, identifier, options) {
var sql = this.QueryGenerator.deleteQuery(tableName, identifier, Utils._.defaults(options || {}, {limit: null}))
return this.queryAndEmit([sql, null, options], 'bulkDelete', options)
return this.sequelize.query(sql, null, options)
}
QueryInterface.prototype.select = function(factory, tableName, options, queryOptions) {
......@@ -601,12 +569,12 @@ module.exports = (function() {
originalAttributes: options.originalAttributes
})
return queryAndEmit.call(this, [sql, factory, queryOptions], 'select')
return this.sequelize.query(sql, factory, queryOptions)
}
QueryInterface.prototype.increment = function(dao, tableName, values, identifier, options) {
var sql = this.QueryGenerator.incrementQuery(tableName, values, identifier, options.attributes)
return queryAndEmit.call(this, [sql, dao, options], 'increment')
return this.sequelize.query(sql, dao, options)
}
QueryInterface.prototype.rawSelect = function(tableName, options, attributeSelector, Model) {
......@@ -640,75 +608,57 @@ module.exports = (function() {
QueryInterface.prototype.createTrigger = function(tableName, triggerName, timingType, fireOnArray,
functionName, functionParams, optionsArray) {
var sql = this.QueryGenerator.createTrigger(tableName, triggerName, timingType, fireOnArray, functionName
, functionParams, optionsArray)
var sql = this.QueryGenerator.createTrigger(tableName, triggerName, timingType, fireOnArray, functionName
, functionParams, optionsArray)
if (sql){
return queryAndEmit.call(this, sql, 'createTrigger')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
} else {
return new Utils.CustomEventEmitter(function(emitter) {
this.emit('createTrigger', null)
emitter.emit('success')
}).run()
return Utils.Promise.resolve()
}
}
QueryInterface.prototype.dropTrigger = function(tableName, triggerName) {
var sql = this.QueryGenerator.dropTrigger(tableName, triggerName)
var sql = this.QueryGenerator.dropTrigger(tableName, triggerName)
if (sql){
return queryAndEmit.call(this, sql, 'dropTrigger')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
} else {
return new Utils.CustomEventEmitter(function(emitter) {
this.emit('dropTrigger', null)
emitter.emit('success')
}).run()
return Utils.Promise.resolve()
}
}
QueryInterface.prototype.renameTrigger = function(tableName, oldTriggerName, newTriggerName) {
var sql = this.QueryGenerator.renameTrigger(tableName, oldTriggerName, newTriggerName)
var sql = this.QueryGenerator.renameTrigger(tableName, oldTriggerName, newTriggerName)
if (sql){
return queryAndEmit.call(this, sql, 'renameTrigger')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
} else {
return new Utils.CustomEventEmitter(function(emitter) {
this.emit('renameTrigger', null)
emitter.emit('success')
}).run()
return Utils.Promise.resolve()
}
}
QueryInterface.prototype.createFunction = function(functionName, params, returnType, language, body, options) {
var sql = this.QueryGenerator.createFunction(functionName, params, returnType, language, body, options)
var sql = this.QueryGenerator.createFunction(functionName, params, returnType, language, body, options)
if (sql){
return queryAndEmit.call(this, sql, 'createFunction')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
} else {
return new Utils.CustomEventEmitter(function(emitter) {
this.emit('createFunction', null)
emitter.emit('success')
}).run()
return Utils.Promise.resolve()
}
}
QueryInterface.prototype.dropFunction = function(functionName, params) {
var sql = this.QueryGenerator.dropFunction(functionName, params)
var sql = this.QueryGenerator.dropFunction(functionName, params)
if (sql){
return queryAndEmit.call(this, sql, 'dropFunction')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
} else {
return new Utils.CustomEventEmitter(function(emitter) {
this.emit('dropFunction', null)
emitter.emit('success')
}).run()
return Utils.Promise.resolve()
}
}
QueryInterface.prototype.renameFunction = function(oldFunctionName, params, newFunctionName) {
var sql = this.QueryGenerator.renameFunction(oldFunctionName, params, newFunctionName)
var sql = this.QueryGenerator.renameFunction(oldFunctionName, params, newFunctionName)
if (sql){
return queryAndEmit.call(this, sql, 'renameFunction')
return this.sequelize.query(sql, null, {logging: this.sequelize.options.logging})
} else {
return new Utils.CustomEventEmitter(function(emitter) {
this.emit('renameFunction', null)
emitter.emit('success')
}).run()
return Utils.Promise.resolve()
}
}
......@@ -727,7 +677,6 @@ module.exports = (function() {
return this.QueryGenerator.quoteTable(identifier)
}
/**
* Split an identifier into .-separated tokens and quote each part.
* If force is true, the identifier will be quoted even if the
......@@ -750,7 +699,7 @@ module.exports = (function() {
}
var sql = this.QueryGenerator.setAutocommitQuery(value)
return this.queryAndEmit([sql, null, { transaction: transaction }], 'setAutocommit')
return this.sequelize.query(sql, null, { transaction: transaction})
}
QueryInterface.prototype.setIsolationLevel = function(transaction, value) {
......@@ -759,7 +708,7 @@ module.exports = (function() {
}
var sql = this.QueryGenerator.setIsolationLevelQuery(value)
return this.queryAndEmit([sql, null, { transaction: transaction }], 'setIsolationLevel')
return this.sequelize.query(sql, null, { transaction: transaction })
}
QueryInterface.prototype.startTransaction = function(transaction, options) {
......@@ -772,7 +721,7 @@ module.exports = (function() {
}, options || {})
var sql = this.QueryGenerator.startTransactionQuery(options)
return this.queryAndEmit([sql, null, options], 'startTransaction')
return this.sequelize.query(sql, null, options)
}
QueryInterface.prototype.commitTransaction = function(transaction, options) {
......@@ -785,7 +734,7 @@ module.exports = (function() {
}, options || {})
var sql = this.QueryGenerator.commitTransactionQuery(options)
return this.queryAndEmit([sql, null, options], 'commitTransaction')
return this.sequelize.query(sql, null, options)
}
QueryInterface.prototype.rollbackTransaction = function(transaction, options) {
......@@ -798,7 +747,7 @@ module.exports = (function() {
}, options || {})
var sql = this.QueryGenerator.rollbackTransactionQuery(options)
return this.queryAndEmit([sql, null, options], 'rollbackTransaction')
return this.sequelize.query(sql, null, options)
}
// private
......
......@@ -361,9 +361,7 @@ describe(Support.getTestDialectTeaser("BelongsTo"), function() {
Task.create({ title: 'task' }).success(function(task) {
task.setUser(user).success(function() {
// Should fail due to FK restriction
user.destroy().then(function () {
assert(false);
}, function(err) {
user.destroy().catch(function(err) {
expect(err).to.be.ok;
Task.findAll().success(function(tasks) {
expect(tasks).to.have.length(1)
......
......@@ -98,10 +98,10 @@ describe(Support.getTestDialectTeaser("Sequelize"), function () {
if (dialect === 'mariadb') {
expect(err.message).to.match(/Access denied for user/)
} else if (dialect === 'postgres') {
// When the test is run with only it produces:
// Error: Error: Failed to authenticate for PostgresSQL. Please double check your settings.
// When its run with all the other tests it produces:
// Error: invalid port number: "99999"
// When the test is run with only it produces:
// Error: Error: Failed to authenticate for PostgresSQL. Please double check your settings.
// When its run with all the other tests it produces:
// Error: invalid port number: "99999"
expect(err.message).to.match(/invalid port number/)
} else {
expect(err.message).to.match(/Failed to authenticate/)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!