UPSERT functionality for MSSQL#6842
UPSERT functionality for MSSQL#6842sushantdhiman merged 5 commits intosequelize:masterfrom harshithkashyap:mssql-upsert
Conversation
|
@harshithkashyap, thanks for your PR! By analyzing the history of the files in this pull request, we identified @mbroadst, @felixfbecker and @sushantdhiman to be potential reviewers. |
| docs/api/tmp.md | ||
| ssce.js | ||
| coverage | ||
| .vscode/ No newline at end of file |
There was a problem hiding this comment.
Out of scope for this PR. You can add this to .git/info/exclude
There was a problem hiding this comment.
No this is ok, we are already ignoring .DS_STORE and .idea files
There was a problem hiding this comment.
Removed the parens from arrow functions. Keeping the .gitignore change for now
|
|
||
| const updateKeys = Object.keys(updateValues); | ||
| const insertKeys = Object.keys(insertValues); | ||
| const insertKeysQuoted = insertKeys.map((key) => this.quoteIdentifier(key)).join(', '); |
There was a problem hiding this comment.
Unneeded parens (applies to all arrow functions you used)
Current coverage is 93.68% (diff: 100%)
|
|
Great job on submitting a completed upsert for MSSQL. 👍 This is one edge case bug I found trying this: If a table has 2 non-PK unique indexes and the PK was not one of the merge attributes it will always default to use the PK and cause a SQL error. The following code always assumes that if 2 clauses were found that one must be the PK which is not strictly true.
While it is an unlikely case there is nothing stopping people from setting up their tables in this manner. Consider revising the clause generation section with this:
|
|
@smassin Thanks for the review. :) Yes, this would fail when there are two unique keys on table. I'll push a commit with the changes. |
|
Additionally, and this is not an error in your code, but we really should use a lock to ensure atomicity of the MERGE statement. See: The strong recommendation is use WITH (HOLDLOCK) to ensure atomicity in concurrent situations. Ex.
|
|
@harshithkashyap Please rebase and we can merge it |
…(HOLDLOCK) to prevent concurrency issues, integration test for multiple unique keys
|
Hi there, The check of the unique columns ignores the definitions through the model indexes option. the upsert uniqueAttrs list will end up without the email field. The code always uses all unique columns to generate the ON condition, regardless of their presence in the upsert values parameter.
source has no column email |
|
@janmeier @sushantdhiman We pass |
Pull Request check-list
npm run testornpm run test-DIALECTpass with this change (including linting)?Futurein the changelog?Description of change
This MR enables
upsertquery for MSSQL dialect using SQL Server MERGE statements.$actionwhich returns Insert/Update based on the executed query.constin places as suggested by eslintWITH(HOLDLOCK)hint to prevent concurrency issue