Skip to content

Commit

Permalink
fix: bulk destroy query conditions duplicated in sequelize mode (#295)
Browse files Browse the repository at this point in the history
Co-authored-by: JimmyDaddy <[email protected]>
  • Loading branch information
JimmyDaddy and JimmyDaddy authored Mar 21, 2022
1 parent e8378d5 commit 90bb2c8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/adapters/sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,12 @@ module.exports = Bone => {
}

// proxy to class.destroy({ individualHooks=false }) see https://github.com/sequelize/sequelize/blob/4063c2ab627ad57919d5b45cc7755f077a69fa5e/lib/model.js#L2895 before(after)BulkDestroy
static async bulkDestroy(options = {}) {
static bulkDestroy(options = {}) {
const { where, force } = options;
const spell = this._remove(where || {}, force, { ...options });
translateOptions(spell, options);
const transOptions = { ...options };
delete transOptions.where;
translateOptions(spell, transOptions);
return spell;
}

Expand Down
49 changes: 49 additions & 0 deletions test/unit/adapters/sequelize.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2000,6 +2000,55 @@ describe('mysql only', () => {
clock.restore();
});

it('bulkDestroy should not duplicate query conditions', async () => {
const fakeDate = new Date(`2012-12-14 12:00:00`).getTime();
const clock = sinon.useFakeTimers(fakeDate);

assert.notEqual(Post.bulkDestroy({
where: {
title: 'halo',
$or: {
authorId: 1
}
},
limit: 2
}).toSqlString(), "UPDATE `articles` SET `gmt_deleted` = '2012-12-14 12:00:00.000' WHERE `title` = 'halo' AND `author_id` = 1 AND `title` = 'halo' AND `author_id` = 1 AND `gmt_deleted` IS NULL LIMIT 2");

assert.equal(Post.bulkDestroy({
where: {
title: 'halo',
$or: {
authorId: 1
}
},
limit: 2
}).toSqlString(), "UPDATE `articles` SET `gmt_deleted` = '2012-12-14 12:00:00.000' WHERE `title` = 'halo' AND `author_id` = 1 AND `gmt_deleted` IS NULL LIMIT 2");

assert.notEqual(Post.bulkDestroy({
where: {
title: 'halo',
$or: {
authorId: 1
}
},
limit: 2,
force: true,
}).toSqlString(), "DELETE FROM `articles` WHERE `title` = 'halo' AND `author_id` = 1 AND `title` = 'halo' AND `author_id` = 1 LIMIT 2");

assert.equal(Post.bulkDestroy({
where: {
title: 'halo',
$or: {
authorId: 1
}
},
limit: 2,
force: true,
}).toSqlString(), "DELETE FROM `articles` WHERE `title` = 'halo' AND `author_id` = 1 LIMIT 2");

clock.restore();
});

describe('Model.destroy with order, limit (mysql only)', () => {

it('should work', async () => {
Expand Down

0 comments on commit 90bb2c8

Please sign in to comment.