Skip to content

Commit

Permalink
feat: move logs model
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent bba1a19 commit ecd8ea2
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 68 deletions.
61 changes: 36 additions & 25 deletions internal/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package ledger

import (
"crypto/sha256"
"database/sql/driver"
"encoding/json"
"fmt"
"github.com/uptrace/bun"
"reflect"
"strconv"
"strings"
Expand All @@ -14,15 +16,39 @@ import (
"github.com/pkg/errors"
)

type LogType int16

const (
SetMetadataLogType LogType = iota // "SET_METADATA"
NewTransactionLogType // "NEW_TRANSACTION"
RevertedTransactionLogType // "REVERTED_TRANSACTION"
DeleteMetadataLogType
)

type LogType int16

func (lt LogType) Value() (driver.Value, error) {
return lt.String(), nil
}

func (lt *LogType) Scan(src interface{}) error {
*lt = LogTypeFromString(string(src.([]byte)))
return nil
}

func (lt LogType) MarshalJSON() ([]byte, error) {
return json.Marshal(lt.String())
}

func (lt *LogType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}

*lt = LogTypeFromString(s)

return nil
}

func (l LogType) String() string {
switch l {
case SetMetadataLogType:
Expand Down Expand Up @@ -53,32 +79,17 @@ func LogTypeFromString(logType string) LogType {
panic(errors.New("invalid log type"))
}

// Needed in order to keep the compatibility with the openapi response for
// ListLogs.
func (lt LogType) MarshalJSON() ([]byte, error) {
return json.Marshal(lt.String())
}

func (lt *LogType) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}

*lt = LogTypeFromString(s)

return nil
}

// Log represents atomic actions made on the ledger.
// notes(gfyrag): Keep keys ordered! the order matter when hashing the log.
type Log struct {
Type LogType `json:"type"`
Data any `json:"data"`
Date time.Time `json:"date"`
IdempotencyKey string `json:"idempotencyKey"`
ID int `json:"id"`
Hash []byte `json:"hash"`
bun.BaseModel `bun:"table:logs,alias:logs"`

Type LogType `json:"type" bun:"type,type:log_type"`
Data any `json:"data" bun:"data,type:jsonb"`
Date time.Time `json:"date" bun:"date,type:timestamptz"`
IdempotencyKey string `json:"idempotencyKey" bun:"idempotency_key,type:varchar(256),unique,nullzero"`
ID int `json:"id" bun:"id,unique,type:numeric"`
Hash []byte `json:"hash" bun:"hash,type:bytea,scanonly"`
}

func (l Log) WithIdempotencyKey(key string) Log {
Expand Down
57 changes: 14 additions & 43 deletions internal/storage/ledger/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,45 +12,28 @@ import (
"github.com/formancehq/go-libs/platform/postgres"
"github.com/formancehq/go-libs/pointer"
"github.com/formancehq/go-libs/query"
"github.com/formancehq/go-libs/time"
ledger "github.com/formancehq/ledger/internal"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
"github.com/pkg/errors"
"github.com/uptrace/bun"
)

// Log override ledger.Log to be able to properly read/write payload which is jsonb
// on the database and 'any' on the Log structure (data column)
type Log struct {
bun.BaseModel `bun:"table:logs,alias:logs"`

Ledger string `bun:"ledger,type:varchar"`
ID int `bun:"id,unique,type:numeric"`
Type string `bun:"type,type:log_type"`
Hash []byte `bun:"hash,type:bytea,scanonly"`
Date time.Time `bun:"date,type:timestamptz"`
Data RawMessage `bun:"data,type:jsonb"`
IdempotencyKey *string `bun:"idempotency_key,type:varchar(256),unique"`
*ledger.Log `bun:",extend"`

Ledger string `bun:"ledger,type:varchar"`
Data RawMessage `bun:"data,type:jsonb"`
}

func (log Log) toCore() ledger.Log {

payload, err := ledger.HydrateLog(ledger.LogTypeFromString(log.Type), log.Data)
payload, err := ledger.HydrateLog(log.Type, log.Data)
if err != nil {
panic(errors.Wrap(err, "hydrating log data"))
}
log.Log.Data = payload

return ledger.Log{
Type: ledger.LogTypeFromString(log.Type),
Data: payload,
IdempotencyKey: func() string {
if log.IdempotencyKey != nil {
return *log.IdempotencyKey
}
return ""
}(),
Date: log.Date.UTC(),
ID: log.ID,
Hash: log.Hash,
}
return *log.Log
}

type RawMessage json.RawMessage
Expand Down Expand Up @@ -79,22 +62,13 @@ func (s *Store) InsertLog(ctx context.Context, log *ledger.Log) error {
return errors.Wrap(err, "failed to marshal log data")
}

newLog := &Log{
Ledger: s.ledger.Name,
Type: log.Type.String(),
Data: data,
Date: log.Date,
IdempotencyKey: func() *string {
if log.IdempotencyKey == "" {
return nil
}
return &log.IdempotencyKey
}(),
}

_, err = s.db.
NewInsert().
Model(newLog).
Model(&Log{
Log: log,
Ledger: s.ledger.Name,
Data: data,
}).
ModelTableExpr(s.GetPrefixedRelationName("logs")).
Value("id", "nextval(?)", s.GetPrefixedRelationName(fmt.Sprintf(`"log_id_%d"`, s.ledger.ID))).
Returning("*").
Expand All @@ -111,9 +85,6 @@ func (s *Store) InsertLog(ctx context.Context, log *ledger.Log) error {
}
}

log.ID = newLog.ID
log.Hash = newLog.Hash

return nil
}))

Expand Down

0 comments on commit ecd8ea2

Please sign in to comment.