Skip to content

Commit

Permalink
fix(imports): completely reproduce ids (with holes) and dates
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Nov 20, 2024
1 parent 5c29e33 commit 24c7aba
Show file tree
Hide file tree
Showing 20 changed files with 133 additions and 51 deletions.
2 changes: 1 addition & 1 deletion Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ generate:
RUN apk update && apk add openjdk11
RUN go install go.uber.org/mock/[email protected]
RUN go install github.com/princjef/gomarkdoc/cmd/gomarkdoc@latest
COPY --dir (+lint/*) /src/
COPY (+tidy/*) /src/
COPY --dir (+sources/src/*) /src/
WORKDIR /src
RUN go generate ./...
SAVE ARTIFACT internal AS LOCAL internal
Expand Down
1 change: 1 addition & 0 deletions docs/api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2748,6 +2748,7 @@ Authorization ( Scopes: ledger:write )
|type|NEW_TRANSACTION|
|type|SET_METADATA|
|type|REVERTED_TRANSACTION|
|type|DELETE_METADATA|

<h2 id="tocS_V2CreateTransactionResponse">V2CreateTransactionResponse</h2>
<!-- backwards compatibility -->
Expand Down
12 changes: 10 additions & 2 deletions internal/controller/ledger/controller_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"database/sql"
"fmt"
"github.com/formancehq/go-libs/v2/time"
"github.com/formancehq/ledger/pkg/features"
"math/big"
"reflect"
Expand Down Expand Up @@ -176,10 +177,17 @@ func (ctrl *DefaultController) importLog(ctx context.Context, sqlTx TX, log ledg
}
}
case ledger.RevertedTransaction:
_, _, err := sqlTx.RevertTransaction(ctx, payload.RevertedTransaction.ID)
_, _, err := sqlTx.RevertTransaction(
ctx,
payload.RevertedTransaction.ID,
*payload.RevertedTransaction.RevertedAt,
)
if err != nil {
return fmt.Errorf("failed to revert transaction: %w", err)
}
if err := sqlTx.CommitTransaction(ctx, &payload.RevertTransaction); err != nil {
return fmt.Errorf("failed to commit transaction: %w", err)
}
case ledger.SavedMetadata:
switch payload.TargetType {
case ledger.MetaTargetTypeTransaction:
Expand Down Expand Up @@ -316,7 +324,7 @@ func (ctrl *DefaultController) revertTransaction(ctx context.Context, sqlTX TX,
hasBeenReverted bool
err error
)
originalTransaction, hasBeenReverted, err := sqlTX.RevertTransaction(ctx, parameters.Input.TransactionID)
originalTransaction, hasBeenReverted, err := sqlTX.RevertTransaction(ctx, parameters.Input.TransactionID, time.Time{})
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/ledger/controller_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ func TestRevertTransaction(t *testing.T) {

txToRevert := ledger.Transaction{}
sqlTX.EXPECT().
RevertTransaction(gomock.Any(), 1).
DoAndReturn(func(_ context.Context, _ int) (*ledger.Transaction, bool, error) {
RevertTransaction(gomock.Any(), 1, time.Time{}).
DoAndReturn(func(_ context.Context, _ int, _ time.Time) (*ledger.Transaction, bool, error) {
txToRevert.RevertedAt = pointer.For(time.Now())
return &txToRevert, true, nil
})
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/ledger/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type TX interface {
// * the reverted transaction
// * a boolean indicating if the transaction has been reverted. false indicates an already reverted transaction (unless error != nil)
// * an error
RevertTransaction(ctx context.Context, id int) (*ledger.Transaction, bool, error)
RevertTransaction(ctx context.Context, id int, at time.Time) (*ledger.Transaction, bool, error)
UpdateTransactionMetadata(ctx context.Context, transactionID int, m metadata.Metadata) (*ledger.Transaction, bool, error)
DeleteTransactionMetadata(ctx context.Context, transactionID int, key string) (*ledger.Transaction, bool, error)
UpdateAccountsMetadata(ctx context.Context, m map[string]metadata.Metadata) error
Expand Down
9 changes: 5 additions & 4 deletions internal/controller/ledger/store_generated_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions internal/storage/ledger/legacy/adapters.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/formancehq/go-libs/v2/bun/bunpaginate"
"github.com/formancehq/go-libs/v2/metadata"
"github.com/formancehq/go-libs/v2/migrations"
"github.com/formancehq/go-libs/v2/time"
ledger "github.com/formancehq/ledger/internal"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
ledgerstore "github.com/formancehq/ledger/internal/storage/ledger"
Expand All @@ -30,8 +31,8 @@ func (tx TX) CommitTransaction(ctx context.Context, transaction *ledger.Transact
return tx.newStore.CommitTransaction(ctx, transaction)
}

func (tx TX) RevertTransaction(ctx context.Context, id int) (*ledger.Transaction, bool, error) {
return tx.newStore.RevertTransaction(ctx, id)
func (tx TX) RevertTransaction(ctx context.Context, id int, at time.Time) (*ledger.Transaction, bool, error) {
return tx.newStore.RevertTransaction(ctx, id, at)
}

func (tx TX) UpdateTransactionMetadata(ctx context.Context, transactionID int, m metadata.Metadata) (*ledger.Transaction, bool, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/ledger/legacy/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func TestGetTransactions(t *testing.T) {
err = store.newStore.CommitTransaction(ctx, &tx3BeforeRevert)
require.NoError(t, err)

_, hasBeenReverted, err := store.newStore.RevertTransaction(ctx, tx3BeforeRevert.ID)
_, hasBeenReverted, err := store.newStore.RevertTransaction(ctx, tx3BeforeRevert.ID, time.Time{})
require.NoError(t, err)
require.True(t, hasBeenReverted)

Expand Down
12 changes: 8 additions & 4 deletions internal/storage/ledger/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *Store) InsertLog(ctx context.Context, log *ledger.Log) error {
return err
}

_, err = s.db.
query := s.db.
NewInsert().
Model(&Log{
Log: log,
Expand All @@ -87,9 +87,13 @@ func (s *Store) InsertLog(ctx context.Context, log *ledger.Log) error {
Memento: mementoData,
}).
ModelTableExpr(s.GetPrefixedRelationName("logs")).
Value("id", "nextval(?)", s.GetPrefixedRelationName(fmt.Sprintf(`"log_id_%d"`, s.ledger.ID))).
Returning("*").
Exec(ctx)
Returning("*")

if log.ID == 0 {
query = query.Value("id", "nextval(?)", s.GetPrefixedRelationName(fmt.Sprintf(`"log_id_%d"`, s.ledger.ID)))
}

_, err = query.Exec(ctx)
if err != nil {
err := postgres.ResolveError(err)
switch {
Expand Down
43 changes: 26 additions & 17 deletions internal/storage/ledger/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,17 @@ func (s *Store) InsertTransaction(ctx context.Context, tx *ledger.Transaction) e
s.tracer,
s.insertTransactionHistogram,
func(ctx context.Context) (*ledger.Transaction, error) {
_, err := s.db.NewInsert().
query := s.db.NewInsert().
Model(tx).
ModelTableExpr(s.GetPrefixedRelationName("transactions")).
Value("id", "nextval(?)", s.GetPrefixedRelationName(fmt.Sprintf(`"transaction_id_%d"`, s.ledger.ID))).
Value("ledger", "?", s.ledger.Name).
Returning("id, timestamp, inserted_at").
Exec(ctx)
Returning("id, timestamp, inserted_at")

if tx.ID == 0 {
query = query.Value("id", "nextval(?)", s.GetPrefixedRelationName(fmt.Sprintf(`"transaction_id_%d"`, s.ledger.ID)))
}

_, err := query.Exec(ctx)
if err != nil {
err = postgres.ResolveError(err)
switch {
Expand Down Expand Up @@ -460,26 +464,31 @@ func (s *Store) updateTxWithRetrieve(ctx context.Context, id int, query *bun.Upd
return &me.Transaction, me.Modified, nil
}

func (s *Store) RevertTransaction(ctx context.Context, id int) (tx *ledger.Transaction, modified bool, err error) {
func (s *Store) RevertTransaction(ctx context.Context, id int, at time.Time) (tx *ledger.Transaction, modified bool, err error) {
_, err = tracing.TraceWithMetric(
ctx,
"RevertTransaction",
s.tracer,
s.revertTransactionHistogram,
func(ctx context.Context) (*ledger.Transaction, error) {
tx, modified, err = s.updateTxWithRetrieve(
ctx,
id,
s.db.NewUpdate().
Model(&ledger.Transaction{}).
ModelTableExpr(s.GetPrefixedRelationName("transactions")).
Where("id = ?", id).
Where("reverted_at is null").
Where("ledger = ?", s.ledger.Name).
query := s.db.NewUpdate().
Model(&ledger.Transaction{}).
ModelTableExpr(s.GetPrefixedRelationName("transactions")).
Where("id = ?", id).
Where("reverted_at is null").
Where("ledger = ?", s.ledger.Name).
Returning("*")
if at.IsZero() {
query = query.
Set("reverted_at = (now() at time zone 'utc')").
Set("updated_at = (now() at time zone 'utc')").
Returning("*"),
)
Set("updated_at = (now() at time zone 'utc')")
} else {
query = query.
Set("reverted_at = ?", at).
Set("updated_at = ?", at)
}

tx, modified, err = s.updateTxWithRetrieve(ctx, id, query)
return nil, err
},
)
Expand Down
8 changes: 4 additions & 4 deletions internal/storage/ledger/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ func TestTransactionsRevert(t *testing.T) {
require.NoError(t, err)

// Revert the tx
revertedTx, reverted, err := store.RevertTransaction(ctx, tx1.ID)
revertedTx, reverted, err := store.RevertTransaction(ctx, tx1.ID, time.Time{})
require.NoError(t, err)
require.True(t, reverted)
require.NotNil(t, revertedTx)
Expand All @@ -556,12 +556,12 @@ func TestTransactionsRevert(t *testing.T) {
require.Equal(t, tx1, *revertedTx)

// Try to revert again
_, reverted, err = store.RevertTransaction(ctx, tx1.ID)
_, reverted, err = store.RevertTransaction(ctx, tx1.ID, time.Time{})
require.NoError(t, err)
require.False(t, reverted)

// Revert a not existing transaction
revertedTx, reverted, err = store.RevertTransaction(ctx, 2)
revertedTx, reverted, err = store.RevertTransaction(ctx, 2, time.Time{})
require.True(t, errors.Is(err, postgres.ErrNotFound))
require.False(t, reverted)
require.Nil(t, revertedTx)
Expand Down Expand Up @@ -765,7 +765,7 @@ func TestTransactionsList(t *testing.T) {
err = store.CommitTransaction(ctx, &tx3BeforeRevert)
require.NoError(t, err)

_, hasBeenReverted, err := store.RevertTransaction(ctx, tx3BeforeRevert.ID)
_, hasBeenReverted, err := store.RevertTransaction(ctx, tx3BeforeRevert.ID, time.Time{})
require.NoError(t, err)
require.True(t, hasBeenReverted)

Expand Down
1 change: 1 addition & 0 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3391,6 +3391,7 @@ components:
- NEW_TRANSACTION
- SET_METADATA
- REVERTED_TRANSACTION
- DELETE_METADATA
data:
type: object
properties: {}
Expand Down
1 change: 1 addition & 0 deletions openapi/v2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1660,6 +1660,7 @@ components:
- NEW_TRANSACTION
- SET_METADATA
- REVERTED_TRANSACTION
- DELETE_METADATA
data:
type: object
properties: {}
Expand Down
6 changes: 3 additions & 3 deletions pkg/client/.speakeasy/gen.lock
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
lockVersion: 2.0.0
id: a9ac79e1-e429-4ee3-96c4-ec973f19bec3
management:
docChecksum: 743c41071ff98ac5e7d00e58f65650e3
docChecksum: 25c171859e282a2487f6d9f9d3888d8a
docVersion: v1
speakeasyVersion: 1.351.0
generationVersion: 2.384.1
releaseVersion: 0.4.31
configChecksum: 006527a5b70ea037906b0ff29a01a2ef
releaseVersion: 0.4.32
configChecksum: 38c4107cc4da2c542bfe11bf874323aa
features:
go:
additionalDependencies: 0.1.0
Expand Down
2 changes: 1 addition & 1 deletion pkg/client/.speakeasy/gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ generation:
auth:
oAuth2ClientCredentialsEnabled: true
go:
version: 0.4.31
version: 0.4.32
additionalDependencies: {}
allowUnknownFieldsInWeakUnions: false
clientServerStatusCodesAsErrors: true
Expand Down
3 changes: 2 additions & 1 deletion pkg/client/docs/models/components/v2logtype.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
| ------------------------------ | ------------------------------ |
| `V2LogTypeNewTransaction` | NEW_TRANSACTION |
| `V2LogTypeSetMetadata` | SET_METADATA |
| `V2LogTypeRevertedTransaction` | REVERTED_TRANSACTION |
| `V2LogTypeRevertedTransaction` | REVERTED_TRANSACTION |
| `V2LogTypeDeleteMetadata` | DELETE_METADATA |
4 changes: 2 additions & 2 deletions pkg/client/formance.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ func New(opts ...SDKOption) *Formance {
sdkConfiguration: sdkConfiguration{
Language: "go",
OpenAPIDocVersion: "v1",
SDKVersion: "0.4.31",
SDKVersion: "0.4.32",
GenVersion: "2.384.1",
UserAgent: "speakeasy-sdk/go 0.4.31 2.384.1 v1 github.com/formancehq/ledger/pkg/client",
UserAgent: "speakeasy-sdk/go 0.4.32 2.384.1 v1 github.com/formancehq/ledger/pkg/client",
Hooks: hooks.New(),
},
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/client/models/components/v2log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
V2LogTypeNewTransaction V2LogType = "NEW_TRANSACTION"
V2LogTypeSetMetadata V2LogType = "SET_METADATA"
V2LogTypeRevertedTransaction V2LogType = "REVERTED_TRANSACTION"
V2LogTypeDeleteMetadata V2LogType = "DELETE_METADATA"
)

func (e V2LogType) ToPointer() *V2LogType {
Expand All @@ -32,6 +33,8 @@ func (e *V2LogType) UnmarshalJSON(data []byte) error {
case "SET_METADATA":
fallthrough
case "REVERTED_TRANSACTION":
fallthrough
case "DELETE_METADATA":
*e = V2LogType(v)
return nil
default:
Expand Down
Loading

0 comments on commit 24c7aba

Please sign in to comment.