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

Commit de60eb50 by Sushant Committed by Jan Aagaard Meier

fix: use paranoid on groupedLimit.on (#6835)

* fix: use paranoid with groupedLimit.on association

* logging and unstable test

* eslint changes

* Update belongs-to-many.js
1 parent f45bcd37
# Future
- [FIXED] Set `timestamps` and `paranoid` options from through model on `belongsToMany` association
- [FIXED] Properly apply paranoid condition when `groupedLimit.on` association is `paranoid`
- [FIXED] Throw MSSQL MERGE Statement foreignKey violations as ForeignKeyConstraintError [#7011](https://github.com/sequelize/sequelize/pull/7011)
- [FIXED] Transaction Name too long, transaction savepoints for SQL Server [#6972](https://github.com/sequelize/sequelize/pull/6972)
- [FIXED] Issue with sync hooks (before/afterInit, before/afterDefine) [#6680](https://github.com/sequelize/sequelize/issues/6680)
......
......@@ -164,6 +164,10 @@ class BelongsToMany extends Association {
}
}
this.options = Object.assign(this.options, _.pick(this.through.model.options, [
'timestamps', 'createdAt', 'updatedAt', 'deletedAt', 'paranoid'
]));
if (this.paired) {
if (this.otherKeyDefault) {
this.otherKey = this.paired.foreignKey;
......
......@@ -59,6 +59,14 @@ class Model {
}
}
// apply paranoid when groupedLimit is used
if (_.get(options, 'groupedLimit.on.options.paranoid')) {
const throughModel = _.get(options, 'groupedLimit.on.through.model');
if (throughModel) {
options.groupedLimit.through = this._paranoidClause(throughModel, options.groupedLimit.through);
}
}
if (!model.options.timestamps || !model.options.paranoid || options.paranoid === false) {
// This model is not paranoid, nothing to do here;
return options;
......
......@@ -395,7 +395,7 @@ describe(Support.getTestDialectTeaser('Model'), function() {
engine: 'MyISAM'
});
return this.sequelize.sync().bind(this).then(function() {
return this.sequelize.sync({ force: true }).bind(this).then(function() {
return this.sequelize.sync(); // The second call should not try to create the indices again
}).then(function() {
return this.sequelize.queryInterface.showIndex(Model.tableName);
......
......@@ -2,6 +2,7 @@
/* jshint -W030 */
var chai = require('chai')
, sinon = require('sinon')
, expect = chai.expect
, Support = require(__dirname + '/../../support')
, Sequelize = Support.Sequelize
......@@ -14,6 +15,18 @@ if (current.dialect.supports['UNION ALL']) {
describe(Support.getTestDialectTeaser('Model'), function() {
describe('findAll', function () {
describe('groupedLimit', function () {
before(function () {
this.clock = sinon.useFakeTimers();
});
afterEach(function () {
this.clock.reset();
});
after(function () {
this.clock.restore();
});
beforeEach(function () {
this.User = this.sequelize.define('user', {
age: Sequelize.INTEGER
......@@ -22,9 +35,20 @@ if (current.dialect.supports['UNION ALL']) {
title: DataTypes.STRING
});
this.Task = this.sequelize.define('task');
this.ProjectUserParanoid = this.sequelize.define('project_user_paranoid', {}, {
timestamps: true,
paranoid: true,
createdAt: false,
updatedAt: false
});
this.User.Projects = this.User.belongsToMany(this.Project, {through: 'project_user' });
this.Project.belongsToMany(this.User, {as: 'members', through: 'project_user' });
this.User.ParanoidProjects = this.User.belongsToMany(this.Project, {through: this.ProjectUserParanoid});
this.Project.belongsToMany(this.User, {as: 'paranoidMembers', through: this.ProjectUserParanoid});
this.User.Tasks = this.User.hasMany(this.Task);
return this.sequelize.sync({force: true}).then(() => {
......@@ -40,6 +64,8 @@ if (current.dialect.supports['UNION ALL']) {
return Promise.join(
projects[0].setMembers(users.slice(0, 4)),
projects[1].setMembers(users.slice(2)),
projects[0].setParanoidMembers(users.slice(0, 4)),
projects[1].setParanoidMembers(users.slice(2)),
users[2].setTasks(tasks)
);
});
......@@ -135,6 +161,55 @@ if (current.dialect.supports['UNION ALL']) {
expect(users.map(u => u.get('id'))).to.deep.equal([1, 3, 5, 7, 4]);
});
});
it('works with paranoid junction models', function() {
return this.User.findAll({
attributes: ['id'],
groupedLimit: {
limit: 3,
on: this.User.ParanoidProjects,
values: this.projects.map(item => item.get('id'))
},
order: [
Sequelize.fn('ABS', Sequelize.col('age')),
['id', 'DESC']
],
include: [this.User.Tasks]
}).then(users => {
/*
project1 - 1, 3, 4
project2 - 3, 5, 7
*/
expect(users).to.have.length(5);
expect(users.map(u => u.get('id'))).to.deep.equal([1, 3, 5, 7, 4]);
return Sequelize.Promise.join(
this.projects[0].setParanoidMembers(users.slice(0, 2)),
this.projects[1].setParanoidMembers(users.slice(4))
);
}).then(() => {
return this.User.findAll({
attributes: ['id'],
groupedLimit: {
limit: 3,
on: this.User.ParanoidProjects,
values: this.projects.map(item => item.get('id'))
},
order: [
Sequelize.fn('ABS', Sequelize.col('age')),
['id', 'DESC']
],
include: [this.User.Tasks]
});
}).then(users => {
/*
project1 - 1, 3
project2 - 4
*/
expect(users).to.have.length(3);
expect(users.map(u => u.get('id'))).to.deep.equal([1, 3, 4]);
});
});
});
describe('on: hasMany', function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!