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

Commit b61b3e83 by Mick Hansen

fix: make paranoid destroy work with field, fixes #2879

1 parent dad54ad6
...@@ -483,9 +483,9 @@ module.exports = (function() { ...@@ -483,9 +483,9 @@ module.exports = (function() {
} }
var self = this var self = this
, values = {}
, updatedAtAttr = this.Model._timestampAttributes.updatedAt , updatedAtAttr = this.Model._timestampAttributes.updatedAt
, createdAtAttr = this.Model._timestampAttributes.createdAt; , createdAtAttr = this.Model._timestampAttributes.createdAt
, hook = self.isNewRecord ? 'Create' : 'Update';
if (updatedAtAttr && options.fields.indexOf(updatedAtAttr) === -1) { if (updatedAtAttr && options.fields.indexOf(updatedAtAttr) === -1) {
options.fields.push(updatedAtAttr); options.fields.push(updatedAtAttr);
...@@ -512,54 +512,6 @@ module.exports = (function() { ...@@ -512,54 +512,6 @@ module.exports = (function() {
}); });
} }
}).then(function() { }).then(function() {
options.fields.forEach(function(field) {
if (self.dataValues[field] !== undefined) {
values[field] = self.dataValues[field];
}
});
if (updatedAtAttr && !options.silent) {
values[updatedAtAttr] = self.Model.__getTimestamp(updatedAtAttr);
}
if (self.isNewRecord && createdAtAttr && !values[createdAtAttr]) {
values[createdAtAttr] = self.Model.__getTimestamp(createdAtAttr);
}
var query = null
, args = []
, hook = '';
if (self.isNewRecord) {
query = 'insert';
args = [self, self.Model.getTableName(options), values, options];
hook = 'Create';
} else {
var identifier = self.primaryKeyValues;
if (identifier) {
for (var attrName in identifier) {
// Field name mapping
var rawAttribute = self.Model.rawAttributes[attrName];
if (rawAttribute.field && rawAttribute.field !== rawAttribute.fieldName) {
identifier[self.Model.rawAttributes[attrName].field] = identifier[attrName];
delete identifier[attrName];
}
}
}
if (identifier === null && self.__options.whereCollection !== null) {
identifier = self.__options.whereCollection;
}
query = 'update';
args = [self, self.Model.getTableName(options), values, identifier, options];
hook = 'Update';
}
// Add the values to the Instance
self.dataValues = _.extend(self.dataValues, values);
return Promise.bind(this).then(function() { return Promise.bind(this).then(function() {
// Run before hook // Run before hook
if (options.hooks) { if (options.hooks) {
...@@ -584,24 +536,7 @@ module.exports = (function() { ...@@ -584,24 +536,7 @@ module.exports = (function() {
}); });
options.fields = _.unique(options.fields.concat(hookChanged)); options.fields = _.unique(options.fields.concat(hookChanged));
} }
// dataValues might have changed inside the hook, rebuild the values hash
values = {};
options.fields.forEach(function(attr) {
if (this.dataValues[attr] !== undefined && !this.Model._isVirtualAttribute(attr)) {
values[attr] = this.dataValues[attr];
}
// Field name mapping
if (this.Model.rawAttributes[attr] && this.Model.rawAttributes[attr].field && this.Model.rawAttributes[attr].field !== attr) {
values[this.Model.rawAttributes[attr].field] = values[attr];
delete values[attr];
}
}, this);
args[2] = values;
if (hookChanged) { if (hookChanged) {
return Promise.bind(this).then(function() { return Promise.bind(this).then(function() {
...@@ -619,6 +554,67 @@ module.exports = (function() { ...@@ -619,6 +554,67 @@ module.exports = (function() {
}).then(function() { }).then(function() {
if (!options.fields.length) return this; if (!options.fields.length) return this;
var values = {}
, query = null
, args = [];
options.fields.forEach(function(attr) {
if (this.dataValues[attr] !== undefined && !this.Model._isVirtualAttribute(attr)) {
values[attr] = this.dataValues[attr];
}
// Field name mapping
if (this.Model.rawAttributes[attr] && this.Model.rawAttributes[attr].field && this.Model.rawAttributes[attr].field !== attr) {
values[this.Model.rawAttributes[attr].field] = values[attr];
delete values[attr];
}
}, this);
if (updatedAtAttr && !options.silent) {
self.dataValues[updatedAtAttr] = values[self.Model.rawAttributes[updatedAtAttr].field || updatedAtAttr] = self.Model.__getTimestamp(updatedAtAttr);
}
if (self.isNewRecord && createdAtAttr && !values[createdAtAttr]) {
self.dataValues[createdAtAttr] = values[self.Model.rawAttributes[createdAtAttr].field || createdAtAttr] = self.Model.__getTimestamp(createdAtAttr);
}
options.fields.forEach(function(attr) {
if (this.dataValues[attr] !== undefined && !this.Model._isVirtualAttribute(attr)) {
values[attr] = this.dataValues[attr];
}
// Field name mapping
if (this.Model.rawAttributes[attr] && this.Model.rawAttributes[attr].field && this.Model.rawAttributes[attr].field !== attr) {
values[this.Model.rawAttributes[attr].field] = values[attr];
delete values[attr];
}
}, this);
if (self.isNewRecord) {
query = 'insert';
args = [self, self.Model.getTableName(options), values, options];
} else {
var identifier = self.primaryKeyValues;
if (identifier) {
for (var attrName in identifier) {
// Field name mapping
var rawAttribute = self.Model.rawAttributes[attrName];
if (rawAttribute.field && rawAttribute.field !== rawAttribute.fieldName) {
identifier[self.Model.rawAttributes[attrName].field] = identifier[attrName];
delete identifier[attrName];
}
}
}
if (identifier === null && self.__options.whereCollection !== null) {
identifier = self.__options.whereCollection;
}
query = 'update';
args = [self, self.Model.getTableName(options), values, identifier, options];
}
return self.QueryInterface[query].apply(self.QueryInterface, args) return self.QueryInterface[query].apply(self.QueryInterface, args)
.then(function(result) { .then(function(result) {
// Transfer database generated values (defaults, autoincrement, etc) // Transfer database generated values (defaults, autoincrement, etc)
......
...@@ -1805,7 +1805,7 @@ module.exports = (function() { ...@@ -1805,7 +1805,7 @@ module.exports = (function() {
var deletedAtCol = this._timestampAttributes.deletedAt var deletedAtCol = this._timestampAttributes.deletedAt
, deletedAtObject = {}; , deletedAtObject = {};
deletedAtObject[ deletedAtCol ] = null; deletedAtObject[this.rawAttributes[deletedAtCol].field || deletedAtCol] = null;
// detect emptiness // detect emptiness
if( if(
......
...@@ -499,6 +499,28 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -499,6 +499,28 @@ describe(Support.getTestDialectTeaser('Model'), function() {
expect(userB.get('id')).to.equal(userId); expect(userB.get('id')).to.equal(userId);
}); });
}); });
it('should work with paranoid destroy', function () {
var User = this.sequelize.define('User', {
deletedAt: {
type: DataTypes.DATE,
field: 'deleted_at'
}
}, {
timestamps: true,
paranoid: true
});
return User.sync({force: true}).then(function () {
return User.create().then(function (user) {
return user.destroy();
}).then(function () {
return User.findAll().then(function (users) {
expect(users.length).to.equal(0);
});
});
});
});
}); });
}); });
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!