mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-23 16:20:29 +00:00
bla
This commit is contained in:
@@ -21,6 +21,8 @@ type Transaction interface {
|
||||
Rollback(ctx context.Context) error
|
||||
End(ctx context.Context, err error) error
|
||||
|
||||
Begin(ctx context.Context, opts *TransactionOptions) (Transaction, error)
|
||||
|
||||
QueryExecutor
|
||||
}
|
||||
|
||||
|
@@ -3,6 +3,7 @@ package gosql
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
"github.com/zitadel/zitadel/backend/storage/database"
|
||||
)
|
||||
@@ -49,6 +50,12 @@ func (tx *sqlTx) Exec(ctx context.Context, sql string, args ...any) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Begin implements [database.Transaction].
|
||||
// it is unimplemented
|
||||
func (tx *sqlTx) Begin(ctx context.Context, opts *database.TransactionOptions) (database.Transaction, error) {
|
||||
return nil, errors.New("nested transactions are not supported")
|
||||
}
|
||||
|
||||
func transactionOptionsToSql(opts *database.TransactionOptions) *sql.TxOptions {
|
||||
if opts == nil {
|
||||
return nil
|
||||
|
@@ -44,13 +44,24 @@ func (tx *pgxTx) QueryRow(ctx context.Context, sql string, args ...any) database
|
||||
return tx.Tx.QueryRow(ctx, sql, args...)
|
||||
}
|
||||
|
||||
// Exec implements [database.Pool].
|
||||
// Exec implements [database.Transaction].
|
||||
// Subtle: this method shadows the method (Pool).Exec of pgxPool.Pool.
|
||||
func (tx *pgxTx) Exec(ctx context.Context, sql string, args ...any) error {
|
||||
_, err := tx.Tx.Exec(ctx, sql, args...)
|
||||
return err
|
||||
}
|
||||
|
||||
// Begin implements [database.Transaction].
|
||||
// As postgres does not support nested transactions we use savepoints to emulate them.
|
||||
// TransactionOptions are ignored as savepoints do not support changing isolation levels.
|
||||
func (tx *pgxTx) Begin(ctx context.Context, _ *database.TransactionOptions) (database.Transaction, error) {
|
||||
savepoint, err := tx.Tx.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pgxTx{savepoint}, nil
|
||||
}
|
||||
|
||||
func transactionOptionsToPgx(opts *database.TransactionOptions) pgx.TxOptions {
|
||||
if opts == nil {
|
||||
return pgx.TxOptions{}
|
||||
|
52
backend/storage/database/handle.go
Normal file
52
backend/storage/database/handle.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package database
|
||||
|
||||
// import (
|
||||
// "context"
|
||||
// "fmt"
|
||||
|
||||
// "github.com/zitadel/zitadel/backend/handler"
|
||||
// )
|
||||
|
||||
// func Begin[In, Out, NextOut any](ctx context.Context, beginner Beginner, opts *TransactionOptions) handler.Defer[In, Out, NextOut] {
|
||||
// // func(ctx context.Context, in *VerifyEmail) (_ *VerifyEmail, _ func(context.Context, error) error, err error) {
|
||||
// return func(handle handler.DeferrableHandle[In, Out], next handler.Handle[Out, NextOut]) handler.Handle[In, NextOut] {
|
||||
// return func(ctx context.Context, in In) (out NextOut, err error) {
|
||||
// tx, err := beginner.Begin(ctx, opts)
|
||||
// if err != nil {
|
||||
// return out, err
|
||||
// }
|
||||
// defer func() {
|
||||
// if err != nil {
|
||||
// rollbackErr := tx.Rollback(ctx)
|
||||
// if rollbackErr != nil {
|
||||
// err = fmt.Errorf("query failed: %w, rollback failed: %v", err, rollbackErr)
|
||||
// }
|
||||
// } else {
|
||||
// err = tx.Commit(ctx)
|
||||
// }
|
||||
// }()
|
||||
// return handle(ctx, in, tx)
|
||||
// }
|
||||
// }
|
||||
|
||||
// }
|
||||
|
||||
// type QueryExecutorSetter interface {
|
||||
// SetQueryExecutor(QueryExecutor)
|
||||
// }
|
||||
|
||||
// func Begin[In QueryExecutorSetter](ctx context.Context, beginner Beginner, in In) (_ In, _ func(context.Context, error) error, err error) {
|
||||
// tx, err := beginner.Begin(ctx, nil)
|
||||
// if err != nil {
|
||||
// return in, nil, err
|
||||
// }
|
||||
// in.SetQueryExecutor(tx)
|
||||
// return in, func(ctx context.Context, err error) error {
|
||||
// err = tx.End(ctx, err)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// in.SetQueryExecutor(beginner)
|
||||
// return nil
|
||||
// }, err
|
||||
// }
|
@@ -118,6 +118,12 @@ func (tx *Transaction) Exec(ctx context.Context, stmt string, args ...any) error
|
||||
return nil
|
||||
}
|
||||
|
||||
// Begin implements [database.Transaction].
|
||||
// it is unimplemented
|
||||
func (tx *Transaction) Begin(ctx context.Context, opts *database.TransactionOptions) (database.Transaction, error) {
|
||||
return nil, errors.New("nested transactions are not supported")
|
||||
}
|
||||
|
||||
// Query implements [database.Transaction].
|
||||
func (tx *Transaction) Query(ctx context.Context, stmt string, args ...any) (database.Rows, error) {
|
||||
e := tx.nextExpecter()
|
||||
|
Reference in New Issue
Block a user