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

Commit 9558242f by Sushant Committed by GitHub

fix(paranoid): use Utils.now to query for paranoid records (#8485)

1 parent bf5fa6d7
......@@ -77,9 +77,16 @@ class Model {
const deletedAtCol = model._timestampAttributes.deletedAt;
const deletedAtAttribute = model.rawAttributes[deletedAtCol];
const deletedAtObject = {};
const now = Utils.now(this.sequelize.options.dialect);
let deletedAtDefaultValue = deletedAtAttribute.hasOwnProperty('defaultValue') ? deletedAtAttribute.defaultValue : null;
deletedAtDefaultValue = deletedAtDefaultValue || { [Op.or]: { [Op.gt]: model.sequelize.literal('CURRENT_TIMESTAMP'), [Op.eq]: null } };
deletedAtDefaultValue = deletedAtDefaultValue || {
[Op.or]: {
[Op.gt]: now,
[Op.eq]: null
}
};
deletedAtObject[deletedAtAttribute.field || deletedAtCol] = deletedAtDefaultValue;
......
......@@ -391,7 +391,7 @@ exports.sliceArgs = sliceArgs;
function now(dialect) {
const now = new Date();
if (['mysql', 'postgres', 'sqlite'].indexOf(dialect) === -1) {
if (['mysql', 'postgres', 'sqlite', 'mssql'].indexOf(dialect) === -1) {
now.setMilliseconds(0);
}
return now;
......
......@@ -16,6 +16,10 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
this.clock = sinon.useFakeTimers();
});
afterEach(function() {
this.clock.reset();
});
after(function() {
this.clock.restore();
});
......@@ -51,6 +55,7 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
defaultValue: false
}
});
return this.User.sync({ force: true });
});
......@@ -249,17 +254,20 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
const User = this.sequelize.define('IncrementUser', {
aNumber: DataTypes.INTEGER
}, { timestamps: true });
let oldDate;
return User.sync({ force: true }).bind(this).then(() => {
return User.create({aNumber: 1});
}).then(function(user) {
oldDate = user.updatedAt;
return User.sync({ force: true })
.then(() => User.create({ aNumber: 1 }))
.then(user => {
oldDate = user.get('updatedAt');
this.clock.tick(1000);
return user.increment('aNumber', {by: 1});
}).then(() => {
return expect(User.findById(1)).to.eventually.have.property('updatedAt').afterTime(oldDate);
return user.increment('aNumber', { by: 1 });
})
.then(user => user.reload())
.then(user => {
return expect(user).to.have.property('updatedAt').afterTime(oldDate);
});
});
......@@ -1162,11 +1170,20 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
});
it('updates the timestamps', function() {
const now = new Date(),
user = this.User.build({ username: 'user' });
const now = new Date();
now.setMilliseconds(0);
const user = this.User.build({ username: 'user' });
this.clock.tick(1000);
return user.save().then(savedUser => {
expect(savedUser).have.property('updatedAt').afterTime(now);
this.clock.tick(1000);
return expect(user.save()).to.eventually.have.property('updatedAt').afterTime(now);
return savedUser.save();
}).then(updatedUser => {
expect(updatedUser).have.property('updatedAt').afterTime(now);
});
});
it('does not update timestamps when passing silent=true', function() {
......@@ -1212,15 +1229,6 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
});
describe('when nothing changed', () => {
beforeEach(function() {
this.clock = sinon.useFakeTimers();
});
afterEach(function() {
this.clock.restore();
});
it('does not update timestamps', function() {
const self = this;
return self.User.create({ username: 'John' }).then(() => {
......
......@@ -5,7 +5,6 @@ const chai = require('chai'),
Sequelize = require('../../../index'),
expect = chai.expect,
Support = require(__dirname + '/../support'),
dialect = Support.getTestDialect(),
DataTypes = require(__dirname + '/../../../lib/data-types'),
config = require(__dirname + '/../../config/config'),
current = Support.sequelize;
......@@ -140,8 +139,7 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
});
});
if (dialect === 'mysql') {
it('should update timestamps w milliseconds', function() {
it('should update timestamps with milliseconds', function() {
const User = this.sequelize.define('User' + config.rand(), {
name: DataTypes.STRING,
bio: DataTypes.TEXT,
......@@ -169,7 +167,6 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
});
});
});
}
it('should only save passed attributes', function() {
const user = this.User.build();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!