From 288012264ecc7d163e1677c7f33cf11892b24f4c Mon Sep 17 00:00:00 2001 From: Daniel Durante Date: Tue, 29 Oct 2013 21:32:47 -0400 Subject: [PATCH] When a table's timestamps are set to true, increment() and decrement() functions should update the updatedAt column. --- lib/dao.js | 15 +++++++-- lib/dialects/mysql/query-generator.js | 8 ++++- lib/dialects/postgres/query-generator.js | 8 ++++- lib/dialects/sqlite/query-generator.js | 8 ++++- lib/query-interface.js | 4 +-- test/dao.test.js | 40 ++++++++++++++++++++++++ 6 files changed, 75 insertions(+), 8 deletions(-) diff --git a/lib/dao.js b/lib/dao.js index d5037caa6e20..6745b1be707b 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -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) { diff --git a/lib/dialects/mysql/query-generator.js b/lib/dialects/mysql/query-generator.js index 5c78294272fe..b095e23437b2 100644 --- a/lib/dialects/mysql/query-generator.js +++ b/lib/dialects/mysql/query-generator.js @@ -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) diff --git a/lib/dialects/postgres/query-generator.js b/lib/dialects/postgres/query-generator.js index ae282f05d5e5..1c6b1a5ee9b3 100644 --- a/lib/dialects/postgres/query-generator.js +++ b/lib/dialects/postgres/query-generator.js @@ -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(","), diff --git a/lib/dialects/sqlite/query-generator.js b/lib/dialects/sqlite/query-generator.js index 40180d41c0ed..7b0084d22ec1 100644 --- a/lib/dialects/sqlite/query-generator.js +++ b/lib/dialects/sqlite/query-generator.js @@ -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(","), diff --git a/lib/query-interface.js b/lib/query-interface.js index 2caf3c177ecf..719ce88071f8 100644 --- a/lib/query-interface.js +++ b/lib/query-interface.js @@ -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'); } diff --git a/test/dao.test.js b/test/dao.test.js index 0fc1d04a4c45..928bdcaccc0b 100644 --- a/test/dao.test.js +++ b/test/dao.test.js @@ -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 () {