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

Commit 9369ba9e by Jan Aagaard Meier

Fix logging options for sequelize.sync (closes #1783), and convert some more code to promises

1 parent 1c43cceb
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
"describe": false, "describe": false,
"expect": false, "expect": false,
"beforeEach": false, "beforeEach": false,
"afterEach": false,
"waits": false, "waits": false,
"waitsFor": false, "waitsFor": false,
"runs": false "runs": false
......
...@@ -9,6 +9,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell ...@@ -9,6 +9,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell
- [FEATURE] Support for FOR UPDATE and FOR SHARE statements [#1777](https://github.com/sequelize/sequelize/pull/1777) - [FEATURE] Support for FOR UPDATE and FOR SHARE statements [#1777](https://github.com/sequelize/sequelize/pull/1777)
- [FEATURE] n:m createAssocation now returns the target model instance instead of the join model instance - [FEATURE] n:m createAssocation now returns the target model instance instead of the join model instance
- [BUG] An error is now thrown if an association would create a naming conflict between the association and the foreign key when doing eager loading. Closes [#1272](https://github.com/sequelize/sequelize/issues/1272) - [BUG] An error is now thrown if an association would create a naming conflict between the association and the foreign key when doing eager loading. Closes [#1272](https://github.com/sequelize/sequelize/issues/1272)
- [BUG] Fix logging options for sequelize.sync
- [INTERNALS] `bulkDeleteQuery` was removed from the MySQL / abstract query generator, since it was never used internally. Please use `deleteQuery` instead. - [INTERNALS] `bulkDeleteQuery` was removed from the MySQL / abstract query generator, since it was never used internally. Please use `deleteQuery` instead.
#### Breaking changes #### Breaking changes
...@@ -16,6 +17,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell ...@@ -16,6 +17,7 @@ Notice: All 1.7.x changes are present in 2.0.x aswell
- `.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. - `.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) - 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 global events. This means you can no longer do things like `QueryInterface.on('showAllSchemas', function ... ` - `QueryInterface` no longer emits global events. This means you can no longer do things like `QueryInterface.on('showAllSchemas', function ... `
- `sequelize.showAllSchemas` now returns an array of schemas, instead of an array containinig an array of schemas
# v2.0.0-dev11 # v2.0.0-dev11
### Caution: This release contains many changes and is highly experimental ### Caution: This release contains many changes and is highly experimental
......
...@@ -103,8 +103,7 @@ var InstanceValidator = module.exports = function(modelInstance, options) { ...@@ -103,8 +103,7 @@ var InstanceValidator = module.exports = function(modelInstance, options) {
}); });
this.modelInstance = modelInstance this.modelInstance = modelInstance
this.chainer = new Utils.QueryChainer()
/** /**
* Exposes a reference to validator.js. This allows you to add custom validations using `Validator.extend` * Exposes a reference to validator.js. This allows you to add custom validations using `Validator.extend`
* @name Validator * @name Validator
...@@ -131,7 +130,7 @@ InstanceValidator.RAW_KEY_NAME = '__raw' ...@@ -131,7 +130,7 @@ InstanceValidator.RAW_KEY_NAME = '__raw'
/** /**
* The main entry point for the Validation module, invoke to start the dance. * The main entry point for the Validation module, invoke to start the dance.
* *
* @return {EventEmitter} * @return {Promise}
*/ */
InstanceValidator.prototype.validate = function() { InstanceValidator.prototype.validate = function() {
if (this.inProgress) { if (this.inProgress) {
......
...@@ -460,7 +460,7 @@ module.exports = (function() { ...@@ -460,7 +460,7 @@ module.exports = (function() {
* @param {Transaction} [options.transaction=null] The transaction that the query should be executed under * @param {Transaction} [options.transaction=null] The transaction that the query should be executed under
* @param [String] [options.type='SELECT'] The type of query you are executing. The query type affects how results are formatted before they are passed back. If no type is provided sequelize will try to guess the right type based on the sql, and fall back to SELECT. The type is a string, but `Sequelize.QueryTypes` is provided is convenience shortcuts. Current options are SELECT, BULKUPDATE and BULKDELETE * @param [String] [options.type='SELECT'] The type of query you are executing. The query type affects how results are formatted before they are passed back. If no type is provided sequelize will try to guess the right type based on the sql, and fall back to SELECT. The type is a string, but `Sequelize.QueryTypes` is provided is convenience shortcuts. Current options are SELECT, BULKUPDATE and BULKDELETE
* @param {Object|Array} [replacements] Either an object of named parameter replacements in the format `:param` or an array of unnamed replacements to replace `?` in your SQL. * @param {Object|Array} [replacements] Either an object of named parameter replacements in the format `:param` or an array of unnamed replacements to replace `?` in your SQL.
* @return {EventEmitter} * @return {Promise}
* *
* @see {Model#build} for more information about callee. * @see {Model#build} for more information about callee.
*/ */
...@@ -492,44 +492,32 @@ module.exports = (function() { ...@@ -492,44 +492,32 @@ module.exports = (function() {
/** /**
* Create a new database schema * Create a new database schema
* @param {String} schema Name of the schema * @param {String} schema Name of the schema
* @return {EventEmitter} * @return {Promise}
*/ */
Sequelize.prototype.createSchema = function(schema) { Sequelize.prototype.createSchema = function(schema) {
var chainer = new Utils.QueryChainer() return this.getQueryInterface().createSchema(schema)
chainer.add(this.getQueryInterface().createSchema(schema))
return chainer.run()
} }
/** /**
* Show all defined schemas * Show all defined schemas
* @return {EventEmitter} * @return {Promise}
*/ */
Sequelize.prototype.showAllSchemas = function() { Sequelize.prototype.showAllSchemas = function() {
var chainer = new Utils.QueryChainer() return this.getQueryInterface().showAllSchemas()
chainer.add(this.getQueryInterface().showAllSchemas())
return chainer.run()
} }
/** /**
* Drop a single schema * Drop a single schema
* @param {String} schema Name of the schema * @param {String} schema Name of the schema
* @return {EventEmitter} * @return {Promise}
*/ */
Sequelize.prototype.dropSchema = function(schema) { Sequelize.prototype.dropSchema = function(schema) {
var chainer = new Utils.QueryChainer() return this.getQueryInterface().dropSchema(schema)
chainer.add(this.getQueryInterface().dropSchema(schema))
return chainer.run()
} }
/** /**
* Drop all schemas * Drop all schemas
* @return {EventEmitter} * @return {Promise}
*/ */
Sequelize.prototype.dropAllSchemas = function() { Sequelize.prototype.dropAllSchemas = function() {
return this.getQueryInterface().dropAllSchemas() return this.getQueryInterface().dropAllSchemas()
...@@ -542,61 +530,58 @@ module.exports = (function() { ...@@ -542,61 +530,58 @@ module.exports = (function() {
* @param {Boolean} [options.force=false] If force is true, each DAO will do DROP TABLE IF EXISTS ..., before it tries to create its own table * @param {Boolean} [options.force=false] If force is true, each DAO will do DROP TABLE IF EXISTS ..., before it tries to create its own table
* @param {Boolean|function} [options.logging=console.log] A function that logs sql queries, or false for no logging * @param {Boolean|function} [options.logging=console.log] A function that logs sql queries, or false for no logging
* @param {String} [options.schema='public'] The schema that the tables should be created in. This can be overriden for each table in sequelize.define * @param {String} [options.schema='public'] The schema that the tables should be created in. This can be overriden for each table in sequelize.define
* @return {EventEmitter} * @return {Promise}
*/ */
Sequelize.prototype.sync = function(options) { Sequelize.prototype.sync = function(options) {
options = options || {} var self = this
if (this.options.sync) {
options = Utils._.extend({}, this.options.sync, options)
}
options = Utils._.defaults(options || {}, this.options.sync, this.options)
options.logging = options.logging === undefined ? false : options.logging options.logging = options.logging === undefined ? false : options.logging
var chainer = new Utils.QueryChainer() var when
// Topologically sort by foreign key constraints to give us an appropriate
// creation order
if (options.force) { if (options.force) {
chainer.add(this, 'drop', [options]) when = this.drop(options)
} else {
when = Promise.resolve()
} }
this.modelManager.forEachDAO(function(dao) { return when.then(function () {
if (dao) { var daos = [null]
chainer.add(dao, 'sync', [options])
} else {
// DB should throw an SQL error if referencing inexistant table
}
})
return chainer.runSerially() // Topologically sort by foreign key constraints to give us an appropriate
// creation order
self.modelManager.forEachDAO(function(dao) {
if (dao) {
daos.push(dao)
} else {
// DB should throw an SQL error if referencing inexistant table
}
})
return Promise.reduce(daos, function (total, dao) {
return dao.sync(options)
})
})
} }
/** /**
* Drop all tables defined through this sequelize instance. This is done by calling Model.drop on each model * Drop all tables defined through this sequelize instance. This is done by calling Model.drop on each model
* @see {DAO#drop} for options * @see {Model#drop} for options
* *
* @param {object} options The options passed to each call to Model.drop * @param {object} options The options passed to each call to Model.drop
* @return {EventEmitter} * @return {Promise}
*/ */
Sequelize.prototype.drop = function(options) { Sequelize.prototype.drop = function(options) {
var self = this var daos = [null]
return new Utils.CustomEventEmitter(function(emitter) {
var chainer = new Utils.QueryChainer()
self.modelManager.forEachDAO(function(dao) { this.modelManager.forEachDAO(function(dao) {
if (dao) { if (dao) {
chainer.add(dao, 'drop', [options]) daos.push(dao)
} }
}, { reverse: false}) }, { reverse: false})
chainer return Promise.reduce(daos, function (total, dao) {
.runSerially() return dao.drop(options)
.success(function() { emitter.emit('success', null) }) })
.error(function(err) { emitter.emit('error', err) })
}).run()
} }
/** /**
...@@ -605,22 +590,10 @@ module.exports = (function() { ...@@ -605,22 +590,10 @@ module.exports = (function() {
* @fires success If authentication was successfull * @fires success If authentication was successfull
* @error 'Invalid credentials' if the authentication failed (even if the database did not respond at all...) * @error 'Invalid credentials' if the authentication failed (even if the database did not respond at all...)
* @alias validate * @alias validate
* @return {EventEmitter} * @return {Promise}
*/ */
Sequelize.prototype.authenticate = function() { Sequelize.prototype.authenticate = function() {
var self = this return this.query('SELECT 1+1 AS result', null, { raw: true, plain: true }).return()
return new Utils.CustomEventEmitter(function(emitter) {
self
.query('SELECT 1+1 AS result', null, { raw: true, plain: true })
.complete(function(err, result) {
if (!!err) {
emitter.emit('error', new Error(err))
} else {
emitter.emit('success')
}
})
}).run()
} }
Sequelize.prototype.validate = Sequelize.prototype.authenticate; Sequelize.prototype.validate = Sequelize.prototype.authenticate;
......
...@@ -1443,12 +1443,11 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -1443,12 +1443,11 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
it("should be able to list schemas", function(done){ it("should be able to list schemas", function(done){
this.sequelize.showAllSchemas().success(function(schemas) { this.sequelize.showAllSchemas().then(function(schemas) {
expect(schemas).to.exist expect(schemas).to.be.instanceof(Array)
expect(schemas[0]).to.be.instanceof(Array)
// sqlite & MySQL doesn't actually create schemas unless Model.sync() is called // sqlite & MySQL doesn't actually create schemas unless Model.sync() is called
// Postgres supports schemas natively // Postgres supports schemas natively
expect(schemas[0]).to.have.length((dialect === "postgres" ? 2 : 1)) expect(schemas).to.have.length((dialect === "postgres" ? 2 : 1))
done() done()
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!