Skip to content

Commit

Permalink
feat: move ledgers model
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent ecd8ea2 commit 6f27088
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 98 deletions.
105 changes: 54 additions & 51 deletions internal/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,63 @@ package ledger

import (
"fmt"
"github.com/formancehq/go-libs/time"
"github.com/uptrace/bun"
"regexp"
"slices"

"github.com/formancehq/go-libs/metadata"
"github.com/formancehq/go-libs/time"
)

type Ledger struct {
bun.BaseModel `bun:"_system.ledgers,alias:ledgers"`

Configuration
ID int `json:"id" bun:"id,type:int,scanonly"`
Name string `json:"name" bun:"name,type:varchar(255),pk"`
AddedAt time.Time `json:"addedAt" bun:"addedat,type:timestamp"`
}

func (l Ledger) HasFeature(feature, value string) bool {
if err := validateFeatureWithValue(feature, value); err != nil {
panic(err)
}

return l.Features[feature] == value
}

func New(name string, configuration Configuration) (*Ledger, error) {

if err := configuration.Validate(); err != nil {
return nil, err
}

if !ledgerNameFormat.MatchString(name) {
return nil, newErrInvalidLedgerName(name, fmt.Errorf("name must match format '%s'", ledgerNameFormat.String()))
}
if !bucketNameFormat.MatchString(configuration.Bucket) {
return nil, newErrInvalidBucketName(configuration.Bucket, fmt.Errorf("name must match format '%s'", bucketNameFormat.String()))
}

return &Ledger{
Configuration: configuration,
Name: name,
AddedAt: time.Now(),
}, nil
}

func NewWithDefaults(name string) (*Ledger, error) {
return New(name, NewDefaultConfiguration())
}

func MustNewWithDefault(name string) Ledger {
ledger, err := NewWithDefaults(name)
if err != nil {
panic(err)
}
return *ledger
}

