Skip to content
Merged
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
5 changes: 5 additions & 0 deletions internal/database/sqlcommon/sqlcommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ func (s *SQLCommon) Init(ctx context.Context, provider Provider, prefix config.P
func (s *SQLCommon) Capabilities() *database.Capabilities { return s.capabilities }

func (s *SQLCommon) RunAsGroup(ctx context.Context, fn func(ctx context.Context) error) error {
if tx := getTXFromContext(ctx); tx != nil {
// transaction already exists - just continue using it
return fn(ctx)
}

ctx, tx, _, err := s.beginOrUseTx(ctx)
if err != nil {
return err
Expand Down
8 changes: 8 additions & 0 deletions internal/database/sqlcommon/sqlcommon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ func TestRunAsGroup(t *testing.T) {
// Query, not specifying a transaction
_, _, err = s.query(ctx, sq.Select("test").From("test"))
assert.NoError(t, err)

// Nested call
err = s.RunAsGroup(ctx, func(ctx2 context.Context) error {
assert.Equal(t, ctx, ctx2)
return nil
})
assert.NoError(t, err)

return
})

Expand Down
5 changes: 4 additions & 1 deletion pkg/database/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,10 @@ type PeristenceInterface interface {

// RunAsGroup instructs the database plugin that all database operations performed within the context
// function can be grouped into a single transaction (if supported).
// Note, the caller is responsible for passing the context back to all database operations performed within the supplied function.
// Requirements:
// - Firefly must not depend on this to guarantee ACID properties (it is only a suggestion/optimization)
// - The database implementation must support nested RunAsGroup calls (ie by reusing a transaction if one exists)
// - The caller is responsible for passing the supplied context to all database operations within the callback function
RunAsGroup(ctx context.Context, fn func(ctx context.Context) error) error

iNamespaceCollection
Expand Down