diff --git a/internal/account.go b/internal/account.go index 75d385b13..d67370210 100644 --- a/internal/account.go +++ b/internal/account.go @@ -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"` } diff --git a/internal/controller/ledger/controller_default.go b/internal/controller/ledger/controller_default.go index e3d43f356..438580a4e 100644 --- a/internal/controller/ledger/controller_default.go +++ b/internal/controller/ledger/controller_default.go @@ -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" @@ -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 } diff --git a/internal/storage/bucket/migrations/32-accounts-assign-date.sql b/internal/storage/bucket/migrations/32-accounts-assign-date.sql new file mode 100644 index 000000000..df0f93fe9 --- /dev/null +++ b/internal/storage/bucket/migrations/32-accounts-assign-date.sql @@ -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') +; \ No newline at end of file diff --git a/internal/storage/ledger/accounts.go b/internal/storage/ledger/accounts.go index 372bb9647..1c3b6c178 100644 --- a/internal/storage/ledger/accounts.go +++ b/internal/storage/ledger/accounts.go @@ -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"` @@ -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, }, }) } diff --git a/internal/storage/ledger/accounts_test.go b/internal/storage/ledger/accounts_test.go index 0bc45924a..4bf8369f0 100644 --- a/internal/storage/ledger/accounts_test.go +++ b/internal/storage/ledger/accounts_test.go @@ -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{ diff --git a/internal/storage/ledger/transactions.go b/internal/storage/ledger/transactions.go index b7046182a..05b76d434 100644 --- a/internal/storage/ledger/transactions.go +++ b/internal/storage/ledger/transactions.go @@ -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() { @@ -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