-
Notifications
You must be signed in to change notification settings - Fork 24
feat(policy): 1659 spike on transactions support #1678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jakedoublev
merged 1 commit into
main
from
1659-spike-basic-transaction-functionality-for-policy
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # Using Database Transactions in Policy Service | ||
|
|
||
| ## Table of Contents | ||
|
|
||
| - [Background](#background) | ||
| - [Chosen Option](#chosen-option) | ||
| - [Considered Options](#considered-options) | ||
| - [LIMIT + OFFSET](#limit--offset) | ||
| - [Keyset Pagination](#keyset-pagination) | ||
| - [Cursor Pagination](#cursor-pagination) | ||
|
|
||
| ## Background | ||
|
|
||
| Due to the complex nature of some operations within the Policy service, we need to consider the use of database transactions to ensure data integrity and consistency. | ||
|
|
||
| ## Use Cases | ||
|
|
||
| Primary drivers for implementing database transactions: | ||
| - Unsafe operations that update namespace/attribute definition names or attribute values and require FQNs to be updated for consistency | ||
| - Attribute defintion creation with multiple attribute values that must be created atomically | ||
|
|
||
| > [!NOTE] | ||
| > There are likely to be more as the service matures and more complex operations are required. | ||
|
|
||
| ## Implementation | ||
|
|
||
| An abstraction over transactional operations will be introduced to hopefully simplify support for multiple database providers in the future: | ||
|
|
||
| ```go | ||
| type DbTransaction interface { | ||
| Commit(ctx context.Context) error | ||
| Rollback(ctx context.Context) | ||
| } | ||
| ``` | ||
|
|
||
| ### PolicyDBClient Changes | ||
|
|
||
| New methods for creating transactions and executing operations will be added: | ||
|
|
||
| ```go | ||
| // Starts a new transaction and returns it | ||
| func (c *PolicyDBClient) Begin() (DbTransaction, error) { | ||
| // use desired database client to start a new transaction | ||
| // - initial implementation will use pgx.Tx with Postgres | ||
| } | ||
| ``` | ||
|
|
||
| ```go | ||
| // provides a PolicyDBClient instance using the transaction's connection | ||
| func (c *PolicyDBClient) WithTx(tx DbTransaction) *PolicyDBClient { | ||
| // use desired database client to create a new PolicyDBClient instance with the transaction's connection | ||
| // - initial implementation will use pgx.Tx with Postgres | ||
| } | ||
| ``` | ||
|
|
||
| ### Usage Example | ||
|
|
||
| It is recommended to create the transaction within the service layer, as calling `WithTx()` from the service layer will inherently provide the transaction to any further `PolicyDBClient` methods called within the DB layer. | ||
|
|
||
| ```go | ||
| tx, err := dbClient.Begin() | ||
| if err != nil { | ||
| // handle error | ||
| } | ||
| defer tx.Rollback(ctx) | ||
|
|
||
| result, err := dbClient.WithTx(tx).SomeOperation(ctx, ...params) | ||
| if err != nil { | ||
| // handle error | ||
| } | ||
|
|
||
| nextResult, err := dbClient.WithTx(tx).AnotherOperation(ctx, ...params) | ||
| if err != nil { | ||
| // handle error | ||
| } | ||
|
|
||
| err = tx.Commit(ctx) | ||
| if err != nil { | ||
| // handle error | ||
| } | ||
| ``` | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.