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

Commit 538a6192 by Kenji Tayama

use defaultValue of deletedAt instead of NULL in paranoidClause and restore

1 parent 2bfa312f
......@@ -863,7 +863,11 @@ Instance.prototype.restore = function(options) {
return this.Model.runHooks('beforeRestore', this, options);
}
}).then(function() {
this.setDataValue(this.Model._timestampAttributes.deletedAt, null);
var deletedAtCol = this.Model._timestampAttributes.deletedAt
, deletedAtAttribute = this.Model.rawAttributes[deletedAtCol]
, deletedAtDefaultValue = deletedAtAttribute.hasOwnProperty('defaultValue') ? deletedAtAttribute.defaultValue : null;
this.setDataValue(deletedAtCol, deletedAtDefaultValue);
return this.save(_.extend({}, options, {hooks : false, omitNull : false}));
}).tap(function() {
// Run after hook
......
......@@ -136,9 +136,11 @@ var paranoidClause = function(model, options) {
}
var deletedAtCol = model._timestampAttributes.deletedAt
, deletedAtObject = {};
, deletedAtAttribute = model.rawAttributes[deletedAtCol]
, deletedAtObject = {}
, deletedAtDefaultValue = deletedAtAttribute.hasOwnProperty('defaultValue') ? deletedAtAttribute.defaultValue : null;
deletedAtObject[model.rawAttributes[deletedAtCol].field || deletedAtCol] = null;
deletedAtObject[deletedAtAttribute.field || deletedAtCol] = deletedAtDefaultValue;
if (Utils._.isEmpty(options.where)) {
options.where = deletedAtObject;
......@@ -2286,8 +2288,12 @@ Model.prototype.restore = function(options) {
}
}).then(function() {
// Run undelete query
var attrValueHash = {};
attrValueHash[self._timestampAttributes.deletedAt] = null;
var attrValueHash = {}
, deletedAtCol = self._timestampAttributes.deletedAt
, deletedAtAttribute = self.rawAttributes[deletedAtCol]
, deletedAtDefaultValue = deletedAtAttribute.hasOwnProperty('defaultValue') ? deletedAtAttribute.defaultValue : null;
attrValueHash[deletedAtCol] = deletedAtDefaultValue;
options.omitNull = false;
return self.QueryInterface.bulkUpdate(self.getTableName(options), attrValueHash, options.where, options, self._timestampAttributes.deletedAt);
}).tap(function() {
......
......@@ -1215,4 +1215,55 @@ describe(Support.getTestDialectTeaser('Sequelize'), function() {
});
});
});
describe('paranoid deletedAt non-null default value', function() {
it('should use defaultValue of deletedAt in paranoid clause and restore', function() {
var epochObj = new Date(0)
, epoch = Number(epochObj);
var User = this.sequelize.define('user', {
username: DataTypes.STRING,
deletedAt: {
type: DataTypes.DATE,
defaultValue: epochObj
}
}, {
paranoid: true,
});
return this.sequelize.sync({force: true}).bind(this).then(function () {
return User.create({username: 'user1'}).then(function(user) {
expect(Number(user.deletedAt)).to.equal(epoch);
return User.findOne({
where: {
username: 'user1'
}
}).then(function (user) {
expect(user).to.exist;
expect(Number(user.deletedAt)).to.equal(epoch);
return user.destroy();
}).then(function(destroyedUser) {
expect(Number(destroyedUser.deletedAt)).not.to.equal(epoch);
return destroyedUser.restore();
}).then(function(restoredUser) {
expect(Number(restoredUser.deletedAt)).to.equal(epoch);
return User.destroy({where: {
username: 'user1'
}});
}).then(function() {
return User.count();
}).then(function(count) {
expect(count).to.equal(0);
return User.restore();
}).then(function() {
return User.findAll();
}).then(function(nonDeletedUsers) {
expect(nonDeletedUsers.length).to.equal(1);
nonDeletedUsers.forEach(function(u) {
expect(Number(u.deletedAt)).to.equal(epoch);
});
});
});
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!