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

Commit 79e3b0f3 by Jan Aagaard Meier

Add cascade option to bulkDelete. Closes #1905

1 parent b987a80f
...@@ -304,12 +304,20 @@ module.exports = (function() { ...@@ -304,12 +304,20 @@ module.exports = (function() {
}, },
deleteQuery: function(tableName, where, options, model) { deleteQuery: function(tableName, where, options, model) {
var query;
options = options || {}; options = options || {};
tableName = Utils.removeTicks(this.quoteTable(tableName), '"'); tableName = Utils.removeTicks(this.quoteTable(tableName), '"');
if (options.truncate === true) { if (options.truncate === true) {
return 'TRUNCATE ' + QueryGenerator.quoteIdentifier(tableName); query = 'TRUNCATE ' + QueryGenerator.quoteIdentifier(tableName);
if (options.cascade) {
query += ' CASCADE';
}
return query;
} }
if (Utils._.isUndefined(options.limit)) { if (Utils._.isUndefined(options.limit)) {
...@@ -322,7 +330,7 @@ module.exports = (function() { ...@@ -322,7 +330,7 @@ module.exports = (function() {
primaryKeys[tableName] = Object.keys(model.primaryKeys); primaryKeys[tableName] = Object.keys(model.primaryKeys);
} }
var query = 'DELETE FROM <%= table %> WHERE <%= primaryKeys %> IN (SELECT <%= primaryKeysSelection %> FROM <%= table %> WHERE <%= where %><%= limit %>)'; query = 'DELETE FROM <%= table %> 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) {
......
...@@ -1260,14 +1260,16 @@ module.exports = (function() { ...@@ -1260,14 +1260,16 @@ module.exports = (function() {
}; };
/** /**
* Delete multiple instances * Delete multiple instances, or set their deletedAt timestamp to the current time if `paranoid` is enabled.
* *
* @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] * @param {Object} [options]
* @param {Boolean} [options.hooks=true] Run before / after bulk destroy hooks? * @param {Boolean} [options.hooks=true] Run before / after bulk destroy hooks?
* @param {Boolean} [options.individualHooks=false] If set to true, destroy will find all records within the where parameter and will execute before / after bulkDestroy hooks on each row * @param {Boolean} [options.individualHooks=false] If set to true, destroy will find all records within the where parameter and will execute before / after bulkDestroy hooks on each row
* @param {Number} [options.limit] How many rows to delete * @param {Number} [options.limit] How many rows to delete
* @param {Boolean} [options.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 * @param {Boolean} [options.force=false] Delete instead of setting deletedAt to current timestamp (only applicable if `paranoid` is enabled)
* @param {Boolean} [options.truncate=false] 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
* @param {Boolean} [options.cascade=false] Only used in conjuction with TRUNCATE. Truncates all tables that have foreign-key references to the named table, or to any tables added to the group due to CASCADE.
* *
* @return {Promise<undefined>} * @return {Promise<undefined>}
*/ */
...@@ -1275,7 +1277,8 @@ module.exports = (function() { ...@@ -1275,7 +1277,8 @@ module.exports = (function() {
options = Utils._.extend({ options = Utils._.extend({
hooks: true, hooks: true,
individualHooks: false, individualHooks: false,
force: false force: false,
cascade: false
}, options || {}); }, options || {});
options.type = QueryTypes.BULKDELETE; options.type = QueryTypes.BULKDELETE;
......
...@@ -771,6 +771,9 @@ if (dialect.match(/^postgres/)) { ...@@ -771,6 +771,9 @@ if (dialect.match(/^postgres/)) {
arguments: ['myTable', undefined, {truncate: true}], arguments: ['myTable', undefined, {truncate: true}],
expectation: "TRUNCATE \"myTable\"" expectation: "TRUNCATE \"myTable\""
}, { }, {
arguments: ['myTable', undefined, {truncate: true, cascade: true}],
expectation: "TRUNCATE \"myTable\" CASCADE"
}, {
arguments: ['myTable', 1, {limit: 10, truncate: true}], arguments: ['myTable', 1, {limit: 10, truncate: true}],
expectation: "TRUNCATE \"myTable\"" expectation: "TRUNCATE \"myTable\""
}, { }, {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!