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

Commit aee7fec4 by Mick Hansen

Merge pull request #5106 from Verdier/master

Do not inject include twice, expand and validate include in aggregate
2 parents 33799294 233072fe
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
- [ADDED] hasOne scope support [#5113](https://github.com/sequelize/sequelize/pull/5113) - [ADDED] hasOne scope support [#5113](https://github.com/sequelize/sequelize/pull/5113)
- [FIXED] attributes from multiple scopes does not merge [#4856](https://github.com/sequelize/sequelize/issues/4856) - [FIXED] attributes from multiple scopes does not merge [#4856](https://github.com/sequelize/sequelize/issues/4856)
- [FIXED] Support Unicode strings in mssql [#3752](https://github.com/sequelize/sequelize/issues/3752) - [FIXED] Support Unicode strings in mssql [#3752](https://github.com/sequelize/sequelize/issues/3752)
- [FIXED] Do not inject include twice in `options.include` [#5106](https://github.com/sequelize/sequelize/pull/5106)
- [FIXED] Expand and validate include in `aggregate` [#5106](https://github.com/sequelize/sequelize/pull/5106)
# 3.15.1 # 3.15.1
- [FIXED] calling Model.update() modifies passed values [#4520](https://github.com/sequelize/sequelize/issues/4520) - [FIXED] calling Model.update() modifies passed values [#4520](https://github.com/sequelize/sequelize/issues/4520)
......
...@@ -601,7 +601,7 @@ validateIncludedElement = function(include, tableNames, options) { ...@@ -601,7 +601,7 @@ validateIncludedElement = function(include, tableNames, options) {
model = include.association.target.name === include.model.name ? include.association.target : include.association.source; model = include.association.target.name === include.model.name ? include.association.target : include.association.source;
} }
Model.$injectScope(model.$scope, include); model.$injectScope(include);
// This check should happen after injecting the scope, since the scope may contain a .attributes // This check should happen after injecting the scope, since the scope may contain a .attributes
if (!include.attributes) { if (!include.attributes) {
...@@ -1328,7 +1328,7 @@ Model.prototype.findAll = function(options) { ...@@ -1328,7 +1328,7 @@ Model.prototype.findAll = function(options) {
return Promise.bind(this).then(function() { return Promise.bind(this).then(function() {
conformOptions(options, this); conformOptions(options, this);
Model.$injectScope(this.$scope, options); this.$injectScope(options);
if (options.hooks) { if (options.hooks) {
return this.runHooks('beforeFind', options); return this.runHooks('beforeFind', options);
...@@ -1511,7 +1511,12 @@ Model.prototype.find = Model.prototype.findOne; ...@@ -1511,7 +1511,12 @@ Model.prototype.find = Model.prototype.findOne;
Model.prototype.aggregate = function(attribute, aggregateFunction, options) { Model.prototype.aggregate = function(attribute, aggregateFunction, options) {
options = Utils._.extend({ attributes: [] }, options || {}); options = Utils._.extend({ attributes: [] }, options || {});
conformOptions(options, this); conformOptions(options, this);
Model.$injectScope(this.$scope, options); this.$injectScope(options);
if (options.include) {
expandIncludeAll.call(this, options);
validateIncludedElements.call(this, options);
}
var attrOptions = this.rawAttributes[attribute] var attrOptions = this.rawAttributes[attribute]
, field = attrOptions && attrOptions.field || attribute , field = attrOptions && attrOptions.field || attribute
...@@ -1559,7 +1564,7 @@ Model.prototype.aggregate = function(attribute, aggregateFunction, options) { ...@@ -1559,7 +1564,7 @@ Model.prototype.aggregate = function(attribute, aggregateFunction, options) {
Model.prototype.count = function(options) { Model.prototype.count = function(options) {
options = Utils._.clone(options || {}); options = Utils._.clone(options || {});
conformOptions(options, this); conformOptions(options, this);
Model.$injectScope(this.$scope, options); this.$injectScope(options);
var col = '*'; var col = '*';
...@@ -2242,7 +2247,7 @@ Model.prototype.destroy = function(options) { ...@@ -2242,7 +2247,7 @@ Model.prototype.destroy = function(options) {
}, options || {}); }, options || {});
options.type = QueryTypes.BULKDELETE; options.type = QueryTypes.BULKDELETE;
Model.$injectScope(this.$scope, options); this.$injectScope(options);
Utils.mapOptionFieldNames(options, this); Utils.mapOptionFieldNames(options, this);
...@@ -2402,7 +2407,7 @@ Model.prototype.update = function(values, options) { ...@@ -2402,7 +2407,7 @@ Model.prototype.update = function(values, options) {
options.type = QueryTypes.BULKUPDATE; options.type = QueryTypes.BULKUPDATE;
Model.$injectScope(this.$scope, options); this.$injectScope(options);
// Clone values so it doesn't get modified for caller scope // Clone values so it doesn't get modified for caller scope
values = _.clone(values); values = _.clone(values);
...@@ -2606,9 +2611,10 @@ Model.prototype.$expandAttributes = function (options) { ...@@ -2606,9 +2611,10 @@ Model.prototype.$expandAttributes = function (options) {
} }
}; };
// Inject current scope into options. Includes should have been conformed (conformOptions) before calling this // Inject $scope into options. Includes should have been conformed (conformOptions) before calling this
Model.$injectScope = function (scope, options) { Model.prototype.$injectScope = function (options) {
scope = optClone(scope); var self = this;
var scope = optClone(this.$scope);
var filteredScope = _.omit(scope, 'include'); // Includes need special treatment var filteredScope = _.omit(scope, 'include'); // Includes need special treatment
...@@ -2619,15 +2625,18 @@ Model.$injectScope = function (scope, options) { ...@@ -2619,15 +2625,18 @@ Model.$injectScope = function (scope, options) {
options.include = options.include || []; options.include = options.include || [];
// Reverse so we consider the latest include first. // Reverse so we consider the latest include first.
// This is used if several scopes specify the same include - the last scope should take precendence // This is used if several scopes specify the same include - the last scope should take precedence
scope.include.reverse().forEach(function (scopeInclude) { scope.include.reverse().forEach(function (scopeInclude) {
if (scopeInclude.all || !_.any(options.include, function matchesModelAndAlias(item) { if (scopeInclude.all || !options.include.some(function matchesModelAndAlias(item) {
var sameModel = item.model && item.model.name === scopeInclude.model.name; var isSameModel = item.model && item.model.name === scopeInclude.model.name;
if (!isSameModel || !item.as) return isSameModel;
if (sameModel && item.as) { if (scopeInclude.as) {
return item.as === scopeInclude.as; return item.as === scopeInclude.as;
} else {
var association = scopeInclude.association || self.getAssociation(scopeInclude.model, scopeInclude.as);
return association ? item.as === association.as : false;
} }
return sameModel;
})) { })) {
options.include.push(scopeInclude); options.include.push(scopeInclude);
} }
......
'use strict';
/* jshint -W030 */
/* jshint -W110 */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types');
describe(Support.getTestDialectTeaser('Model'), function() {
beforeEach(function() {
this.User = this.sequelize.define('User', {
username: DataTypes.STRING
});
this.Project = this.sequelize.define('Project', {
name: DataTypes.STRING
});
this.User.hasMany(this.Project);
this.Project.belongsTo(this.User);
return this.sequelize.sync({force: true});
});
describe('count', function() {
beforeEach(function () {
var self = this;
return this.User.bulkCreate([
{username: 'boo'},
{username: 'boo2'}
]).then(function () {
return self.User.findOne();
}).then(function (user) {
return user.createProject({
name: 'project1'
});
});
});
it('should count rows', function () {
return expect(this.User.count()).to.eventually.equal(2);
});
it('should support include', function () {
return expect(this.User.count({
include: [{
model: this.Project,
where: {
name: 'project1'
}
}]
})).to.eventually.equal(1);
});
});
});
...@@ -5,12 +5,16 @@ ...@@ -5,12 +5,16 @@
var chai = require('chai') var chai = require('chai')
, Sequelize = require('../../../../index') , Sequelize = require('../../../../index')
, expect = chai.expect , expect = chai.expect
, Support = require(__dirname + '/../../support'); , Support = require(__dirname + '/../../support')
, Promise = require(__dirname + '/../../../../lib/promise');
describe(Support.getTestDialectTeaser('Model'), function() { describe(Support.getTestDialectTeaser('Model'), function() {
describe('scope', function () { describe('scope', function () {
describe('aggregate', function () { describe('aggregate', function () {
beforeEach(function () { beforeEach(function () {
this.Child = this.sequelize.define('Child', {
priority: Sequelize.INTEGER
});
this.ScopeMe = this.sequelize.define('ScopeMe', { this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING, username: Sequelize.STRING,
email: Sequelize.STRING, email: Sequelize.STRING,
...@@ -34,9 +38,19 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -34,9 +38,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}, },
withOrder: { withOrder: {
order: 'username' order: 'username'
},
withInclude: {
include: [{
model: this.Child,
where: {
priority: 1
}
}]
} }
} }
}); });
this.Child.belongsTo(this.ScopeMe);
this.ScopeMe.hasMany(this.Child);
return this.sequelize.sync({force: true}).then(function() { return this.sequelize.sync({force: true}).then(function() {
var records = [ var records = [
...@@ -46,7 +60,18 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -46,7 +60,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
{username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7} {username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7}
]; ];
return this.ScopeMe.bulkCreate(records); return this.ScopeMe.bulkCreate(records);
}.bind(this)); }.bind(this)).then(function () {
return this.ScopeMe.findAll();
}.bind(this)).then(function (records) {
return Promise.all([
records[0].createChild({
priority: 1
}),
records[1].createChild({
priority: 2
})
]);
});
}); });
it('should apply defaultScope', function () { it('should apply defaultScope', function () {
...@@ -68,6 +93,18 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -68,6 +93,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('should be able to merge scopes with where', function () { it('should be able to merge scopes with where', function () {
return expect(this.ScopeMe.scope('lowAccess').aggregate( '*', 'count', { where: { username: 'dan'}})).to.eventually.equal(1); return expect(this.ScopeMe.scope('lowAccess').aggregate( '*', 'count', { where: { username: 'dan'}})).to.eventually.equal(1);
}); });
it('should be able to use where on include', function () {
return expect(this.ScopeMe.scope('withInclude').aggregate( 'ScopeMe.id', 'count', {
plain: true,
dataType: new Sequelize.INTEGER(),
includeIgnoreAttributes: false,
limit: null,
offset: null,
order: null,
attributes: []
})).to.eventually.equal(1);
});
}); });
}); });
}); });
...@@ -5,12 +5,16 @@ ...@@ -5,12 +5,16 @@
var chai = require('chai') var chai = require('chai')
, Sequelize = require('../../../../index') , Sequelize = require('../../../../index')
, expect = chai.expect , expect = chai.expect
, Promise = require(__dirname + '/../../../../lib/promise')
, Support = require(__dirname + '/../../support'); , Support = require(__dirname + '/../../support');
describe(Support.getTestDialectTeaser('Model'), function() { describe(Support.getTestDialectTeaser('Model'), function() {
describe('scope', function () { describe('scope', function () {
describe('count', function () { describe('count', function () {
beforeEach(function () { beforeEach(function () {
this.Child = this.sequelize.define('Child', {
priority: Sequelize.INTEGER
});
this.ScopeMe = this.sequelize.define('ScopeMe', { this.ScopeMe = this.sequelize.define('ScopeMe', {
username: Sequelize.STRING, username: Sequelize.STRING,
email: Sequelize.STRING, email: Sequelize.STRING,
...@@ -23,7 +27,7 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -23,7 +27,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
gte: 5 gte: 5
} }
}, },
attributes: ['username', 'email', 'access_level'] attributes: ['id', 'username', 'email', 'access_level']
}, },
scopes: { scopes: {
lowAccess: { lowAccess: {
...@@ -35,11 +39,21 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -35,11 +39,21 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}, },
withOrder: { withOrder: {
order: 'username' order: 'username'
},
withInclude: {
include: [{
model: this.Child,
where: {
priority: 1
}
}]
} }
} }
}); });
this.Child.belongsTo(this.ScopeMe);
this.ScopeMe.hasMany(this.Child);
return this.sequelize.sync({force: true}).then(function() { return this.sequelize.sync({force: true}).then(function () {
var records = [ var records = [
{username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7}, {username: 'tony', email: 'tony@sequelizejs.com', access_level: 3, other_value: 7},
{username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11}, {username: 'tobi', email: 'tobi@fakeemail.com', access_level: 10, other_value: 11},
...@@ -47,7 +61,18 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -47,7 +61,18 @@ describe(Support.getTestDialectTeaser('Model'), function() {
{username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7} {username: 'fred', email: 'fred@foobar.com', access_level: 3, other_value: 7}
]; ];
return this.ScopeMe.bulkCreate(records); return this.ScopeMe.bulkCreate(records);
}.bind(this)); }.bind(this)).then(function () {
return this.ScopeMe.findAll();
}.bind(this)).then(function (records) {
return Promise.all([
records[0].createChild({
priority: 1
}),
records[1].createChild({
priority: 2
})
]);
});
}); });
it('should apply defaultScope', function () { it('should apply defaultScope', function () {
...@@ -73,6 +98,10 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -73,6 +98,10 @@ describe(Support.getTestDialectTeaser('Model'), function() {
it('should ignore the order option if it is found within the scope', function () { it('should ignore the order option if it is found within the scope', function () {
return expect(this.ScopeMe.scope('withOrder').count()).to.eventually.equal(4); return expect(this.ScopeMe.scope('withOrder').count()).to.eventually.equal(4);
}); });
it('should be able to use where on include', function () {
return expect(this.ScopeMe.scope('withInclude').count()).to.eventually.equal(1);
});
}); });
}); });
}); });
...@@ -253,7 +253,9 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -253,7 +253,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
limit: 9 limit: 9
}; };
current.Model.$injectScope(scope, options); current.Model.prototype.$injectScope.call({
$scope: scope
}, options);
expect(options).to.deep.equal({ expect(options).to.deep.equal({
where: { where: {
...@@ -275,7 +277,9 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -275,7 +277,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
var options = {}; var options = {};
current.Model.$injectScope(scope, options); current.Model.prototype.$injectScope.call({
$scope: scope
}, options);
expect(options.include).to.have.length(1); expect(options.include).to.have.length(1);
expect(options.include[0]).to.deep.equal({ model: Project, where: { something: true }}); expect(options.include[0]).to.deep.equal({ model: Project, where: { something: true }});
...@@ -290,7 +294,9 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -290,7 +294,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
include: [{ model: Project, where: { something: true }}] include: [{ model: Project, where: { something: true }}]
}; };
current.Model.$injectScope(scope, options); current.Model.prototype.$injectScope.call({
$scope: scope
}, options);
expect(options.include).to.have.length(1); expect(options.include).to.have.length(1);
expect(options.include[0]).to.deep.equal({ model: Project, where: { something: true }}); expect(options.include[0]).to.deep.equal({ model: Project, where: { something: true }});
...@@ -305,7 +311,9 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -305,7 +311,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
include: [{model: User, as: 'otherUser'}] include: [{model: User, as: 'otherUser'}]
}; };
current.Model.$injectScope(scope, options); current.Model.prototype.$injectScope.call({
$scope: scope
}, options);
expect(options.include).to.have.length(2); expect(options.include).to.have.length(2);
expect(options.include[0]).to.deep.equal({model: User, as: 'otherUser'}); expect(options.include[0]).to.deep.equal({model: User, as: 'otherUser'});
...@@ -325,7 +333,9 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -325,7 +333,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
] ]
}; };
current.Model.$injectScope(scope, options); current.Model.prototype.$injectScope.call({
$scope: scope
}, options);
expect(options.include).to.have.length(2); expect(options.include).to.have.length(2);
expect(options.include[0]).to.deep.equal({ model: User, where: { something: true }}); expect(options.include[0]).to.deep.equal({ model: User, where: { something: true }});
...@@ -346,7 +356,9 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -346,7 +356,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
] ]
}; };
current.Model.$injectScope(scope, options); current.Model.prototype.$injectScope.call({
$scope: scope
}, options);
expect(options.include).to.have.length(2); expect(options.include).to.have.length(2);
expect(options.include[0]).to.deep.equal({ model: User, where: { something: true }}); expect(options.include[0]).to.deep.equal({ model: User, where: { something: true }});
...@@ -367,7 +379,9 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -367,7 +379,9 @@ describe(Support.getTestDialectTeaser('Model'), function() {
] ]
}; };
current.Model.$injectScope(scope, options); current.Model.prototype.$injectScope.call({
$scope: scope
}, options);
expect(options.include).to.have.length(2); expect(options.include).to.have.length(2);
expect(options.include[0]).to.deep.equal({ all: true }); expect(options.include[0]).to.deep.equal({ all: true });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!