Skip to content

Commit

Permalink
feat: let database generate logs dates
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent fe85231 commit 931b1b3
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 3 deletions.
4 changes: 1 addition & 3 deletions internal/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ type Log struct {

Type LogType `json:"type" bun:"type,type:log_type"`
Data LogPayload `json:"data" bun:"data,type:jsonb"`
Date time.Time `json:"date" bun:"date,type:timestamptz"`
Date time.Time `json:"date" bun:"date,type:timestamptz,nullzero"`
IdempotencyKey string `json:"idempotencyKey" bun:"idempotency_key,type:varchar(256),unique,nullzero"`
// IdempotencyHash is a signature used when using IdempotencyKey.
// It allows to check if the usage of IdempotencyKey match inputs given on the first idempotency key usage.
Expand Down Expand Up @@ -172,8 +172,6 @@ func NewLog(payload LogPayload) Log {
return Log{
Type: payload.Type(),
Data: payload,
// todo: get from database
Date: time.Now(),
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/storage/bucket/migrations/29-logs-add-memento.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
--todo: add migration
alter table "{{.Bucket}}".logs
add column memento bytea;
2 changes: 2 additions & 0 deletions internal/storage/bucket/migrations/31-logs-assign-date.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table "{{.Bucket}}".logs
alter column date set default (now() at time zone 'utc');
4 changes: 4 additions & 0 deletions internal/storage/ledger/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ func TestInsertLog(t *testing.T) {

require.Equal(t, 1, log1.ID)
require.NotZero(t, log1.Hash)
require.NotEmpty(t, log1.Date)

// Ensure than the database hashing is the same as the go hashing
log1Copy.Date = log1.Date
chainedLog1 := log1Copy.ChainLog(nil)
require.Equal(t, chainedLog1.Hash, log1.Hash)

Expand All @@ -59,8 +61,10 @@ func TestInsertLog(t *testing.T) {
require.NoError(t, err)
require.Equal(t, 2, log2.ID)
require.NotZero(t, log2.Hash)
require.NotZero(t, log2.Date)

// Ensure than the database hashing is the same as the go hashing
log2Copy.Date = log2.Date
chainedLog2 := log2Copy.ChainLog(&log1)
require.Equal(t, chainedLog2.Hash, log2.Hash)
})
Expand Down

0 comments on commit 931b1b3

Please sign in to comment.