-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CPDP-8817: expose context based transaction support for data store me…
…thods - Currently transaction support is only available create/upsert methods. - Update/Delete/Find still doesn't support them due to chaining of the where clauses
- Loading branch information
1 parent
7b623b2
commit a386dde
Showing
6 changed files
with
375 additions
and
53 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,43 @@ | ||
package authorizer | ||
|
||
import ( | ||
"context" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
type TransactionContextKey string | ||
|
||
const ( | ||
TransactionCtx = TransactionContextKey("DB_TRANSACTION") | ||
) | ||
|
||
type TransactionFetcher interface { | ||
IsTransactionCtx(ctx context.Context) bool | ||
GetTransactionCtx(ctx context.Context) *gorm.DB | ||
WithTransactionCtx(ctx context.Context, tx *gorm.DB) context.Context | ||
} | ||
|
||
type SimpleTransactionFetcher struct{} | ||
|
||
func (s SimpleTransactionFetcher) GetTransactionCtx(ctx context.Context) *gorm.DB { | ||
if v := ctx.Value(TransactionCtx); v != nil { | ||
if dbTx, ok := v.(*gorm.DB); ok { | ||
return dbTx | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (s SimpleTransactionFetcher) WithTransactionCtx(ctx context.Context, tx *gorm.DB) context.Context { | ||
return context.WithValue(ctx, TransactionCtx, tx) | ||
} | ||
|
||
func (s SimpleTransactionFetcher) IsTransactionCtx(ctx context.Context) bool { | ||
if v := ctx.Value(TransactionCtx); v != nil { | ||
if _, ok := v.(*gorm.DB); ok { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
Oops, something went wrong.