MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / WithTx

Function WithTx

app/controlplane/pkg/data/data.go:170–191  ·  view source on GitHub ↗

WithTx initiates a transaction and wraps the DB function

(ctx context.Context, client *ent.Client, fn func(tx *ent.Tx) error)

Source from the content-addressed store, hash-verified

168
169// WithTx initiates a transaction and wraps the DB function
170func WithTx(ctx context.Context, client *ent.Client, fn func(tx *ent.Tx) error) error {
171 tx, err := client.Tx(ctx)
172 if err != nil {
173 return err
174 }
175 defer func() {
176 if v := recover(); v != nil {
177 _ = tx.Rollback()
178 panic(v)
179 }
180 }()
181 if err = fn(tx); err != nil {
182 if rerr := tx.Rollback(); rerr != nil {
183 err = fmt.Errorf("%w: rolling back transaction: %w", err, rerr)
184 }
185 return err
186 }
187 if err = tx.Commit(); err != nil {
188 return fmt.Errorf("committing transaction: %w", err)
189 }
190 return nil
191}

Callers 15

CreateMethod · 0.85
SoftDeleteMethod · 0.85
SaveMethod · 0.85
CreateMethod · 0.85
UpdateMethod · 0.85
CreateMethod · 0.85
MarkAsLatestMethod · 0.85
CreateMethod · 0.85
SoftDeleteMethod · 0.85
AddMemberToGroupMethod · 0.85
RemoveMemberFromGroupMethod · 0.85

Calls 3

RollbackMethod · 0.65
CommitMethod · 0.65
TxMethod · 0.45

Tested by

no test coverage detected