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

Commit 28801226 by Daniel Durante

When a table's timestamps are set to true, increment() and decrement() functions…

… should update the updatedAt column.
1 parent 3a94a396
...@@ -355,8 +355,10 @@ module.exports = (function() { ...@@ -355,8 +355,10 @@ module.exports = (function() {
} }
DAO.prototype.increment = function(fields, count) { DAO.prototype.increment = function(fields, count) {
var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id }, var identifier = this.__options.hasPrimaryKeys ? this.primaryKeyValues : { id: this.id }
values = {} , updatedAtAttr = Utils._.underscoredIf(this.__options.updatedAt, this.__options.underscored)
, values = {}
, options = {}
if (count === undefined) { if (count === undefined) {
count = 1; count = 1;
...@@ -372,7 +374,14 @@ module.exports = (function() { ...@@ -372,7 +374,14 @@ module.exports = (function() {
values = fields; 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) { DAO.prototype.decrement = function (fields, count) {
......
...@@ -244,7 +244,7 @@ module.exports = (function() { ...@@ -244,7 +244,7 @@ module.exports = (function() {
return query return query
}, },
incrementQuery: function (tableName, attrValueHash, where) { incrementQuery: function (tableName, attrValueHash, where, options) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull) attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)
var values = [] var values = []
...@@ -256,6 +256,12 @@ module.exports = (function() { ...@@ -256,6 +256,12 @@ module.exports = (function() {
values.push(this.quoteIdentifier(key) + "=" + this.quoteIdentifier(key) + " + " + _value) 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) var table = this.quoteIdentifier(tableName)
values = values.join(",") values = values.join(",")
where = this.getWhereConditions(where) where = this.getWhereConditions(where)
......
...@@ -366,7 +366,7 @@ module.exports = (function() { ...@@ -366,7 +366,7 @@ module.exports = (function() {
return Utils._.template(query)(replacements) return Utils._.template(query)(replacements)
}, },
incrementQuery: function(tableName, attrValueHash, where) { incrementQuery: function(tableName, attrValueHash, where, options) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull) attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %> RETURNING *" var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %> RETURNING *"
...@@ -377,6 +377,12 @@ module.exports = (function() { ...@@ -377,6 +377,12 @@ module.exports = (function() {
values.push(this.quoteIdentifier(key) + "=" + this.quoteIdentifier(key) + " + " + this.escape(value)) 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 = { var replacements = {
table: this.quoteIdentifiers(tableName), table: this.quoteIdentifiers(tableName),
values: values.join(","), values: values.join(","),
......
...@@ -214,7 +214,7 @@ module.exports = (function() { ...@@ -214,7 +214,7 @@ module.exports = (function() {
return Utils._.template(query)(replacements) return Utils._.template(query)(replacements)
}, },
incrementQuery: function(tableName, attrValueHash, where) { incrementQuery: function(tableName, attrValueHash, where, options) {
attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull) attrValueHash = Utils.removeNullValuesFromHash(attrValueHash, this.options.omitNull)
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>" var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
...@@ -225,6 +225,12 @@ module.exports = (function() { ...@@ -225,6 +225,12 @@ module.exports = (function() {
values.push(this.quoteIdentifier(key) + "=" + this.quoteIdentifier(key) + "+ " + this.escape(value)) 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 = { var replacements = {
table: this.quoteIdentifier(tableName), table: this.quoteIdentifier(tableName),
values: values.join(","), values: values.join(","),
......
...@@ -656,8 +656,8 @@ module.exports = (function() { ...@@ -656,8 +656,8 @@ module.exports = (function() {
return queryAndEmit.call(this, [sql, factory, queryOptions], 'select') return queryAndEmit.call(this, [sql, factory, queryOptions], 'select')
} }
QueryInterface.prototype.increment = function(dao, tableName, values, identifier) { QueryInterface.prototype.increment = function(dao, tableName, values, identifier, options) {
var sql = this.QueryGenerator.incrementQuery(tableName, values, identifier); var sql = this.QueryGenerator.incrementQuery(tableName, values, identifier, options);
return queryAndEmit.call(this, [sql, dao], 'increment'); return queryAndEmit.call(this, [sql, dao], 'increment');
} }
......
...@@ -337,6 +337,26 @@ describe(Support.getTestDialectTeaser("DAO"), function () { ...@@ -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 () { describe('decrement', function () {
...@@ -415,6 +435,26 @@ describe(Support.getTestDialectTeaser("DAO"), 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 () { describe('reload', function () {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!