From a40f7e34c32bf6be295c974e84c9ec76b175811e Mon Sep 17 00:00:00 2001 From: Geoffrey Ragot Date: Wed, 9 Oct 2024 10:55:37 +0200 Subject: [PATCH] feat: let database generate all timestamps --- internal/controller/ledger/controller_default.go | 10 ++-------- internal/ledger.go | 1 + internal/log.go | 1 + .../13-set-transaction-timestamp-default-utc.sql | 5 ++++- internal/storage/ledger/accounts.go | 1 + internal/transaction.go | 1 - 6 files changed, 9 insertions(+), 10 deletions(-) diff --git a/internal/controller/ledger/controller_default.go b/internal/controller/ledger/controller_default.go index 6794a323e..e3d43f356 100644 --- a/internal/controller/ledger/controller_default.go +++ b/internal/controller/ledger/controller_default.go @@ -240,17 +240,10 @@ func (ctrl *DefaultController) CreateTransaction(ctx context.Context, parameters finalMetadata[k] = v } - now := time.Now() - ts := input.Timestamp - if ts.IsZero() { - ts = now - } - transaction := ledger.NewTransaction(). WithPostings(result.Postings...). WithMetadata(finalMetadata). - WithTimestamp(ts). - WithInsertedAt(now). + WithTimestamp(input.Timestamp). WithReference(input.Reference) err = sqlTX.CommitTransaction(ctx, &transaction) if err != nil { @@ -356,6 +349,7 @@ 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{ diff --git a/internal/ledger.go b/internal/ledger.go index bd2f14847..aeff6b663 100644 --- a/internal/ledger.go +++ b/internal/ledger.go @@ -50,6 +50,7 @@ func New(name string, configuration Configuration) (*Ledger, error) { return &Ledger{ Configuration: configuration, Name: name, + // todo: get from database AddedAt: time.Now(), }, nil } diff --git a/internal/log.go b/internal/log.go index 470e964c8..4c97eaac7 100644 --- a/internal/log.go +++ b/internal/log.go @@ -172,6 +172,7 @@ func NewLog(payload LogPayload) Log { return Log{ Type: payload.Type(), Data: payload, + // todo: get from database Date: time.Now(), } } diff --git a/internal/storage/bucket/migrations/13-set-transaction-timestamp-default-utc.sql b/internal/storage/bucket/migrations/13-set-transaction-timestamp-default-utc.sql index c30c79a30..f955ef437 100644 --- a/internal/storage/bucket/migrations/13-set-transaction-timestamp-default-utc.sql +++ b/internal/storage/bucket/migrations/13-set-transaction-timestamp-default-utc.sql @@ -1,3 +1,6 @@ alter table "{{.Bucket}}".transactions add column inserted_at timestamp without time zone -default (now() at time zone 'utc'); \ No newline at end of file +default (now() at time zone 'utc'); + +alter table "{{.Bucket}}".transactions +alter column timestamp 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 18f0c8e5c..372bb9647 100644 --- a/internal/storage/ledger/accounts.go +++ b/internal/storage/ledger/accounts.go @@ -251,6 +251,7 @@ 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 { diff --git a/internal/transaction.go b/internal/transaction.go index d6acf16fc..103381281 100644 --- a/internal/transaction.go +++ b/internal/transaction.go @@ -32,7 +32,6 @@ func (data TransactionData) WithPostings(postings ...Posting) TransactionData { func NewTransactionData() TransactionData { return TransactionData{ Metadata: metadata.Metadata{}, - Timestamp: time.Now(), } }