fix(11-08): bind nested callbacks to parent transaction
This commit is contained in:
@@ -146,6 +146,39 @@ func TestTransactionAfterCommit(t *testing.T) {
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("nested_rejects_unrelated_transaction_handle", func(t *testing.T) {
|
||||
var r recorder
|
||||
err := Transaction(ctx, gdb, func(ctx context.Context, _ *gorm.DB) error {
|
||||
foreign := gdb.WithContext(ctx).Begin()
|
||||
if foreign.Error != nil {
|
||||
return foreign.Error
|
||||
}
|
||||
defer foreign.Rollback()
|
||||
entered := false
|
||||
err := Transaction(ctx, foreign, func(ctx context.Context, tx *gorm.DB) error {
|
||||
entered = true
|
||||
AfterCommit(ctx, tx, func(context.Context, *gorm.DB) { r.add("foreign") })
|
||||
return tx.Create(&acItem{Name: "foreign-nested"}).Error
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "parent transaction handle") {
|
||||
t.Fatalf("unrelated nested handle err = %v", err)
|
||||
}
|
||||
if entered {
|
||||
t.Fatal("unrelated nested transaction entered fn")
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got := r.names(); len(got) != 0 {
|
||||
t.Fatalf("callbacks = %v, want none", got)
|
||||
}
|
||||
if committedCount(t, db, "foreign-nested") != 0 {
|
||||
t.Fatal("unrelated nested transaction committed work")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("panicking_callback_does_not_fail_commit", func(t *testing.T) {
|
||||
var r recorder
|
||||
err := Transaction(ctx, gdb, func(ctx context.Context, tx *gorm.DB) error {
|
||||
|
||||
Reference in New Issue
Block a user