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

Commit 2127e4cd by Mick Hansen

Merge pull request #2848 from ekmartin/empty_destroy

Throw an error if no where clause is given to Model.destroy()
2 parents 5cb09bbe 3a0b5452
# Next # Next
- [FEATURE] Throw an error if no where clause is given to `Model.destroy()`.
- [BUG] Fixed issue with `order: sequelize.literal('string')` - [BUG] Fixed issue with `order: sequelize.literal('string')`
- [FEATURE] add `clone: true` support to `.get()`. Is needed when using `delete` on values from a `.get()` (`toJSON()`, `this.values`). (.get() is just a reference to the values for performance reasons when there's no custom getters or includes) - [FEATURE] add `clone: true` support to `.get()`. Is needed when using `delete` on values from a `.get()` (`toJSON()`, `this.values`). (.get() is just a reference to the values for performance reasons when there's no custom getters or includes)
- [FEATURE] add `sequelize.escape(value)` convenience method - [FEATURE] add `sequelize.escape(value)` convenience method
......
...@@ -264,7 +264,7 @@ module.exports = (function() { ...@@ -264,7 +264,7 @@ module.exports = (function() {
var table = this.quoteTable(tableName); var table = this.quoteTable(tableName);
if (options.truncate === true) { if (options.truncate === true) {
// Truncate does not allow LIMIT and WHERE // Truncate does not allow LIMIT and WHERE
return 'TRUNCATE ' + table; return 'TRUNCATE TABLE ' + table;
} }
where = this.getWhereConditions(where); where = this.getWhereConditions(where);
......
...@@ -149,7 +149,7 @@ module.exports = (function() { ...@@ -149,7 +149,7 @@ module.exports = (function() {
} else if (this.isBulkUpdateQuery()) { } else if (this.isBulkUpdateQuery()) {
result = data.length; result = data.length;
} else if (this.isBulkDeleteQuery()){ } else if (this.isBulkDeleteQuery()){
result = data[0].AFFECTEDROWS; result = data[0] && data[0].AFFECTEDROWS;
} else if (this.isVersionQuery()) { } else if (this.isVersionQuery()) {
result = data[0].version; result = data[0].version;
} }
......
...@@ -1412,6 +1412,13 @@ module.exports = (function() { ...@@ -1412,6 +1412,13 @@ module.exports = (function() {
* @return {Promise<undefined>} * @return {Promise<undefined>}
*/ */
Model.prototype.destroy = function(options) { Model.prototype.destroy = function(options) {
var self = this
, instances;
if (!options || !(options.where || options.truncate)) {
throw new Error('Missing where or truncate attribute in the options parameter passed to destroy.');
}
options = Utils._.extend({ options = Utils._.extend({
hooks: true, hooks: true,
individualHooks: false, individualHooks: false,
...@@ -1421,8 +1428,6 @@ module.exports = (function() { ...@@ -1421,8 +1428,6 @@ module.exports = (function() {
options.type = QueryTypes.BULKDELETE; options.type = QueryTypes.BULKDELETE;
var self = this
, instances;
mapFieldNames.call(this, options, this); mapFieldNames.call(this, options, this);
......
...@@ -857,6 +857,19 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -857,6 +857,19 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
describe('update', function() { describe('update', function() {
it('throws an error if no where clause is given', function() {
var User = this.sequelize.define('User', { username: DataTypes.STRING });
return this.sequelize.sync({ force: true }).then(function() {
return User.update();
}).then(function() {
throw new Error('Update should throw an error if no where clause is given.');
}, function(err) {
expect(err).to.be.an.instanceof(Error);
expect(err.message).to.equal('Missing where attribute in the options parameter passed to update.');
});
});
if (current.dialect.supports.transactions) { if (current.dialect.supports.transactions) {
it('supports transactions', function(done) { it('supports transactions', function(done) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { Support.prepareTransactionTest(this.sequelize, function(sequelize) {
...@@ -1081,6 +1094,54 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1081,6 +1094,54 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
describe('destroy', function() { describe('destroy', function() {
it('truncate should clear the table', function() {
var User = this.sequelize.define('User', { username: DataTypes.STRING }),
data = [
{ username: 'user1' },
{ username: 'user2' }
];
return this.sequelize.sync({ force: true }).then(function() {
return User.bulkCreate(data);
}).then(function() {
return User.destroy({ truncate: true });
}).then(function() {
return expect(User.findAll()).to.eventually.have.length(0);
});
});
it('throws an error if no where clause is given', function() {
var User = this.sequelize.define('User', { username: DataTypes.STRING });
return this.sequelize.sync({ force: true }).then(function() {
return User.destroy();
}).then(function() {
throw new Error('Destroy should throw an error if no where clause is given.');
}, function(err) {
expect(err).to.be.an.instanceof(Error);
expect(err.message).to.equal('Missing where or truncate attribute in the options parameter passed to destroy.');
});
});
it('deletes all instances when given an empty where object', function() {
var User = this.sequelize.define('User', { username: DataTypes.STRING }),
data = [
{ username: 'user1' },
{ username: 'user2' }
];
return this.sequelize.sync({ force: true }).then(function() {
return User.bulkCreate(data);
}).then(function() {
return User.destroy({ where: {} });
}).then(function(affectedRows) {
expect(affectedRows).to.equal(2);
return User.findAll();
}).then(function(users) {
expect(users).to.have.length(0);
});
});
if (current.dialect.supports.transactions) { if (current.dialect.supports.transactions) {
it('supports transactions', function(done) { it('supports transactions', function(done) {
Support.prepareTransactionTest(this.sequelize, function(sequelize) { Support.prepareTransactionTest(this.sequelize, function(sequelize) {
...@@ -1089,7 +1150,10 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1089,7 +1150,10 @@ describe(Support.getTestDialectTeaser('Model'), function() {
User.sync({ force: true }).success(function() { User.sync({ force: true }).success(function() {
User.create({ username: 'foo' }).success(function() { User.create({ username: 'foo' }).success(function() {
sequelize.transaction().then(function(t) { sequelize.transaction().then(function(t) {
User.destroy({transaction: t }).success(function() { User.destroy({
where: {},
transaction: t
}).success(function() {
User.count().success(function(count1) { User.count().success(function(count1) {
User.count({ transaction: t }).success(function(count2) { User.count({ transaction: t }).success(function(count2) {
expect(count1).to.equal(1); expect(count1).to.equal(1);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!