-
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.
- Loading branch information
Showing
6 changed files
with
151 additions
and
4 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
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,52 @@ | ||
package gormtx | ||
|
||
import ( | ||
"context" | ||
"github.com/aneshas/tx/v2" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var ( | ||
_ tx.DB = &DB{} | ||
_ tx.Transaction = &Tx{} | ||
) | ||
|
||
// NewDB instantiates new tx.DB *gorm.DB wrapper | ||
func NewDB(db *gorm.DB) tx.DB { | ||
return &DB{DB: db} | ||
} | ||
|
||
// DB implements tx.DB | ||
type DB struct { | ||
*gorm.DB | ||
} | ||
|
||
// Begin begins gorm transaction | ||
func (db *DB) Begin(ctx context.Context) (tx.Transaction, error) { | ||
txx := db.WithContext(ctx).Begin() | ||
if txx.Error != nil { | ||
return nil, txx.Error | ||
} | ||
|
||
return &Tx{txx}, nil | ||
} | ||
|
||
// Tx wraps *gorm.DB in order top implement tx.Transaction | ||
type Tx struct { | ||
*gorm.DB | ||
} | ||
|
||
// Commit commits the transaction | ||
func (t Tx) Commit(_ context.Context) error { | ||
return t.DB.Commit().Error | ||
} | ||
|
||
// Rollback rolls back the transaction | ||
func (t Tx) Rollback(_ context.Context) error { | ||
return t.DB.Rollback().Error | ||
} | ||
|
||
// From returns underlying *gorm.DB (wrapped in *Tx) | ||
func From(ctx context.Context) (*Tx, bool) { | ||
return tx.From[*Tx](ctx) | ||
} |
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,76 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package gormtx_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/aneshas/tx/v2" | ||
"github.com/aneshas/tx/v2/gormtx" | ||
"github.com/aneshas/tx/v2/testutil" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/stretchr/testify/assert" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
"testing" | ||
) | ||
|
||
var ( | ||
pool *pgxpool.Pool | ||
db *gorm.DB | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
t := new(testing.T) | ||
|
||
p, sqlDB := testutil.SetupDB(t) | ||
|
||
gormDB, err := gorm.Open(postgres.New(postgres.Config{ | ||
Conn: sqlDB, | ||
}), &gorm.Config{}) | ||
|
||
assert.NoError(t, err) | ||
|
||
pool = p | ||
db = gormDB | ||
|
||
m.Run() | ||
} | ||
|
||
func TestShould_Commit_Sql_Transaction(t *testing.T) { | ||
name := "success_sql" | ||
|
||
doSql(t, tx.New(gormtx.NewDB(db)), name, false) | ||
testutil.AssertSuccess(t, pool, name) | ||
} | ||
|
||
func TestShould_Rollback_Sql_Transaction(t *testing.T) { | ||
name := "failure_sql" | ||
|
||
doSql(t, tx.New(gormtx.NewDB(db)), name, true) | ||
testutil.AssertFailure(t, pool, name) | ||
} | ||
|
||
func doSql(t *testing.T, transactor *tx.TX, name string, fail bool) { | ||
t.Helper() | ||
|
||
err := transactor.WithTransaction(context.TODO(), func(ctx context.Context) error { | ||
ttx, _ := gormtx.From(ctx) | ||
|
||
db := ttx.Exec(`insert into cats (name) values(?)`, name) | ||
if db.Error != nil { | ||
return db.Error | ||
} | ||
|
||
if fail { | ||
return fmt.Errorf("db error") | ||
} | ||
|
||
return db.Error | ||
}) | ||
|
||
if !fail { | ||
assert.NoError(t, err) | ||
} | ||
} |
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