mirror of
https://github.com/coder/coder.git
synced 2025-07-13 21:36:50 +00:00
fix: Fix nested transactions should function correctly (#2470)
* fix: Fix nested transactions should function correctly Inner tx should reuse outer tx
This commit is contained in:
@ -47,9 +47,17 @@ type sqlQuerier struct {
|
||||
|
||||
// InTx performs database operations inside a transaction.
|
||||
func (q *sqlQuerier) InTx(function func(Store) error) error {
|
||||
if q.sdb == nil {
|
||||
if _, ok := q.db.(*sql.Tx); ok {
|
||||
// If the current inner "db" is already a transaction, we just reuse it.
|
||||
// We do not need to handle commit/rollback as the outer tx will handle
|
||||
// that.
|
||||
err := function(q)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("execute transaction: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
transaction, err := q.sdb.Begin()
|
||||
if err != nil {
|
||||
return xerrors.Errorf("begin transaction: %w", err)
|
||||
|
46
coderd/database/db_test.go
Normal file
46
coderd/database/db_test.go
Normal file
@ -0,0 +1,46 @@
|
||||
//go:build linux
|
||||
|
||||
package database_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/coder/coder/coderd/database"
|
||||
)
|
||||
|
||||
func TestNestedInTx(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
uid := uuid.New()
|
||||
sqlDB := testSQLDB(t)
|
||||
err := database.MigrateUp(sqlDB)
|
||||
require.NoError(t, err, "migrations")
|
||||
|
||||
db := database.New(sqlDB)
|
||||
err = db.InTx(func(outer database.Store) error {
|
||||
return outer.InTx(func(inner database.Store) error {
|
||||
//nolint:gocritic
|
||||
require.Equal(t, outer, inner, "should be same transaction")
|
||||
|
||||
_, err := inner.InsertUser(context.Background(), database.InsertUserParams{
|
||||
ID: uid,
|
||||
Email: "coder@coder.com",
|
||||
Username: "coder",
|
||||
HashedPassword: []byte{},
|
||||
CreatedAt: database.Now(),
|
||||
UpdatedAt: database.Now(),
|
||||
RBACRoles: []string{},
|
||||
})
|
||||
return err
|
||||
})
|
||||
})
|
||||
require.NoError(t, err, "outer tx: %w", err)
|
||||
|
||||
user, err := db.GetUserByID(context.Background(), uid)
|
||||
require.NoError(t, err, "user exists")
|
||||
require.Equal(t, uid, user.ID, "user id expected")
|
||||
}
|
Reference in New Issue
Block a user