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

Commit 4d82907d by Patrick Carnahan Committed by GitHub

fix(mssql): upsert query with falsey values (#12453) (#12459)

1 parent 9a45509e
...@@ -465,7 +465,7 @@ class MSSQLQueryGenerator extends AbstractQueryGenerator { ...@@ -465,7 +465,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 * 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) { for (const key in clause) {
if (!clause[key]) { if (typeof clause[key] === 'undefined' || clause[key] == null) {
valid = false; valid = false;
break; break;
} }
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
const Support = require('../../support'); const Support = require('../../support');
const expectsql = Support.expectsql; const expectsql = Support.expectsql;
const current = Support.sequelize; const current = Support.sequelize;
const DataTypes = require('../../../../lib/data-types');
const Op = require('../../../../lib/operators');
const TableHints = require('../../../../lib/table-hints'); const TableHints = require('../../../../lib/table-hints');
const QueryGenerator = require('../../../../lib/dialects/mssql/query-generator'); const QueryGenerator = require('../../../../lib/dialects/mssql/query-generator');
...@@ -21,6 +23,56 @@ if (current.dialect.name === 'mssql') { ...@@ -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() { it('createDatabaseQuery with collate', function() {
expectsql(this.queryGenerator.createDatabaseQuery('myDatabase', { collate: 'Latin1_General_CS_AS_KS_WS' }), { 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;" mssql: "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = 'myDatabase' ) BEGIN CREATE DATABASE [myDatabase] COLLATE N'Latin1_General_CS_AS_KS_WS'; END;"
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!