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

Commit 86f258c6 by Sascha Depold

Merge branch 'updateAttributes-spec' of https://github.com/durango/sequelize int…

…o durango-updateAttributes-spec

Conflicts:
	lib/dao.js
2 parents 95a23b6d ef31e540
...@@ -108,7 +108,7 @@ module.exports = (function() { ...@@ -108,7 +108,7 @@ module.exports = (function() {
fields.push(updatedAtAttr) fields.push(updatedAtAttr)
} }
if (fields.indexOf(createdAtAttr) === -1) { if (fields.indexOf(createdAtAttr) === -1 && this.isNewRecord === true) {
fields.push(createdAtAttr) fields.push(createdAtAttr)
} }
} }
...@@ -240,26 +240,38 @@ module.exports = (function() { ...@@ -240,26 +240,38 @@ module.exports = (function() {
var self = this var self = this
var readOnlyAttributes = Object.keys(this.__factory.primaryKeys) var readOnlyAttributes = Object.keys(this.__factory.primaryKeys)
readOnlyAttributes.push('id') readOnlyAttributes.push('id')
readOnlyAttributes.push('createdAt')
readOnlyAttributes.push('updatedAt') if (this.isNewRecord !== true) {
readOnlyAttributes.push('deletedAt') readOnlyAttributes.push(this.daoFactory.options.underscored === true ? 'created_at' : 'createdAt')
}
// readOnlyAttributes.push(this.daoFactory.options.underscored === true ? 'updated_at' : 'updatedAt')
readOnlyAttributes.push(this.daoFactory.options.underscored === true ? 'deleted_at' : 'deletedAt')
var isDirty = this.isDirty var isDirty = this.isDirty
Utils._.each(updates, function(value, attr) { Utils._.each(updates, function(value, attr) {
var updateAllowed = ( var updateAllowed = (
(readOnlyAttributes.indexOf(attr) == -1) && (readOnlyAttributes.indexOf(attr) == -1) &&
(readOnlyAttributes.indexOf(Utils._.underscored(attr)) == -1) && (readOnlyAttributes.indexOf(Utils._.underscored(attr)) == -1) &&
(self.attributes.indexOf(attr) > -1) (self.attributes.indexOf(attr) > -1)
) )
if (updateAllowed) { if (updateAllowed) {
if (Utils.hasChanged(self[attr], value)) { if (Utils.hasChanged(self[attr], value)) {
isDirty = true isDirty = true
} }
self[attr] = value self[attr] = value
} }
}) })
// since we're updating the record, we should be updating the updatedAt column..
if (this.daoFactory.options.timestamps === true) {
isDirty = true
self[this.daoFactory.options.underscored === true ? 'updated_at' : 'updatedAt'] = new Date()
}
this.isDirty = isDirty this.isDirty = isDirty
} }
......
...@@ -798,6 +798,24 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -798,6 +798,24 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
}) })
describe('update', function() { describe('update', function() {
it('updates the attributes that we select only without updating createdAt', function(done) {
var User = this.sequelize.define('User1', {
username: Sequelize.STRING,
secretValue: Sequelize.STRING
}, {
paranoid:true
})
User.sync({ force: true }).success(function() {
User.create({username: 'Peter', secretValue: '42'}).success(function(user) {
user.updateAttributes({ secretValue: '43' }, ['secretValue']).on('sql', function(sql) {
expect(sql).to.match(/UPDATE\s+[`"]+User1s[`"]+\s+SET\s+[`"]+secretValue[`"]='43',[`"]+updatedAt[`"]+='[^`",]+'\s+WHERE [`"]+id[`"]+=1/)
done()
})
})
})
})
it('allows sql logging of updated statements', function(done) { it('allows sql logging of updated statements', function(done) {
var User = this.sequelize.define('User', { var User = this.sequelize.define('User', {
name: Sequelize.STRING, name: Sequelize.STRING,
......
...@@ -169,12 +169,34 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -169,12 +169,34 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
}) })
}) })
it("returns false for bulk non-changed attribute", function(done) { it("returns true for bulk non-changed attribute + model with timestamps", function(done) {
this.User.create({ username: 'user' }).success(function(user) { this.User.create({ username: 'user' }).success(function(user) {
user.setAttributes({ user.setAttributes({
username: 'user' username: 'user'
}) })
expect(user.isDirty).to.be.rue
done()
})
})
it("returns false for bulk non-changed attribute + model without timestamps", function(done) {
var User = this.sequelize.define('User' + parseInt(Math.random() * 10000000), {
username: DataTypes.STRING
}, {
timestamps: false
})
User
.sync({ force: true })
.then(function() {
return User.create({ username: "user" })
})
.then(function(user) {
return user.setAttributes({ username: "user" })
expect(user.isDirty).to.be.false expect(user.isDirty).to.be.false
})
.then(function() {
done() done()
}) })
}) })
...@@ -1112,17 +1134,21 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -1112,17 +1134,21 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
identifier: 'identifier' identifier: 'identifier'
}).success(function(user) { }).success(function(user) {
var oldCreatedAt = user.createdAt var oldCreatedAt = user.createdAt
, oldUpdatedAt = user.updatedAt
, oldIdentifier = user.identifier , oldIdentifier = user.identifier
setTimeout(function () {
user.updateAttributes({ user.updateAttributes({
name: 'foobar', name: 'foobar',
createdAt: new Date(2000, 1, 1), createdAt: new Date(2000, 1, 1),
identifier: 'another identifier' identifier: 'another identifier'
}).success(function(user) { }).success(function(user) {
expect((new Date(user.createdAt)).getTime()).to.equal((new Date(oldCreatedAt)).getTime()) expect(new Date(user.createdAt)).to.equalDate(new Date(oldCreatedAt))
expect(new Date(user.updatedAt)).to.not.equalTime(new Date(oldUpdatedAt))
expect(user.identifier).to.equal(oldIdentifier) expect(user.identifier).to.equal(oldIdentifier)
done() done()
}) })
}, 1000)
}) })
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!