Skip to content

Commit

Permalink
feat: Sync Stack repo 9f6219e
Browse files Browse the repository at this point in the history
  • Loading branch information
flemzord committed Oct 6, 2023
1 parent 2f7f95c commit 94aeb02
Show file tree
Hide file tree
Showing 67 changed files with 698 additions and 467 deletions.
12 changes: 4 additions & 8 deletions cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"io"

"github.com/formancehq/ledger/cmd/internal"
"github.com/formancehq/ledger/internal/api"
"github.com/formancehq/ledger/internal/engine"
driver2 "github.com/formancehq/ledger/internal/storage/driver"
driver "github.com/formancehq/ledger/internal/storage/driver"
"github.com/formancehq/stack/libs/go-libs/otlp/otlpmetrics"
"github.com/formancehq/stack/libs/go-libs/otlp/otlptraces"
"github.com/formancehq/stack/libs/go-libs/publish"
Expand All @@ -24,21 +23,18 @@ func resolveOptions(output io.Writer, userOptions ...fx.Option) []fx.Option {
v := viper.GetViper()
debug := v.GetBool(service.DebugFlag)
if debug {
driver2.InstrumentalizeSQLDriver()
driver.InstrumentalizeSQLDriver()
}

options = append(options,
publish.CLIPublisherModule(v, ServiceName),
otlptraces.CLITracesModule(v),
otlpmetrics.CLIMetricsModule(v),
api.Module(api.Config{
Version: Version,
}),
driver2.CLIModule(v, output, debug),
driver.CLIModule(v, output, debug),
internal.NewAnalyticsModule(v, Version),
engine.Module(engine.Configuration{
NumscriptCache: engine.NumscriptCacheConfiguration{
MaxCount: v.GetInt(numscriptCacheMaxCount),
MaxCount: v.GetInt(numscriptCacheMaxCountFlag),
},
}),
)
Expand Down
25 changes: 22 additions & 3 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import (
"net/http"
"time"

"github.com/formancehq/ledger/internal/storage/driver"

"github.com/formancehq/ledger/internal/api"

ledger "github.com/formancehq/ledger/internal"
"github.com/formancehq/stack/libs/go-libs/ballast"
"github.com/formancehq/stack/libs/go-libs/httpserver"
Expand All @@ -16,8 +20,10 @@ import (
)

const (
ballastSizeInBytesFlag = "ballast-size"
numscriptCacheMaxCount = "numscript-cache-max-count"
ballastSizeInBytesFlag = "ballast-size"
numscriptCacheMaxCountFlag = "numscript-cache-max-count"
readOnlyFlag = "read-only"
autoUpgradeFlag = "auto-upgrade"
)

func NewServe() *cobra.Command {
Expand All @@ -27,6 +33,17 @@ func NewServe() *cobra.Command {
return app.New(cmd.OutOrStdout(), resolveOptions(
cmd.OutOrStdout(),
ballast.Module(viper.GetUint(ballastSizeInBytesFlag)),
api.Module(api.Config{
Version: Version,
ReadOnly: viper.GetBool(readOnlyFlag),
}),
fx.Invoke(func(lc fx.Lifecycle, driver *driver.Driver) {
if viper.GetBool(autoUpgradeFlag) {
lc.Append(fx.Hook{
OnStart: driver.UpgradeAllLedgersSchemas,
})
}
}),
fx.Invoke(func(lc fx.Lifecycle, h chi.Router, logger logging.Logger) {

wrappedRouter := chi.NewRouter()
Expand All @@ -45,7 +62,9 @@ func NewServe() *cobra.Command {
},
}
cmd.Flags().Uint(ballastSizeInBytesFlag, 0, "Ballast size in bytes, default to 0")
cmd.Flags().Int(numscriptCacheMaxCount, 1024, "Numscript cache max count")
cmd.Flags().Int(numscriptCacheMaxCountFlag, 1024, "Numscript cache max count")
cmd.Flags().Bool(readOnlyFlag, false, "Read only mode")
cmd.Flags().Bool(autoUpgradeFlag, false, "Automatically upgrade all schemas")
return cmd
}

Expand Down
25 changes: 6 additions & 19 deletions cmd/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,31 +156,18 @@ func NewStorageUpgradeAll() *cobra.Command {
if err != nil {
return err
}
defer sqlDB.Close()
defer func() {
if err := sqlDB.Close(); err != nil {
logger.Errorf("Error closing database: %s", err)
}
}()

driver := driver.New(sqlDB)
if err := driver.Initialize(ctx); err != nil {
return err
}

systemStore := driver.GetSystemStore()
ledgers, err := systemStore.ListLedgers(ctx)
if err != nil {
return err
}

for _, ledger := range ledgers {
store, err := driver.GetLedgerStore(ctx, ledger)
if err != nil {
return err
}
logger.Infof("Upgrading storage '%s'", ledger)
if err := upgradeStore(ctx, store, ledger); err != nil {
return err
}
}

return nil
return driver.UpgradeAllLedgersSchemas(ctx)
},
}
return cmd
Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/formancehq/ledger

go 1.19
go 1.20

require (
github.com/Masterminds/semver/v3 v3.2.0
Expand All @@ -17,7 +17,6 @@ require (
github.com/jackc/pgx/v5 v5.3.0
github.com/lib/pq v1.10.7
github.com/logrusorgru/aurora v2.0.3+incompatible
github.com/ory/dockertest/v3 v3.9.1
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/pborman/uuid v1.2.1
github.com/pkg/errors v0.9.1
Expand All @@ -33,8 +32,8 @@ require (
go.opentelemetry.io/otel v1.16.0
go.opentelemetry.io/otel/metric v1.16.0
go.opentelemetry.io/otel/trace v1.16.0
go.uber.org/atomic v1.10.0
go.uber.org/fx v1.19.2
go.uber.org/mock v0.3.0
gopkg.in/segmentio/analytics-go.v3 v3.1.0
)

Expand Down Expand Up @@ -96,6 +95,7 @@ require (
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.3 // indirect
github.com/ory/dockertest/v3 v3.9.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down Expand Up @@ -144,11 +144,12 @@ require (
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.39.0 // indirect
go.opentelemetry.io/proto/otlp v0.19.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/dig v1.16.1 // indirect
go.uber.org/multierr v1.9.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/crypto v0.9.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,8 @@ go.uber.org/dig v1.16.1/go.mod h1:557JTAUZT5bUK0SvCwikmLPPtdQhfvLYtO5tJgQSbnk=
go.uber.org/fx v1.19.2 h1:SyFgYQFr1Wl0AYstE8vyYIzP4bFz2URrScjwC4cwUvY=
go.uber.org/fx v1.19.2/go.mod h1:43G1VcqSzbIv77y00p1DRAsyZS8WdzuYdhZXmEUkMyQ=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
Expand Down Expand Up @@ -603,8 +605,8 @@ golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
2 changes: 2 additions & 0 deletions internal/api/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type Ledger interface {
RevertTransaction(ctx context.Context, parameters command.Parameters, id *big.Int) (*ledger.Transaction, error)
SaveMeta(ctx context.Context, parameters command.Parameters, targetType string, targetID any, m metadata.Metadata) error
DeleteMetadata(ctx context.Context, parameters command.Parameters, targetType string, targetID any, key string) error

IsDatabaseUpToDate(ctx context.Context) (bool, error)
}

type Backend interface {
Expand Down
Loading

0 comments on commit 94aeb02

Please sign in to comment.