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

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 { ...@@ -77,9 +77,16 @@ class Model {
const deletedAtCol = model._timestampAttributes.deletedAt; const deletedAtCol = model._timestampAttributes.deletedAt;
const deletedAtAttribute = model.rawAttributes[deletedAtCol]; const deletedAtAttribute = model.rawAttributes[deletedAtCol];
const deletedAtObject = {}; const deletedAtObject = {};
const now = Utils.now(this.sequelize.options.dialect);
let deletedAtDefaultValue = deletedAtAttribute.hasOwnProperty('defaultValue') ? deletedAtAttribute.defaultValue : null; 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; deletedAtObject[deletedAtAttribute.field || deletedAtCol] = deletedAtDefaultValue;
......
...@@ -391,7 +391,7 @@ exports.sliceArgs = sliceArgs; ...@@ -391,7 +391,7 @@ exports.sliceArgs = sliceArgs;
function now(dialect) { function now(dialect) {
const now = new Date(); const now = new Date();
if (['mysql', 'postgres', 'sqlite'].indexOf(dialect) === -1) { if (['mysql', 'postgres', 'sqlite', 'mssql'].indexOf(dialect) === -1) {
now.setMilliseconds(0); now.setMilliseconds(0);
} }
return now; return now;
......
...@@ -16,6 +16,10 @@ describe(Support.getTestDialectTeaser('Instance'), () => { ...@@ -16,6 +16,10 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
this.clock = sinon.useFakeTimers(); this.clock = sinon.useFakeTimers();
}); });
afterEach(function() {
this.clock.reset();
});
after(function() { after(function() {
this.clock.restore(); this.clock.restore();
}); });
...@@ -51,6 +55,7 @@ describe(Support.getTestDialectTeaser('Instance'), () => { ...@@ -51,6 +55,7 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
defaultValue: false defaultValue: false
} }
}); });
return this.User.sync({ force: true }); return this.User.sync({ force: true });
}); });
...@@ -249,18 +254,21 @@ describe(Support.getTestDialectTeaser('Instance'), () => { ...@@ -249,18 +254,21 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
const User = this.sequelize.define('IncrementUser', { const User = this.sequelize.define('IncrementUser', {
aNumber: DataTypes.INTEGER aNumber: DataTypes.INTEGER
}, { timestamps: true }); }, { timestamps: true });
let oldDate; let oldDate;
return User.sync({ force: true }).bind(this).then(() => { return User.sync({ force: true })
return User.create({aNumber: 1}); .then(() => User.create({ aNumber: 1 }))
}).then(function(user) { .then(user => {
oldDate = user.updatedAt; oldDate = user.get('updatedAt');
this.clock.tick(1000); this.clock.tick(1000);
return user.increment('aNumber', {by: 1}); return user.increment('aNumber', { by: 1 });
}).then(() => { })
return expect(User.findById(1)).to.eventually.have.property('updatedAt').afterTime(oldDate); .then(user => user.reload())
}); .then(user => {
return expect(user).to.have.property('updatedAt').afterTime(oldDate);
});
}); });
it('with timestamps set to true and options.silent set to true', function() { it('with timestamps set to true and options.silent set to true', function() {
...@@ -1162,11 +1170,20 @@ describe(Support.getTestDialectTeaser('Instance'), () => { ...@@ -1162,11 +1170,20 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
}); });
it('updates the timestamps', function() { it('updates the timestamps', function() {
const now = new Date(), const now = new Date();
user = this.User.build({ username: 'user' }); now.setMilliseconds(0);
const user = this.User.build({ username: 'user' });
this.clock.tick(1000); this.clock.tick(1000);
return expect(user.save()).to.eventually.have.property('updatedAt').afterTime(now);
return user.save().then(savedUser => {
expect(savedUser).have.property('updatedAt').afterTime(now);
this.clock.tick(1000);
return savedUser.save();
}).then(updatedUser => {
expect(updatedUser).have.property('updatedAt').afterTime(now);
});
}); });
it('does not update timestamps when passing silent=true', function() { it('does not update timestamps when passing silent=true', function() {
...@@ -1212,15 +1229,6 @@ describe(Support.getTestDialectTeaser('Instance'), () => { ...@@ -1212,15 +1229,6 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
}); });
describe('when nothing changed', () => { describe('when nothing changed', () => {
beforeEach(function() {
this.clock = sinon.useFakeTimers();
});
afterEach(function() {
this.clock.restore();
});
it('does not update timestamps', function() { it('does not update timestamps', function() {
const self = this; const self = this;
return self.User.create({ username: 'John' }).then(() => { return self.User.create({ username: 'John' }).then(() => {
......
...@@ -5,7 +5,6 @@ const chai = require('chai'), ...@@ -5,7 +5,6 @@ const chai = require('chai'),
Sequelize = require('../../../index'), Sequelize = require('../../../index'),
expect = chai.expect, expect = chai.expect,
Support = require(__dirname + '/../support'), Support = require(__dirname + '/../support'),
dialect = Support.getTestDialect(),
DataTypes = require(__dirname + '/../../../lib/data-types'), DataTypes = require(__dirname + '/../../../lib/data-types'),
config = require(__dirname + '/../../config/config'), config = require(__dirname + '/../../config/config'),
current = Support.sequelize; current = Support.sequelize;
...@@ -140,36 +139,34 @@ describe(Support.getTestDialectTeaser('Instance'), () => { ...@@ -140,36 +139,34 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
}); });
}); });
if (dialect === 'mysql') { it('should update timestamps with milliseconds', function() {
it('should update timestamps w milliseconds', function() { const User = this.sequelize.define('User' + config.rand(), {
const User = this.sequelize.define('User' + config.rand(), { name: DataTypes.STRING,
name: DataTypes.STRING, bio: DataTypes.TEXT,
bio: DataTypes.TEXT, email: DataTypes.STRING,
email: DataTypes.STRING, createdAt: {type: DataTypes.DATE(6), allowNull: false},
createdAt: {type: DataTypes.DATE(6), allowNull: false}, updatedAt: {type: DataTypes.DATE(6), allowNull: false}
updatedAt: {type: DataTypes.DATE(6), allowNull: false} }, {
}, { timestamps: true
timestamps: true });
});
this.clock.tick(2100); //move the clock forward 2100 ms. this.clock.tick(2100); //move the clock forward 2100 ms.
return User.sync({force: true}).then(() => { return User.sync({force: true}).then(() => {
return User.create({ return User.create({
name: 'snafu', name: 'snafu',
email: 'email' email: 'email'
}).then(user => { }).then(user => {
return user.reload(); return user.reload();
}).then(user => { }).then(user => {
expect(user.get('name')).to.equal('snafu'); expect(user.get('name')).to.equal('snafu');
expect(user.get('email')).to.equal('email'); expect(user.get('email')).to.equal('email');
const testDate = new Date(); const testDate = new Date();
testDate.setTime(2100); testDate.setTime(2100);
expect(user.get('createdAt')).to.equalTime(testDate); expect(user.get('createdAt')).to.equalTime(testDate);
});
}); });
}); });
} });
it('should only save passed attributes', function() { it('should only save passed attributes', function() {
const user = this.User.build(); 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!