Database policy support for PUT/PATCH operations - PostgreSQL#3694
Open
ArjunNarendra wants to merge 6 commits into
Open
Database policy support for PUT/PATCH operations - PostgreSQL#3694ArjunNarendra wants to merge 6 commits into
ArjunNarendra wants to merge 6 commits into
Conversation
…ate error/success message is returned
…te/update policies
Contributor
There was a problem hiding this comment.
Pull request overview
This PR extends PostgreSQL PUT/PATCH (upsert) behavior to correctly apply database policies for both the update and insert branches, aligning Postgres behavior with the expected policy semantics for REST upserts.
Changes:
- Updated PostgreSQL upsert SQL generation to evaluate update-policy vs create-policy depending on which branch executes, while preserving “try update then insert” semantics.
- Added a PostgreSQL-specific
GetMultipleResultSetsIfAnyAsyncimplementation to interpret multi-result-set upsert outcomes and return 403/404 appropriately. - Updated PostgreSQL REST integration tests and test config to exercise create-policy behavior for PUT/PATCH insert cases.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/Service.Tests/SqlTests/RestApiTests/Put/PostgreSqlPutApiTests.cs | Adds expected SQL for PUT insert-with-policy and removes ignored overrides so tests run. |
| src/Service.Tests/SqlTests/RestApiTests/Patch/PostgreSqlPatchApiTests.cs | Adds expected SQL for PATCH insert-with-policy and removes ignored overrides so tests run. |
| src/Service.Tests/dab-config.PostgreSql.json | Adds a PostgreSQL create-action database policy used by the new PUT/PATCH scenarios. |
| src/Core/Resolvers/SqlMutationEngine.cs | Passes additional context into the result-set handler for upsert result interpretation. |
| src/Core/Resolvers/PostgresQueryBuilder.cs | Reworks PostgreSQL upsert SQL to support separate create/update policies and emit a PK-existence count result set. |
| src/Core/Resolvers/PostgreSqlExecutor.cs | Implements PostgreSQL-specific multi-result-set handling to map policy failures vs not-found. |
| src/Core/Configurations/RuntimeConfigValidator.cs | Allows PostgreSQL to define create-action database policies in config validation. |
| config-generators/postgresql-commands.txt | Updates generator commands to set separate create/read permissions and the new create policy. |
Comment on lines
2056
to
+2059
| queryExecutor.GetMultipleResultSetsIfAnyAsync, | ||
| dataSourceName, | ||
| GetHttpContext(), | ||
| new List<string> { prettyPrintPk, entityName }); | ||
| new List<string> { prettyPrintPk, entityName, isFallbackToUpdate.ToString() }); |
Comment on lines
46
to
51
| private static readonly HashSet<DatabaseType> _databaseTypesSupportingCreatePolicy = | ||
| [ | ||
| DatabaseType.MSSQL, | ||
| DatabaseType.DWSQL | ||
| DatabaseType.DWSQL, | ||
| DatabaseType.PostgreSQL | ||
| ]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why make this change?
As per the behaviour expected from PUT/PATCH operations with database policies discussed in #1430, implement db policy support for PostgreSQL to fix #1372.
What is this change?
Prior to this change, there was only one database policy for each operation. Since now database policies will be supported for both insert (or create)/update actions via PUT/PATCH operations, these 2 operations can have 2 database policies defined for them, one for each action.
The query generated by
PostgresQueryBuilder.Build(SqlUpsertQueryStructure structure)is modified to accommodate create/update policies while also keeping intact the normal upsert behavior expected (try update, then insert).The method
IQueryExecutor.GetMultipleResultSetsIfAnyAsynchas been provided another implementation specific to PostgreSql inPostgreSqlExecutor. TheDbDataReaderinstance for the query being executed for the PUT/PATCH return operation would always contain two result sets.Different scenarios are added to the method PostgreSqlExecutor.GetMultipleResultSetsIfAnyAsync to throw appropriate exceptions (Forbidden/Authorization failure - 403 and NotFound - 404). Appropriate comments are added within the code to demonstrate each case.
How was this tested?
Integration Tests - Done