Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move-txn-current-check-up #431

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions gorm/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func (t *Transaction) AddAfterCommitHook(hooks ...func(context.Context)) {

// getReadOnlyDBInstance returns the read only db txn if RO DB available otherwise it returns read/write db txn
func getReadOnlyDBTxn(ctx context.Context, opts *databaseOptions, txn *Transaction) (*gorm.DB, error) {
if txn.current != nil {
return txn.current, nil
}
var db *gorm.DB
switch {
case txn.parentRO == nil:
Expand All @@ -121,9 +124,6 @@ func getReadOnlyDBTxn(ctx context.Context, opts *databaseOptions, txn *Transacti
txnOpts := *opts.txOpts
txn.currentOpts.txOpts = &txnOpts
}
if txn.current != nil {
return txn.current, nil
}
db = txn.beginReadOnlyWithContextAndOptions(ctx, txn.currentOpts.txOpts)
if db.Error != nil {
return nil, db.Error
Expand All @@ -136,6 +136,9 @@ func getReadOnlyDBTxn(ctx context.Context, opts *databaseOptions, txn *Transacti

// getReadWriteDBTxn returns the read/write db txn
func getReadWriteDBTxn(ctx context.Context, opts *databaseOptions, txn *Transaction) (*gorm.DB, error) {
if txn.current != nil {
return txn.current, nil
}
var db *gorm.DB
switch {
case txn.parent == nil:
Expand All @@ -152,9 +155,6 @@ func getReadWriteDBTxn(ctx context.Context, opts *databaseOptions, txn *Transact
txnOpts := *opts.txOpts
txn.currentOpts.txOpts = &txnOpts
}
if txn.current != nil {
return txn.current, nil
}
db = txn.beginWithContextAndOptions(ctx, txn.currentOpts.txOpts)
if db.Error != nil {
return nil, db.Error
Expand Down
20 changes: 10 additions & 10 deletions gorm/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,8 @@ func TestBeginFromContextStartWithReadWriteOptions(t *testing.T) {
}
test.txOpts = &sql.TxOptions{}
_, err = beginFromContextWithOptions(ctx, test.withOpts, test.txOpts)
if err != ErrCtxTxnOptMismatch {
t.Error("begin transaction should fail with an error TxOptionsMismatch")
if err != nil {
t.Error("Received an error beginning transaction")
}
} else {
txn1, err := beginFromContextWithOptions(ctx, test.withOpts, test.txOpts)
Expand All @@ -807,14 +807,14 @@ func TestBeginFromContextStartWithReadWriteOptions(t *testing.T) {
}
test.txOpts.ReadOnly = true
_, err = beginFromContextWithOptions(ctx, test.withOpts, test.txOpts)
if err != ErrCtxTxnOptMismatch {
t.Error("begin transaction should fail with an error TxOptionsMismatch")
if err != nil {
t.Error("Received an error beginning transaction")
}
test.txOpts.ReadOnly = false
test.txOpts.Isolation = sql.LevelSerializable
_, err = beginFromContextWithOptions(ctx, test.withOpts, test.txOpts)
if err != ErrCtxTxnOptMismatch {
t.Error("begin transaction should fail with an error TxOptionsMismatch")
if err != nil {
t.Error("Received an error beginning transaction")
}
test.txOpts.Isolation = sql.LevelDefault
test.withOpts = readOnly
Expand All @@ -836,14 +836,14 @@ func TestBeginFromContextStartWithReadWriteOptions(t *testing.T) {
}
test.txOpts.ReadOnly = true
_, err = beginFromContextWithOptions(ctx, test.withOpts, test.txOpts)
if err != ErrCtxTxnOptMismatch {
t.Error("begin transaction should fail with an error TxOptionsMismatch")
if err != nil {
t.Error("Received an error beginning transaction")
}
test.txOpts.ReadOnly = false
test.txOpts.Isolation = sql.LevelSerializable
_, err = beginFromContextWithOptions(ctx, test.withOpts, test.txOpts)
if err != ErrCtxTxnOptMismatch {
t.Error("begin transaction should fail with an error TxOptionsMismatch")
if err != nil {
t.Error("Received an error beginning transaction")
}
txn3, err := beginFromContextWithOptions(ctx, test.withOpts, nil)
if err != nil {
Expand Down
Loading