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

Commit ff1c97cd by Sushant Committed by GitHub

fix(update): skips update when nothing to update (#10248)

1 parent 510ff542
...@@ -2975,11 +2975,19 @@ class Model { ...@@ -2975,11 +2975,19 @@ class Model {
}); });
} }
}).then(results => { }).then(results => {
if (results) {
// Update already done row-by-row - exit // Update already done row-by-row - exit
if (results) {
return [results.length, results]; return [results.length, results];
} }
// only updatedAt is being passed, then skip update
if (
_.isEmpty(valuesUse)
|| Object.keys(valuesUse).length === 1 && valuesUse[this._timestampAttributes.updatedAt]
) {
return [0];
}
valuesUse = Utils.mapValueFieldNames(valuesUse, options.fields, this); valuesUse = Utils.mapValueFieldNames(valuesUse, options.fields, this);
options = Utils.mapOptionFieldNames(options, this); options = Utils.mapOptionFieldNames(options, this);
options.hasTrigger = this.options ? this.options.hasTrigger : false; options.hasTrigger = this.options ? this.options.hasTrigger : false;
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
const Support = require('../support'); const Support = require('../support');
const DataTypes = require('../../../lib/data-types'); const DataTypes = require('../../../lib/data-types');
const chai = require('chai'); const chai = require('chai');
const sinon = require('sinon');
const expect = chai.expect; const expect = chai.expect;
const current = Support.sequelize; const current = Support.sequelize;
const _ = require('lodash'); const _ = require('lodash');
...@@ -35,6 +36,59 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -35,6 +36,59 @@ describe(Support.getTestDialectTeaser('Model'), () => {
})); }));
}); });
describe('skips update query', () => {
it('if no data to update', function() {
const spy = sinon.spy();
return this.Account.create({ ownerId: 3 }).then(() => {
return this.Account.update({
unknownField: 'haha'
}, {
where: {
ownerId: 3
},
logging: spy
});
}).then(result => {
expect(result[0]).to.equal(0);
expect(spy.called, 'Update query was issued when no data to update').to.be.false;
});
});
it('skips when timestamps disabled', function() {
const Model = this.sequelize.define('Model', {
ownerId: {
type: DataTypes.INTEGER,
allowNull: false,
field: 'owner_id'
},
name: {
type: DataTypes.STRING
}
}, {
timestamps: false
});
const spy = sinon.spy();
return Model.sync({ force: true })
.then(() => Model.create({ ownerId: 3 }))
.then(() => {
return Model.update({
unknownField: 'haha'
}, {
where: {
ownerId: 3
},
logging: spy
});
})
.then(result => {
expect(result[0]).to.equal(0);
expect(spy.called, 'Update query was issued when no data to update').to.be.false;
});
});
});
it('changed should be false after reload', function() { it('changed should be false after reload', function() {
return this.Account.create({ ownerId: 2, name: 'foo' }) return this.Account.create({ ownerId: 2, name: 'foo' })
.then(account => { .then(account => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!