const (
// todo: add feature to completely disable logs
FeatureMovesHistory = "MOVES_HISTORY"
Expand Down Expand Up @@ -81,9 +131,9 @@ func (f FeatureSet) With(feature, value string) FeatureSet {
}

type Configuration struct {
Bucket string `json:"bucket"`
Metadata metadata.Metadata `json:"metadata"`
Features map[string]string `bun:"features,type:jsonb" json:"features"`
Bucket string `json:"bucket" bun:"bucket,type:varchar(255)"`
Metadata metadata.Metadata `json:"metadata" bun:"metadata,type:jsonb"`
Features map[string]string `json:"features" bun:"features,type:jsonb"`
}

func (c *Configuration) SetDefaults() {
Expand Down Expand Up @@ -118,50 +168,3 @@ func NewDefaultConfiguration() Configuration {
Features: DefaultFeatures,
}
}

type Ledger struct {
Configuration
ID int `json:"id"`
Name string `json:"name"`
AddedAt time.Time `json:"addedAt"`
}

func (l Ledger) HasFeature(feature, value string) bool {
if err := validateFeatureWithValue(feature, value); err != nil {
panic(err)
}

return l.Features[feature] == value
}

func New(name string, configuration Configuration) (*Ledger, error) {

if err := configuration.Validate(); err != nil {
return nil, err
}

if !ledgerNameFormat.MatchString(name) {
return nil, newErrInvalidLedgerName(name, fmt.Errorf("name must match format '%s'", ledgerNameFormat.String()))
}
if !bucketNameFormat.MatchString(configuration.Bucket) {
return nil, newErrInvalidBucketName(configuration.Bucket, fmt.Errorf("name must match format '%s'", bucketNameFormat.String()))
}

return &Ledger{
Configuration: configuration,
Name: name,
AddedAt: time.Now(),
}, nil
}

func NewWithDefaults(name string) (*Ledger, error) {
return New(name, NewDefaultConfiguration())
}

func MustNewWithDefault(name string) Ledger {
ledger, err := New(name, NewDefaultConfiguration())
if err != nil {
panic(err)
}
return *ledger
}
56 changes: 9 additions & 47 deletions internal/storage/system/ledgers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,18 @@ import (

"github.com/formancehq/go-libs/metadata"
"github.com/formancehq/go-libs/platform/postgres"
"github.com/formancehq/go-libs/pointer"
"github.com/formancehq/go-libs/time"
ledger "github.com/formancehq/ledger/internal"

"github.com/formancehq/go-libs/bun/bunpaginate"

"github.com/uptrace/bun"
)

type Ledger struct {
bun.BaseModel `bun:"_system.ledgers,alias:ledgers"`

ID int `bun:"id,type:int,scanonly"`
Name string `bun:"name,type:varchar(255),pk"`
AddedAt time.Time `bun:"addedat,type:timestamp"`
Bucket string `bun:"bucket,type:varchar(255)"`
Metadata map[string]string `bun:"metadata,type:jsonb"`
Features map[string]string `bun:"features,type:jsonb"`
}

func (l Ledger) toCore() ledger.Ledger {
return ledger.Ledger{
Name: l.Name,
Configuration: ledger.Configuration{
Bucket: l.Bucket,
Metadata: l.Metadata,
Features: l.Features,
},
AddedAt: l.AddedAt,
ID: l.ID,
}
}

func (s *Store) ListLedgers(ctx context.Context, q ledgercontroller.ListLedgersQuery) (*bunpaginate.Cursor[ledger.Ledger], error) {
query := s.db.NewSelect().
Model(&Ledger{}).
Model(&ledger.Ledger{}).
Column("*").
Order("addedat asc")

cursor, err := bunpaginate.UsingOffset[ledgercontroller.PaginatedQueryOptions[struct{}], Ledger](
cursor, err := bunpaginate.UsingOffset[ledgercontroller.PaginatedQueryOptions[struct{}], ledger.Ledger](
ctx,
query,
bunpaginate.OffsetPaginatedQuery[ledgercontroller.PaginatedQueryOptions[struct{}]](q),
Expand All @@ -55,24 +27,16 @@ func (s *Store) ListLedgers(ctx context.Context, q ledgercontroller.ListLedgersQ
return nil, err
}

return bunpaginate.MapCursor(cursor, Ledger.toCore), nil
return cursor, nil
}

func (s *Store) CreateLedger(ctx context.Context, l *ledger.Ledger) (bool, error) {
if l.Metadata == nil {
l.Metadata = metadata.Metadata{}
}

mappedLedger := &Ledger{
BaseModel: bun.BaseModel{},
Name: l.Name,
AddedAt: l.AddedAt,
Bucket: l.Bucket,
Metadata: l.Metadata,
Features: l.Features,
}
ret, err := s.db.NewInsert().
Model(mappedLedger).
Model(l).
Ignore().
Returning("id").
Exec(ctx)
Expand All @@ -85,13 +49,11 @@ func (s *Store) CreateLedger(ctx context.Context, l *ledger.Ledger) (bool, error
return false, postgres.ResolveError(err)
}

l.ID = mappedLedger.ID

return affected > 0, nil
}

func (s *Store) GetLedger(ctx context.Context, name string) (*ledger.Ledger, error) {
ret := &Ledger{}
ret := &ledger.Ledger{}
if err := s.db.NewSelect().
Model(ret).
Column("*").
Expand All @@ -100,12 +62,12 @@ func (s *Store) GetLedger(ctx context.Context, name string) (*ledger.Ledger, err
return nil, postgres.ResolveError(err)
}

return pointer.For(ret.toCore()), nil
return ret, nil
}

func (s *Store) UpdateLedgerMetadata(ctx context.Context, name string, m metadata.Metadata) error {
_, err := s.db.NewUpdate().
Model(&Ledger{}).
Model(&ledger.Ledger{}).
Set("metadata = metadata || ?", m).
Where("name = ?", name).
Exec(ctx)
Expand All @@ -114,7 +76,7 @@ func (s *Store) UpdateLedgerMetadata(ctx context.Context, name string, m metadat

func (s *Store) UpdateLedgerState(ctx context.Context, name string, state string) error {
_, err := s.db.NewUpdate().
Model(&Ledger{}).
Model(&ledger.Ledger{}).
Set("state = ?", state).
Where("name = ?", name).
Exec(ctx)
Expand All @@ -123,7 +85,7 @@ func (s *Store) UpdateLedgerState(ctx context.Context, name string, state string

func (s *Store) DeleteLedgerMetadata(ctx context.Context, name string, key string) error {
_, err := s.db.NewUpdate().
Model(&Ledger{}).
Model(&ledger.Ledger{}).
Set("metadata = metadata - ?", key).
Where("name = ?", name).
Exec(ctx)
Expand Down

0 comments on commit 6f27088

Please sign in to comment.