Skip to content

feat: add txn interceptor with gorm v2#487

Merged
chinmayb merged 1 commit intoinfobloxopen:masterfrom
chinmayb:gorm-v2-transaction
Sep 18, 2025
Merged

feat: add txn interceptor with gorm v2#487
chinmayb merged 1 commit intoinfobloxopen:masterfrom
chinmayb:gorm-v2-transaction

Conversation

@chinmayb
Copy link
Copy Markdown
Contributor

@chinmayb chinmayb commented Sep 18, 2025

func (m *mockServerStream) SendMsg(interface{}) error { return nil }
func (m *mockServerStream) RecvMsg(interface{}) error { return nil }

func TestStreamServerInterceptor(t *testing.T) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

additional test cases for stream interceptor is added

Copy link
Copy Markdown
Contributor

@amaskalenka amaskalenka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

You can also leverage db.Transaction method to get rid of the custom code for Commit and Rollback

var resp interface{}
terr := db.Transaction(func(tx *gorm.DB) error {
    ctx = NewContext(ctx, txn)
    r, err := handler(ctx, req)
    if err != nil {
        return err
    }
    resp = r
    return nil
})

if terr != nil {
     return nil, err
}

return resp, nil

@chinmayb
Copy link
Copy Markdown
Contributor Author

@amaskalenka Good point! I will take it up as improvement and add it as a separate PR as I'd need to modify tests.

@chinmayb chinmayb merged commit 475fd26 into infobloxopen:master Sep 18, 2025
2 checks passed
@chinmayb
Copy link
Copy Markdown
Contributor Author

chinmayb commented Sep 18, 2025

@amaskalenka just noticed that, there are commitHooks, that support will be lost if we use db.Transaction. So its better to keep it as it is ?

txn := &Transaction{parent: txn.parent, afterCommitHook: txn.afterCommitHook}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants