-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(ledger): use a unique index on idempotency key and ledger
- Loading branch information
1 parent
8436bef
commit a34eec5
Showing
3 changed files
with
99 additions
and
0 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
3 changes: 3 additions & 0 deletions
3
components/ledger/internal/storage/ledgerstore/migrations/8-ik-ledger-unique-index.sql
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,3 @@ | ||
drop index logs_idempotency_key; | ||
|
||
create unique index logs_idempotency_key on logs (ledger, idempotency_key); |
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,86 @@ | ||
package suite | ||
|
||
import ( | ||
"math/big" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/formancehq/stack/tests/integration/internal/modules" | ||
|
||
"github.com/formancehq/formance-sdk-go/v2/pkg/models/operations" | ||
"github.com/formancehq/formance-sdk-go/v2/pkg/models/shared" | ||
"github.com/formancehq/stack/libs/go-libs/pointer" | ||
. "github.com/formancehq/stack/tests/integration/internal" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = WithModules([]*Module{modules.Search, modules.Ledger}, func() { | ||
BeforeEach(func() { | ||
createLedgerResponse, err := Client().Ledger.V2CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ | ||
Ledger: "default", | ||
}) | ||
Expect(err).To(BeNil()) | ||
Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) | ||
|
||
createLedgerResponse, err = Client().Ledger.V2CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ | ||
Ledger: "test", | ||
}) | ||
Expect(err).To(BeNil()) | ||
Expect(createLedgerResponse.StatusCode).To(Equal(http.StatusNoContent)) | ||
}) | ||
When("creating a transaction on a ledger", func() { | ||
var ( | ||
timestamp = time.Now().Add(-1 * time.Minute).Round(time.Second).UTC() | ||
timestamp2 = time.Now().Round(time.Second).UTC() | ||
) | ||
It("should fail", func() { | ||
// Create a transaction | ||
response, err := Client().Ledger.V2CreateTransaction( | ||
TestContext(), | ||
operations.V2CreateTransactionRequest{ | ||
IdempotencyKey: pointer.For("foo"), | ||
V2PostTransaction: shared.V2PostTransaction{ | ||
Metadata: map[string]string{}, | ||
Postings: []shared.V2Posting{ | ||
{ | ||
Amount: big.NewInt(100), | ||
Asset: "USD", | ||
Source: "world", | ||
Destination: "alice", | ||
}, | ||
}, | ||
Timestamp: ×tamp, | ||
Reference: pointer.For("foo"), | ||
}, | ||
Ledger: "default", | ||
}, | ||
) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(response.StatusCode).To(Equal(200)) | ||
|
||
response, err = Client().Ledger.V2CreateTransaction( | ||
TestContext(), | ||
operations.V2CreateTransactionRequest{ | ||
IdempotencyKey: pointer.For("foo"), | ||
V2PostTransaction: shared.V2PostTransaction{ | ||
Metadata: map[string]string{}, | ||
Postings: []shared.V2Posting{ | ||
{ | ||
Amount: big.NewInt(100), | ||
Asset: "USD", | ||
Source: "world", | ||
Destination: "alice", | ||
}, | ||
}, | ||
Timestamp: ×tamp2, | ||
Reference: pointer.For("foo2"), | ||
}, | ||
Ledger: "test", | ||
}, | ||
) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(response.StatusCode).To(Equal(200)) | ||
}) | ||
}) | ||
}) |