From 0e5358a46f13f1ca88e95b8ef5f68b18c7b162f7 Mon Sep 17 00:00:00 2001 From: Geoffrey Ragot Date: Thu, 10 Oct 2024 10:43:38 +0200 Subject: [PATCH] feat: add logs memento --- internal/log.go | 16 +++++++-------- .../bucket/migrations/29-logs-add-memento.sql | 2 ++ internal/storage/ledger/logs.go | 20 +++++++++++++++---- 3 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 internal/storage/bucket/migrations/29-logs-add-memento.sql diff --git a/internal/log.go b/internal/log.go index 4c97eaac7..15889223a 100644 --- a/internal/log.go +++ b/internal/log.go @@ -142,8 +142,8 @@ func (l *Log) ComputeHash(previous *Log) { } payload := l.Data.(any) - if hv, ok := payload.(hashValuer); ok { - payload = hv.hashValue() + if hv, ok := payload.(Memento); ok { + payload = hv.GetMemento() } if err := enc.Encode(struct { @@ -181,8 +181,8 @@ type LogPayload interface { Type() LogType } -type hashValuer interface { - hashValue() any +type Memento interface { + GetMemento() any } type AccountMetadata map[string]metadata.Metadata @@ -198,7 +198,7 @@ func (p CreatedTransaction) Type() LogType { var _ LogPayload = (*CreatedTransaction)(nil) -func (p CreatedTransaction) hashValue() any { +func (p CreatedTransaction) GetMemento() any { // Exclude postCommitVolumes and postCommitEffectiveVolumes fields from transactions. // We don't want those fields to be part of the hash as they are not part of the decision-making process. return struct { @@ -210,7 +210,7 @@ func (p CreatedTransaction) hashValue() any { } } -var _ hashValuer = (*CreatedTransaction)(nil) +var _ Memento = (*CreatedTransaction)(nil) type SavedMetadata struct { TargetType string `json:"targetType"` @@ -315,7 +315,7 @@ func (r RevertedTransaction) Type() LogType { var _ LogPayload = (*RevertedTransaction)(nil) -func (r RevertedTransaction) hashValue() any { +func (r RevertedTransaction) GetMemento() any { return struct { RevertedTransactionID int `json:"revertedTransactionID"` RevertTransaction Transaction `json:"transaction"` @@ -325,7 +325,7 @@ func (r RevertedTransaction) hashValue() any { } } -var _ hashValuer = (*RevertedTransaction)(nil) +var _ Memento = (*RevertedTransaction)(nil) func HydrateLog(_type LogType, data []byte) (LogPayload, error) { var payload any diff --git a/internal/storage/bucket/migrations/29-logs-add-memento.sql b/internal/storage/bucket/migrations/29-logs-add-memento.sql new file mode 100644 index 000000000..1d3f1df41 --- /dev/null +++ b/internal/storage/bucket/migrations/29-logs-add-memento.sql @@ -0,0 +1,2 @@ +alter table "{{.Bucket}}".logs +add column memento bytea; \ No newline at end of file diff --git a/internal/storage/ledger/logs.go b/internal/storage/ledger/logs.go index 2f7e32139..454d3ec06 100644 --- a/internal/storage/ledger/logs.go +++ b/internal/storage/ledger/logs.go @@ -24,6 +24,7 @@ type Log struct { Ledger string `bun:"ledger,type:varchar"` Data RawMessage `bun:"data,type:jsonb"` + Memento RawMessage `bun:"memento,type:bytea"` } func (log Log) toCore() ledger.Log { @@ -72,17 +73,28 @@ func (s *Store) InsertLog(ctx context.Context, log *ledger.Log) error { } _, err := tracing.TraceWithLatency(ctx, "InsertLog", tracing.NoResult(func(ctx context.Context) error { - data, err := json.Marshal(log.Data) + payloadData, err := json.Marshal(log.Data) if err != nil { return fmt.Errorf("failed to marshal log data: %w", err) } + mementoObject := log.Data.(any) + if memento, ok := mementoObject.(ledger.Memento); ok { + mementoObject = memento + } + + mementoData, err := json.Marshal(mementoObject) + if err != nil { + return err + } + _, err = s.db. NewInsert(). Model(&Log{ - Log: log, - Ledger: s.ledger.Name, - Data: data, + Log: log, + Ledger: s.ledger.Name, + Data: payloadData, + Memento: mementoData, }). ModelTableExpr(s.GetPrefixedRelationName("logs")). Value("id", "nextval(?)", s.GetPrefixedRelationName(fmt.Sprintf(`"log_id_%d"`, s.ledger.ID))).