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

Commit ef0b12c9 by Jan Aagaard Meier

Merge pull request #1023 from durango/incr-decr-fix

When a table's timestamps are set to true, increment() and decrement() f...
2 parents eda3d59e 28801226
......@@ -355,8 +355,10 @@ module.exports = (function() {
}
DAO.prototype.increment = function(fields, count) {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id },
values = {}
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id }
, updatedAtAttr = Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored)
, values = {}
, options = {}
if (count === undefined) {
count = 1;
......@@ -372,7 +374,14 @@ module.exports = (function() {
values = fields;
}
return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.__factory.tableName, this.__factory.options.schema), values, identifier)
if (this.__options.timestamps) {
if (!values[updatedAtAttr]) {
options[updatedAtAttr] = Utils.now(this.daoFactory.daoFactoryManager.sequelize.options.dialect)
}
}
return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.__factory.tableName, this.__factory.options.schema), values, identifier, options)
}
DAO.prototype.decrement = function (fields, count) {
......
......@@ -244,7 +244,7 @@ module.exports = (function() {
return query
},
incrementQuery: function (tableName, attrValueHash, where) {
incrementQuery: function (tableName, attrValueHash, where, options) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)
var values = []
......@@ -256,6 +256,12 @@ module.exports = (function() {
values.push(this.quoteIdentifier(key) + "=" + this.quoteIdentifier(key) + " + " + _value)
}
options = options || {}
for (var key in options) {
var value = options[key];
values.push(this.quoteIdentifier(key) + "=" + this.escape(value))
}
var table = this.quoteIdentifier(tableName)
values = values.join(",")
where = this.getWhereConditions(where)
......
......@@ -366,7 +366,7 @@ module.exports = (function() {
return Utils._.template(query)(replacements)
},
incrementQuery: function(tableName, attrValueHash, where) {
incrementQuery: function(tableName, attrValueHash, where, options) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %> RETURNING *"
......@@ -377,6 +377,12 @@ module.exports = (function() {
values.push(this.quoteIdentifier(key) + "=" + this.quoteIdentifier(key) + " + " + this.escape(value))
}
options = options || {}
for (var key in options) {
var value = options[key];
values.push(this.quoteIdentifier(key) + "=" + this.escape(value))
}
var replacements = {
table: this.quoteIdentifiers(tableName),
values: values.join(","),
......
......@@ -214,7 +214,7 @@ module.exports = (function() {
return Utils._.template(query)(replacements)
},
incrementQuery: function(tableName, attrValueHash, where) {
incrementQuery: function(tableName, attrValueHash, where, options) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
......@@ -225,6 +225,12 @@ module.exports = (function() {
values.push(this.quoteIdentifier(key) + "=" + this.quoteIdentifier(key) + "+ " + this.escape(value))
}
options = options || {}
for (var key in options) {
var value = options[key];
values.push(this.quoteIdentifier(key) + "=" + this.escape(value))
}
var replacements = {
table: this.quoteIdentifier(tableName),
values: values.join(","),
......
......@@ -656,8 +656,8 @@ module.exports = (function() {
return queryAndEmit.call(this, [sql, factory, queryOptions], 'select')
}
QueryInterface.prototype.increment = function(dao, tableName, values, identifier) {
var sql = this.QueryGenerator.incrementQuery(tableName, values, identifier);
QueryInterface.prototype.increment = function(dao, tableName, values, identifier, options) {
var sql = this.QueryGenerator.incrementQuery(tableName, values, identifier, options);
return queryAndEmit.call(this, [sql, dao], 'increment');
}
......
......@@ -337,6 +337,26 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
})
})
it('with timestamps set to true', function (done) {
var User = this.sequelize.define('IncrementUser', {
aNumber: DataTypes.INTEGER
}, { timestamps: true })
User.sync({ force: true }).success(function() {
User.create({aNumber: 1}).success(function (user) {
var oldDate = user.updatedAt
setTimeout(function () {
user.increment('aNumber', 1).success(function() {
User.find(1).success(function (user) {
expect(user.updatedAt).to.be.afterTime(oldDate)
done()
})
})
}, 1000)
})
})
})
})
describe('decrement', function () {
......@@ -415,6 +435,26 @@ describe(Support.getTestDialectTeaser("DAO"), function () {
})
})
})
it('with timestamps set to true', function (done) {
var User = this.sequelize.define('IncrementUser', {
aNumber: DataTypes.INTEGER
}, { timestamps: true })
User.sync({ force: true }).success(function() {
User.create({aNumber: 1}).success(function (user) {
var oldDate = user.updatedAt
setTimeout(function () {
user.decrement('aNumber', 1).success(function() {
User.find(1).success(function (user) {
expect(user.updatedAt).to.be.afterTime(oldDate)
done()
})
})
}, 1000)
})
})
})
})
describe('reload', function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!