Skip to content

Eliminate code duplication requests between Upsert and Insert #3287

Open
aaronburtle wants to merge 5 commits intomainfrom
dev/aaronburtle/FactorOutCodeDuplicationInInsertAndUpsert
Open

Eliminate code duplication requests between Upsert and Insert #3287
aaronburtle wants to merge 5 commits intomainfrom
dev/aaronburtle/FactorOutCodeDuplicationInInsertAndUpsert

Conversation

@aaronburtle
Copy link
Contributor

Why make this change?

Closes #3209

What is this change?

Allow Upserts coming from REST requests to fall through to the Insert logic when auto-generated primary keys are missing. This eliminates code duplication between these two ops in the SqlMutationEngine

Add a test to cover the case when we have a PUT/PATCH with keys provided in the request body.

How was this tested?

New test was added to ensure complete coverage over the scenarios introduced with the keyless PUT/PATCH change.

Sample Request(s)

PUT that updates existing row (expects 200 OK):

PUT https://localhost:5001/api/magazine
Content-Type: application/json

{
    "id": 1,
    "title": "Updated Vogue",
    "issue_number": 9999
}

PUT that inserts new row (expects 201 Created):

PUT https://localhost:5001/api/magazine
Content-Type: application/json

{
    "id": 5001,
    "title": "Brand New Magazine",
    "issue_number": 42
}

PATCH that updates existing row (expects 200 OK):

PATCH https://localhost:5001/api/magazine
Content-Type: application/json

{
    "id": 1,
    "title": "Updated Vogue"
}

PATCH that inserts new row (expects 201 Created):

PATCH https://localhost:5001/api/magazine
Content-Type: application/json

{
    "id": 5001,
    "title": "Brand New Magazine"
}

@aaronburtle
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@aaronburtle
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@aaronburtle aaronburtle marked this pull request as ready for review March 17, 2026 19:12
Copilot AI review requested due to automatic review settings March 17, 2026 19:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates SQL mutation handling so REST upserts (PUT/PATCH) without a PK in the route can reuse the existing insert/update flow: if PKs are absent it behaves like an insert, and if PKs are supplied in the body it can correctly update an existing row. It also adds regression tests to cover keyless PUT/PATCH requests where PKs are provided in the request body.

Changes:

  • Promote primary key values from request body into PrimaryKeyValuePairs for keyless upsert requests when all PK fields are present.
  • Treat keyless upsert requests that still lack PKs as effective inserts to avoid generating unsafe UPDATE statements.
  • Add cross-DB REST tests for keyless PUT/PATCH upsert behavior when PKs are included in the body.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Service.Tests/SqlTests/RestApiTests/Put/PutApiTestBase.cs Adds PUT regression tests for keyless routes with PKs in body; minor comment line touched.
src/Service.Tests/SqlTests/RestApiTests/Put/PostgreSqlPutApiTests.cs Adds SQL verification queries for new PUT tests (PostgreSQL).
src/Service.Tests/SqlTests/RestApiTests/Put/MySqlPutApiTests.cs Adds SQL verification queries for new PUT tests (MySQL).
src/Service.Tests/SqlTests/RestApiTests/Put/MsSqlPutApiTests.cs Adds SQL verification queries for new PUT tests (SQL Server).
src/Service.Tests/SqlTests/RestApiTests/Patch/PatchApiTestBase.cs Adds PATCH regression tests for keyless routes with PKs in body.
src/Service.Tests/SqlTests/RestApiTests/Patch/PostgreSqlPatchApiTests.cs Adds SQL verification queries for new PATCH tests (PostgreSQL).
src/Service.Tests/SqlTests/RestApiTests/Patch/MySqlPatchApiTests.cs Adds SQL verification queries for new PATCH tests (MySQL).
src/Service.Tests/SqlTests/RestApiTests/Patch/MsSqlPatchApiTests.cs Adds SQL verification queries for new PATCH tests (SQL Server).
src/Core/Resolvers/SqlMutationEngine.cs Refactors upsert flow so keyless upserts either use body PKs for UPDATE or fall through to INSERT path.

aaronburtle and others added 2 commits March 17, 2026 12:45
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@aaronburtle
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 6 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Code duplication in Upsert and Insert operations

2 participants