Skip to content

Commit

Permalink
feat(ledger): detach context when creating transaction (#1635)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-nicolas committed Aug 5, 2024
1 parent 1e0569c commit 8436bef
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"math/big"
"net/http"

"github.com/formancehq/stack/libs/go-libs/contextutil"
"github.com/formancehq/stack/libs/go-libs/pointer"

"github.com/formancehq/stack/libs/go-libs/bun/bunpaginate"
Expand Down Expand Up @@ -101,7 +102,9 @@ func postTransaction(w http.ResponseWriter, r *http.Request) {
return
}

res, err := l.CreateTransaction(r.Context(), getCommandParameters(r), *payload.ToRunScript())
ctx, _ := contextutil.Detached(r.Context())

res, err := l.CreateTransaction(ctx, getCommandParameters(r), *payload.ToRunScript())
if err != nil {
switch {
case engine.IsCommandError(err):
Expand Down
1 change: 0 additions & 1 deletion components/ledger/internal/storage/ledgerstore/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ func (store *Store) InsertLogs(ctx context.Context, activeLogs ...*ledger.Chaine
}(),
}
}))).
On("CONFLICT (idempotency_key) DO NOTHING").
Exec(ctx)
return err
}
Expand Down
26 changes: 1 addition & 25 deletions components/ledger/internal/storage/ledgerstore/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,29 +85,6 @@ func TestGetLastLog(t *testing.T) {
require.Equal(t, tx1.Timestamp, lastLog.Data.(ledger.NewTransactionLogPayload).Transaction.Timestamp)
}

func TestInsertLogSameIdempotencyKey(t *testing.T) {
t.Parallel()
store := newLedgerStore(t)

logTx := ledger.NewTransactionLog(
ledger.NewTransaction().
WithPostings(
ledger.NewPosting("world", "bank", "USD", big.NewInt(100)),
),
map[string]metadata.Metadata{},
)
log := logTx.WithIdempotencyKey("test")

appendLog(t, store, log.ChainLog(nil), log.ChainLog(nil))

logs, err := store.GetLogs(context.Background(), NewGetLogsQuery(PaginatedQueryOptions[any]{
PageSize: 10,
}))
require.NoError(t, err)
require.Len(t, logs.Data, 1)
require.Equal(t, logs.Data[0].Log, *logTx)
}

func TestReadLogWithIdempotencyKey(t *testing.T) {
t.Parallel()
store := newLedgerStore(t)
Expand All @@ -126,8 +103,7 @@ func TestReadLogWithIdempotencyKey(t *testing.T) {
lastLog, err := store.ReadLogWithIdempotencyKey(context.Background(), "test")
require.NoError(t, err)
require.NotNil(t, lastLog)
require.Len(t, ret, 1)
require.Equal(t, *ret[0], *lastLog)
require.Equal(t, *ret, *lastLog)
}

func TestGetLogs(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions components/ledger/internal/storage/ledgerstore/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ func newLedgerStore(t T, hooks ...bun.QueryHook) *Store {
return store
}

func appendLog(t *testing.T, store *Store, logs ...*ledger.ChainedLog) []*ledger.ChainedLog {
err := store.InsertLogs(context.Background(), logs...)
func appendLog(t *testing.T, store *Store, log *ledger.ChainedLog) *ledger.ChainedLog {
err := store.InsertLogs(context.Background(), log)
require.NoError(t, err)
return logs
return log
}

0 comments on commit 8436bef

Please sign in to comment.