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

Commit 8b20a45a by Ilia Ermolin Committed by Sushant

fix(instance/decrement): doesn't work with negative value (#8267)

1 parent e41590af
......@@ -403,7 +403,7 @@ const QueryGenerator = {
for (const key in attrValueHash) {
const value = attrValueHash[key];
values.push(this.quoteIdentifier(key) + '=' + this.quoteIdentifier(key) + operator + this.escape(value));
values.push(this.quoteIdentifier(key) + '=' + this.quoteIdentifier(key) + operator + ' ' + this.escape(value));
}
attributes = attributes || {};
......
......@@ -398,6 +398,22 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
});
});
it('with negative value', function () {
const self = this;
return this.User.findById(1).then(user1 => {
return self.sequelize.Promise.all([
user1.decrement('aNumber', { by: -2 }),
user1.decrement(['aNumber', 'bNumber'], { by: -2 }),
user1.decrement({ 'aNumber': -1, 'bNumber': -2 }),
]).then(() => {
return self.User.findById(1).then(user3 => {
expect(user3.aNumber).to.be.equal(+5);
expect(user3.bNumber).to.be.equal(+4);
});
});
});
});
it('with timestamps set to true', function() {
const User = this.sequelize.define('IncrementUser', {
aNumber: DataTypes.INTEGER
......
......@@ -175,22 +175,27 @@ if (current.dialect.name === 'mssql') {
[{
title:'Should use the plus operator',
arguments: ['+', 'myTable', { foo: 'bar' }, {}, {}],
expectation: 'UPDATE myTable SET foo=foo+\'bar\' '
expectation: 'UPDATE myTable SET foo=foo+ \'bar\' '
},
{
title:'Should use the plus operator with where clause',
arguments: ['+', 'myTable', { foo: 'bar' }, { bar: 'biz'}, {}],
expectation: 'UPDATE myTable SET foo=foo+\'bar\' WHERE bar = \'biz\''
expectation: 'UPDATE myTable SET foo=foo+ \'bar\' WHERE bar = \'biz\''
},
{
title:'Should use the minus operator',
arguments: ['-', 'myTable', { foo: 'bar' }, {}, {}],
expectation: 'UPDATE myTable SET foo=foo-\'bar\' '
expectation: 'UPDATE myTable SET foo=foo- \'bar\' '
},
{
title:'Should use the minus operator with negative value',
arguments: ['-', 'myTable', { foo: -1 }, {}, {}],
expectation: 'UPDATE myTable SET foo=foo- -1 '
},
{
title:'Should use the minus operator with where clause',
arguments: ['-', 'myTable', { foo: 'bar' }, { bar: 'biz'}, {}],
expectation: 'UPDATE myTable SET foo=foo-\'bar\' WHERE bar = \'biz\''
expectation: 'UPDATE myTable SET foo=foo- \'bar\' WHERE bar = \'biz\''
}].forEach(test => {
it(test.title, () => {
expectsql(QueryGenerator.arithmeticQuery.call(QueryGenerator, test.arguments), {
......
......@@ -14,22 +14,27 @@ if (dialect === 'mysql') {
{
title:'Should use the plus operator',
arguments: ['+', 'myTable', { foo: 'bar' }, {}, {}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`+\'bar\' '
expectation: 'UPDATE `myTable` SET `foo`=`foo`+ \'bar\' '
},
{
title:'Should use the plus operator with where clause',
arguments: ['+', 'myTable', { foo: 'bar' }, { bar: 'biz'}, {}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`+\'bar\' WHERE `bar` = \'biz\''
expectation: 'UPDATE `myTable` SET `foo`=`foo`+ \'bar\' WHERE `bar` = \'biz\''
},
{
title:'Should use the minus operator',
arguments: ['-', 'myTable', { foo: 'bar' }, {}, {}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`-\'bar\' '
expectation: 'UPDATE `myTable` SET `foo`=`foo`- \'bar\' '
},
{
title:'Should use the minus operator with negative value',
arguments: ['-', 'myTable', { foo: -1 }, {}, {}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`- -1 '
},
{
title:'Should use the minus operator with where clause',
arguments: ['-', 'myTable', { foo: 'bar' }, { bar: 'biz'}, {}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`-\'bar\' WHERE `bar` = \'biz\''
expectation: 'UPDATE `myTable` SET `foo`=`foo`- \'bar\' WHERE `bar` = \'biz\''
}
],
attributesToSQL: [
......
......@@ -17,22 +17,27 @@ if (dialect.match(/^postgres/)) {
{
title:'Should use the plus operator',
arguments: ['+', 'myTable', { foo: 'bar' }, {}, {}],
expectation: 'UPDATE "myTable" SET "foo"="foo"+\'bar\' RETURNING *'
expectation: 'UPDATE "myTable" SET "foo"="foo"+ \'bar\' RETURNING *'
},
{
title:'Should use the plus operator with where clause',
arguments: ['+', 'myTable', { foo: 'bar' }, { bar: 'biz'}, {}],
expectation: 'UPDATE "myTable" SET "foo"="foo"+\'bar\' WHERE "bar" = \'biz\' RETURNING *'
expectation: 'UPDATE "myTable" SET "foo"="foo"+ \'bar\' WHERE "bar" = \'biz\' RETURNING *'
},
{
title:'Should use the minus operator',
arguments: ['-', 'myTable', { foo: 'bar' }, {}, {}],
expectation: 'UPDATE "myTable" SET "foo"="foo"-\'bar\' RETURNING *'
expectation: 'UPDATE "myTable" SET "foo"="foo"- \'bar\' RETURNING *'
},
{
title:'Should use the minus operator with negative value',
arguments: ['-', 'myTable', { foo: -1 }, {}, {}],
expectation: 'UPDATE "myTable" SET "foo"="foo"- -1 RETURNING *'
},
{
title:'Should use the minus operator with where clause',
arguments: ['-', 'myTable', { foo: 'bar' }, { bar: 'biz'}, {}],
expectation: 'UPDATE "myTable" SET "foo"="foo"-\'bar\' WHERE "bar" = \'biz\' RETURNING *'
expectation: 'UPDATE "myTable" SET "foo"="foo"- \'bar\' WHERE "bar" = \'biz\' RETURNING *'
}
],
attributesToSQL: [
......
......@@ -23,22 +23,27 @@ if (dialect === 'sqlite') {
{
title:'Should use the plus operator',
arguments: ['+', 'myTable', { foo: 'bar' }, {}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`+\'bar\' '
expectation: 'UPDATE `myTable` SET `foo`=`foo`+ \'bar\' '
},
{
title:'Should use the plus operator with where clause',
arguments: ['+', 'myTable', { foo: 'bar' }, { bar: 'biz'}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`+\'bar\' WHERE `bar` = \'biz\''
expectation: 'UPDATE `myTable` SET `foo`=`foo`+ \'bar\' WHERE `bar` = \'biz\''
},
{
title:'Should use the minus operator',
arguments: ['-', 'myTable', { foo: 'bar' }],
expectation: 'UPDATE `myTable` SET `foo`=`foo`-\'bar\' '
expectation: 'UPDATE `myTable` SET `foo`=`foo`- \'bar\' '
},
{
title:'Should use the minus operator with negative value',
arguments: ['-', 'myTable', { foo: -1 }],
expectation: 'UPDATE `myTable` SET `foo`=`foo`- -1 '
},
{
title:'Should use the minus operator with where clause',
arguments: ['-', 'myTable', { foo: 'bar' }, { bar: 'biz'}],
expectation: 'UPDATE `myTable` SET `foo`=`foo`-\'bar\' WHERE `bar` = \'biz\''
expectation: 'UPDATE `myTable` SET `foo`=`foo`- \'bar\' WHERE `bar` = \'biz\''
}
],
attributesToSQL: [
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!