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

Commit 582b9aff by Jan Aagaard Meier

Proper truncate support for destroy

1 parent b64b3541
...@@ -432,16 +432,19 @@ module.exports = (function() { ...@@ -432,16 +432,19 @@ module.exports = (function() {
* Delete multiple instances * Delete multiple instances
* *
* @param {Object} where Options to describe the scope of the search. * @param {Object} where Options to describe the scope of the search.
* @param {Object} options Possible options are:
- limit: How many rows to delete
- truncate: If set to true, dialects that support it will use TRUNCATE instead of DELETE FROM. If a table is truncated the where and limit options are ignored
* @return {Object} A promise which fires `success`, `error`, `complete` and `sql`. * @return {Object} A promise which fires `success`, `error`, `complete` and `sql`.
*/ */
DAOFactory.prototype.destroy = function(where) { DAOFactory.prototype.destroy = function(where, options) {
if (this.options.timestamps && this.options.paranoid) { if (this.options.timestamps && this.options.paranoid) {
var attr = this.options.underscored ? 'deleted_at' : 'deletedAt' var attr = this.options.underscored ? 'deleted_at' : 'deletedAt'
var attrValueHash = {} var attrValueHash = {}
attrValueHash[attr] = Utils.now() attrValueHash[attr] = Utils.now()
return this.QueryInterface.bulkUpdate(this.tableName, attrValueHash, where) return this.QueryInterface.bulkUpdate(this.tableName, attrValueHash, where)
} else { } else {
return this.QueryInterface.bulkDelete(this.tableName, where) return this.QueryInterface.bulkDelete(this.tableName, where, options)
} }
} }
......
...@@ -290,8 +290,12 @@ module.exports = (function() { ...@@ -290,8 +290,12 @@ module.exports = (function() {
deleteQuery: function(tableName, where, options) { deleteQuery: function(tableName, where, options) {
options = options || {} options = options || {}
var query = options && options.truncate === true ? "TRUNCATE " : "DELETE FROM " var table = QueryGenerator.addQuotes(tableName)
, table = QueryGenerator.addQuotes(tableName) if (options.truncate === true) {
// Truncate does not allow LIMIT and WHERE
return "TRUNCATE " + table
}
where = QueryGenerator.getWhereConditions(where) where = QueryGenerator.getWhereConditions(where)
var limit = "" var limit = ""
...@@ -303,9 +307,7 @@ module.exports = (function() { ...@@ -303,9 +307,7 @@ module.exports = (function() {
limit = " LIMIT " + Utils.escape(options.limit) limit = " LIMIT " + Utils.escape(options.limit)
} }
query += table + " WHERE " + where + limit return "DELETE FROM " + table + " WHERE " + where + limit
return query
}, },
bulkDeleteQuery: function(tableName, where, options) { bulkDeleteQuery: function(tableName, where, options) {
......
...@@ -363,14 +363,17 @@ module.exports = (function() { ...@@ -363,14 +363,17 @@ module.exports = (function() {
deleteQuery: function(tableName, where, options) { deleteQuery: function(tableName, where, options) {
options = options || {} options = options || {}
if (options.truncate === true) {
return "TRUNCATE " + QueryGenerator.addQuotes(tableName)
}
if(Utils._.isUndefined(options.limit)) { if(Utils._.isUndefined(options.limit)) {
options.limit = 1; options.limit = 1;
} }
primaryKeys[tableName] = primaryKeys[tableName] || []; primaryKeys[tableName] = primaryKeys[tableName] || [];
var query = options && options.truncate === true ? "TRUNCATE <%= table %>" : "DELETE FROM <%= table %>" var query = "DELETE FROM <%= table %> WHERE <%= primaryKeys %> IN (SELECT <%= primaryKeysSelection %> FROM <%= table %> WHERE <%= where %><%= limit %>)"
query += " WHERE <%= primaryKeys %> IN (SELECT <%= primaryKeysSelection %> FROM <%= table %> WHERE <%= where %><%= limit %>)"
var pks; var pks;
if (primaryKeys[tableName] && primaryKeys[tableName].length > 0) { if (primaryKeys[tableName] && primaryKeys[tableName].length > 0) {
......
...@@ -153,7 +153,8 @@ module.exports = (function() { ...@@ -153,7 +153,8 @@ module.exports = (function() {
- truncate -> boolean - whether to use an 'optimized' mechanism (i.e. TRUNCATE) if available, - truncate -> boolean - whether to use an 'optimized' mechanism (i.e. TRUNCATE) if available,
note that this should not be the default behaviour because TRUNCATE does not note that this should not be the default behaviour because TRUNCATE does not
always play nicely (e.g. InnoDB tables with FK constraints) always play nicely (e.g. InnoDB tables with FK constraints)
(@see http://dev.mysql.com/doc/refman/5.6/en/truncate-table.html) (@see http://dev.mysql.com/doc/refman/5.6/en/truncate-table.html).
Note that truncate must ignore limit and where
*/ */
deleteQuery: function(tableName, where, options) { deleteQuery: function(tableName, where, options) {
throwMethodUndefined('deleteQuery') throwMethodUndefined('deleteQuery')
......
...@@ -280,8 +280,8 @@ module.exports = (function() { ...@@ -280,8 +280,8 @@ module.exports = (function() {
return queryAndEmit.call(this, [sql, dao], 'delete') return queryAndEmit.call(this, [sql, dao], 'delete')
} }
QueryInterface.prototype.bulkDelete = function(tableName, identifier) { QueryInterface.prototype.bulkDelete = function(tableName, identifier, options) {
var sql = this.QueryGenerator.deleteQuery(tableName, identifier, {limit: null}) var sql = this.QueryGenerator.deleteQuery(tableName, identifier, Utils._.defaults(options || {}, {limit: null}))
return queryAndEmit.call(this, sql, 'bulkDelete') return queryAndEmit.call(this, sql, 'bulkDelete')
} }
......
...@@ -301,8 +301,11 @@ describe('QueryGenerator', function() { ...@@ -301,8 +301,11 @@ describe('QueryGenerator', function() {
arguments: ['myTable', 1], arguments: ['myTable', 1],
expectation: "DELETE FROM `myTable` WHERE `id`=1 LIMIT 1" expectation: "DELETE FROM `myTable` WHERE `id`=1 LIMIT 1"
},{ },{
arguments: ['myTable', 1, {truncate: true}], arguments: ['myTable', undefined, {truncate: true}],
expectation: "TRUNCATE `myTable` WHERE `id`=1 LIMIT 1" expectation: "TRUNCATE `myTable`"
},{
arguments: ['myTable', 1, {limit: 10, truncate: true}],
expectation: "TRUNCATE `myTable`"
}, { }, {
arguments: ['myTable', 1, {limit: 10}], arguments: ['myTable', 1, {limit: 10}],
expectation: "DELETE FROM `myTable` WHERE `id`=1 LIMIT 10" expectation: "DELETE FROM `myTable` WHERE `id`=1 LIMIT 10"
......
...@@ -293,8 +293,11 @@ describe('QueryGenerator', function() { ...@@ -293,8 +293,11 @@ describe('QueryGenerator', function() {
arguments: ['myTable', 1], arguments: ['myTable', 1],
expectation: "DELETE FROM \"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"myTable\" WHERE \"id\"=1 LIMIT 1)" expectation: "DELETE FROM \"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"myTable\" WHERE \"id\"=1 LIMIT 1)"
}, { }, {
arguments: ['myTable', 1, {truncate: true}], arguments: ['myTable', undefined, {truncate: true}],
expectation: "TRUNCATE \"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"myTable\" WHERE \"id\"=1 LIMIT 1)" expectation: "TRUNCATE \"myTable\""
}, {
arguments: ['myTable', 1, {limit: 10, truncate: true}],
expectation: "TRUNCATE \"myTable\""
}, { }, {
arguments: ['myTable', 1, {limit: 10}], arguments: ['myTable', 1, {limit: 10}],
expectation: "DELETE FROM \"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"myTable\" WHERE \"id\"=1 LIMIT 10)" expectation: "DELETE FROM \"myTable\" WHERE \"id\" IN (SELECT \"id\" FROM \"myTable\" WHERE \"id\"=1 LIMIT 10)"
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!