Skip to content

Commit

Permalink
fix(mssql): upsert query with falsey values (sequelize#12453)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickcarnahan committed Jul 15, 2020
1 parent 3d2df28 commit c746dc8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/dialects/mssql/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator {
* Exclude NULL Composite PK/UK. Partial Composite clauses should also be excluded as it doesn't guarantee a single row
*/
for (const key in clause) {
if (!clause[key]) {
if (typeof clause[key] === 'undefined' || clause[key] == null) {
valid = false;
break;
}
Expand Down
52 changes: 52 additions & 0 deletions test/unit/dialects/mssql/query-generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
const Support = require('../../support');
const expectsql = Support.expectsql;
const current = Support.sequelize;
const DataTypes = require('../../../../lib/data-types');
const Op = require('../../../../lib/operators');
const TableHints = require('../../../../lib/table-hints');
const QueryGenerator = require('../../../../lib/dialects/mssql/query-generator');

Expand All @@ -21,6 +23,56 @@ if (current.dialect.name === 'mssql') {
});
});

it('upsertQuery with falsey values', function() {
const testTable = this.sequelize.define(
'test_table',
{
Name: {
type: DataTypes.STRING,
primaryKey: true
},
Age: {
type: DataTypes.INTEGER
},
IsOnline: {
type: DataTypes.BOOLEAN,
primaryKey: true
}
},
{
freezeTableName: true,
timestamps: false
}
);

const insertValues = {
Name: 'Charlie',
Age: 24,
IsOnline: false
};

const updateValues = {
Age: 24
};

const whereValues = [
{
Name: 'Charlie',
IsOnline: false
}
];

const where = {
[Op.or]: whereValues
};

// the main purpose of this test is to validate this does not throw
expectsql(this.queryGenerator.upsertQuery('test_table', updateValues, insertValues, where, testTable), {
mssql:
"MERGE INTO [test_table] WITH(HOLDLOCK) AS [test_table_target] USING (VALUES(24)) AS [test_table_source]([Age]) ON [test_table_target].[Name] = [test_table_source].[Name] AND [test_table_target].[IsOnline] = [test_table_source].[IsOnline] WHEN MATCHED THEN UPDATE SET [test_table_target].[Name] = N'Charlie', [test_table_target].[Age] = 24, [test_table_target].[IsOnline] = 0 WHEN NOT MATCHED THEN INSERT ([Age]) VALUES(24) OUTPUT $action, INSERTED.*;"
});
});

it('createDatabaseQuery with collate', function() {
expectsql(this.queryGenerator.createDatabaseQuery('myDatabase', { collate: 'Latin1_General_CS_AS_KS_WS' }), {
mssql: "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'myDatabase' ) BEGIN CREATE DATABASE [myDatabase] COLLATE N'Latin1_General_CS_AS_KS_WS'; END;"
Expand Down

0 comments on commit c746dc8

Please sign in to comment.