Skip to content

Commit

Permalink
feat: let database generate defaults dates for accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent 931b1b3 commit 036aa86
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 34 deletions.
6 changes: 3 additions & 3 deletions internal/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type Account struct {

Address string `json:"address" bun:"address"`
Metadata metadata.Metadata `json:"metadata" bun:"metadata,type:jsonb"`
FirstUsage time.Time `json:"-" bun:"first_usage"`
InsertionDate time.Time `json:"_" bun:"insertion_date"`
UpdatedAt time.Time `json:"-" bun:"updated_at"`
FirstUsage time.Time `json:"-" bun:"first_usage,nullzero"`
InsertionDate time.Time `json:"_" bun:"insertion_date,nullzero"`
UpdatedAt time.Time `json:"-" bun:"updated_at,nullzero"`
Volumes VolumesByAssets `json:"volumes,omitempty" bun:"pcv,scanonly"`
EffectiveVolumes VolumesByAssets `json:"effectiveVolumes,omitempty" bun:"pcev,scanonly"`
}
Expand Down
7 changes: 0 additions & 7 deletions internal/controller/ledger/controller_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"math/big"
"reflect"

"github.com/formancehq/go-libs/time"

"github.com/formancehq/go-libs/migrations"
"github.com/formancehq/ledger/internal/tracing"

Expand Down Expand Up @@ -349,15 +347,10 @@ func (ctrl *DefaultController) SaveTransactionMetadata(ctx context.Context, para
}

func (ctrl *DefaultController) SaveAccountMetadata(ctx context.Context, parameters Parameters[SaveAccountMetadata]) error {
// todo: let database generate date
now := time.Now()
_, err := forgeLog(ctx, ctrl.store, parameters, func(ctx context.Context, sqlTX TX, input SaveAccountMetadata) (*ledger.SavedMetadata, error) {
if _, err := sqlTX.UpsertAccount(ctx, &ledger.Account{
Address: input.Address,
Metadata: input.Metadata,
FirstUsage: now,
InsertionDate: now,
UpdatedAt: now,
}); err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alter table "{{.Bucket}}".accounts
alter column first_usage set default (now() at time zone 'utc'),
alter column insertion_date set default (now() at time zone 'utc'),
alter column updated_at set default (now() at time zone 'utc')
;
6 changes: 0 additions & 6 deletions internal/storage/ledger/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ func (s *Store) CountAccounts(ctx context.Context, q ledgercontroller.ListAccoun

func (s *Store) UpdateAccountsMetadata(ctx context.Context, m map[string]metadata.Metadata) error {
_, err := tracing.TraceWithLatency(ctx, "UpdateAccountsMetadata", tracing.NoResult(func(ctx context.Context) error {
// todo: let database generate date
now := time.Now()

type AccountWithLedger struct {
ledger.Account `bun:",extend"`
Ledger string `bun:"ledger,type:varchar"`
Expand All @@ -266,9 +263,6 @@ func (s *Store) UpdateAccountsMetadata(ctx context.Context, m map[string]metadat
Account: ledger.Account{
Address: account,
Metadata: accountMetadata,
InsertionDate: now,
UpdatedAt: now,
FirstUsage: now,
},
})
}
Expand Down
10 changes: 5 additions & 5 deletions internal/storage/ledger/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,19 +410,19 @@ func TestUpsertAccount(t *testing.T) {
store := newLedgerStore(t)
ctx := logging.TestingContext()

now := time.Now()

account := ledger.Account{
Address: "foo",
FirstUsage: now,
InsertionDate: now,
UpdatedAt: now,
}

// Initial insert
upserted, err := store.UpsertAccount(ctx, &account)
require.NoError(t, err)
require.True(t, upserted)
require.NotEmpty(t, account.FirstUsage)
require.NotEmpty(t, account.InsertionDate)
require.NotEmpty(t, account.UpdatedAt)

now := time.Now()

// Reset the account model
account = ledger.Account{
Expand Down
22 changes: 9 additions & 13 deletions internal/storage/ledger/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,15 @@ func (s *Store) selectTransactions(date *time.Time, expandVolumes, expandEffecti
}

func (s *Store) CommitTransaction(ctx context.Context, tx *ledger.Transaction) error {
if tx.InsertedAt.IsZero() {
tx.InsertedAt = time.Now()
postCommitVolumes, err := s.UpdateVolumes(ctx, tx.VolumeUpdates()...)
if err != nil {
return fmt.Errorf("failed to update balances: %w", err)
}
tx.PostCommitVolumes = postCommitVolumes.Copy()

err = s.InsertTransaction(ctx, tx)
if err != nil {
return fmt.Errorf("failed to insert transaction: %w", err)
}

for _, address := range tx.InvolvedAccounts() {
Expand All @@ -248,17 +255,6 @@ func (s *Store) CommitTransaction(ctx context.Context, tx *ledger.Transaction) e
}
}

postCommitVolumes, err := s.UpdateVolumes(ctx, tx.VolumeUpdates()...)
if err != nil {
return fmt.Errorf("failed to update balances: %w", err)
}
tx.PostCommitVolumes = postCommitVolumes.Copy()

err = s.InsertTransaction(ctx, tx)
if err != nil {
return fmt.Errorf("failed to insert transaction: %w", err)
}

if s.ledger.HasFeature(ledger.FeatureMovesHistory, "ON") {
moves := ledger.Moves{}
postings := tx.Postings
Expand Down

0 comments on commit 036aa86

Please sign in to comment.