Skip to content

Commit

Permalink
feat: let database generate all timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 23, 2024
1 parent 150aef9 commit a40f7e3
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 10 deletions.
10 changes: 2 additions & 8 deletions internal/controller/ledger/controller_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
1 change: 1 addition & 0 deletions internal/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions internal/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func NewLog(payload LogPayload) Log {
return Log{
Type: payload.Type(),
Data: payload,
// todo: get from database
Date: time.Now(),
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
alter table "{{.Bucket}}".transactions
add column inserted_at timestamp without time zone
default (now() at time zone 'utc');
default (now() at time zone 'utc');

alter table "{{.Bucket}}".transactions
alter column timestamp set default (now() at time zone 'utc');
1 change: 1 addition & 0 deletions internal/storage/ledger/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion internal/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func (data TransactionData) WithPostings(postings ...Posting) TransactionData {
func NewTransactionData() TransactionData {
return TransactionData{
Metadata: metadata.Metadata{},
Timestamp: time.Now(),
}
}

Expand Down

0 comments on commit a40f7e3

Please sign in to comment.