Skip to content

Commit

Permalink
chore: clean some code
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 23, 2024
1 parent 67f4e9f commit 13ddb5d
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 127 deletions.
10 changes: 5 additions & 5 deletions Earthfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sources:
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY --dir internal pkg cmd .
COPY --dir internal pkg cmd tools .
COPY main.go .
SAVE ARTIFACT /src

Expand Down Expand Up @@ -86,9 +86,9 @@ tests:
RUN ginkgo -r -p $goFlags
END
IF [ "$coverage" = "true" ]
# exclude files suffixed with _generated.go, these are mocks used by tests
# toremovelater: also exclude machine code as it will be updated soon
RUN cat cover.out | grep -v "_generated.go" | grep -v "/machine/" > cover2.out
# as special case, exclude files suffixed by debug.go
# toremovelater: exclude machine code as it will be updated soon
RUN cat cover.out | grep -v debug.go | grep -v "/machine/" > cover2.out
RUN mv cover2.out cover.out
SAVE ARTIFACT cover.out AS LOCAL cover.out
END
Expand Down Expand Up @@ -208,5 +208,5 @@ export-database-schema:

export-docs-events:
FROM +tidy
RUN go run main.go doc events --write-dir docs/events
RUN go run tools/docs/events/main.go --write-dir docs/events
SAVE ARTIFACT docs/events AS LOCAL docs/events
16 changes: 0 additions & 16 deletions cmd/doc.go

This file was deleted.

20 changes: 0 additions & 20 deletions cmd/doc_events_test.go

This file was deleted.

18 changes: 0 additions & 18 deletions cmd/doc_flags_test.go

This file was deleted.

2 changes: 0 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ func NewRootCommand() *cobra.Command {
return upgradeAll(cmd)
}))

root.AddCommand(NewDocCommand())

return root
}

Expand Down
8 changes: 0 additions & 8 deletions internal/posting.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,6 @@ type Posting struct {
Asset string `json:"asset"`
}

func (p Posting) GetSource() string {
return p.Source
}

func (p Posting) GetDestination() string {
return p.Destination
}

func NewPosting(source string, destination string, asset string, amount *big.Int) Posting {
return Posting{
Source: source,
Expand Down
46 changes: 0 additions & 46 deletions internal/storage/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,43 +29,6 @@ type Driver struct {
db *bun.DB
}

func (d *Driver) CreateBucket(ctx context.Context, bucketName string) (*bucket.Bucket, error) {
tx, err := d.db.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
return nil, err
}
defer func() {
_ = tx.Rollback()
}()

b := bucket.New(d.db, bucketName)

isInitialized, err := b.IsInitialized(ctx)
if err != nil {
return nil, fmt.Errorf("checking if bucket is initialized: %w", err)
}

if isInitialized {
isUpToDate, err := b.IsUpToDate(ctx)
if err != nil {
return nil, fmt.Errorf("checking if bucket is up to date: %w", err)
}
if !isUpToDate {
return nil, systemcontroller.ErrNeedUpgradeBucket
}
} else {
if err := bucket.Migrate(ctx, tx, bucketName); err != nil {
return nil, fmt.Errorf("migrating bucket: %w", err)
}
}

if err := tx.Commit(); err != nil {
return nil, fmt.Errorf("committing sql transaction to create bucket schema: %w", err)
}

return b, nil
}

func (d *Driver) createLedgerStore(ctx context.Context, db bun.IDB, ledger ledger.Ledger) (*ledgerstore.Store, error) {

tx, err := db.BeginTx(ctx, &sql.TxOptions{})
Expand Down Expand Up @@ -164,15 +127,6 @@ func (d *Driver) UpdateLedgerMetadata(ctx context.Context, name string, m metada
return err
}

func (d *Driver) UpdateLedgerState(ctx context.Context, name string, state string) error {
_, err := d.db.NewUpdate().
Model(&ledger.Ledger{}).
Set("state = ?", state).
Where("name = ?", name).
Exec(ctx)
return err
}

func (d *Driver) DeleteLedgerMetadata(ctx context.Context, name string, key string) error {
_, err := d.db.NewUpdate().
Model(&ledger.Ledger{}).
Expand Down
6 changes: 4 additions & 2 deletions internal/storage/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"github.com/formancehq/go-libs/bun/bunpaginate"
"github.com/formancehq/go-libs/metadata"
"github.com/formancehq/go-libs/pointer"
ledger "github.com/formancehq/ledger/internal"
ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
"github.com/formancehq/ledger/internal/storage/driver"
Expand All @@ -21,7 +22,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestUpgradeAllBuckets(t *testing.T) {
func TestUpgradeAllLedgers(t *testing.T) {
t.Parallel()

d := newStorageDriver(t)
Expand All @@ -31,11 +32,12 @@ func TestUpgradeAllBuckets(t *testing.T) {

for i := 0; i < count; i++ {
name := fmt.Sprintf("ledger%d", i)
_, err := d.CreateBucket(ctx, name)
_, err := d.CreateLedger(ctx, pointer.For(ledger.MustNewWithDefault(name)))
require.NoError(t, err)
}

require.NoError(t, d.UpgradeAllBuckets(ctx))
require.NoError(t, d.UpgradeAllLedgers(ctx))
}

func TestLedgersCreate(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/testserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (s *Server) Start() {
args := []string{
"serve",
"--" + cmd.BindFlag, ":0",
"--" + cmd.AutoUpgradeFlag,
"--" + bunconnect.PostgresURIFlag, s.configuration.PostgresConfiguration.DatabaseSourceName,
"--" + bunconnect.PostgresMaxOpenConnsFlag, fmt.Sprint(s.configuration.PostgresConfiguration.MaxOpenConns),
"--" + bunconnect.PostgresConnMaxIdleTimeFlag, fmt.Sprint(s.configuration.PostgresConfiguration.ConnMaxIdleTime),
Expand Down
13 changes: 9 additions & 4 deletions cmd/doc_events.go → tools/docs/events/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package main

import (
"encoding/json"
Expand All @@ -11,13 +11,11 @@ import (
"reflect"
)

// todo: export in a /tools folder
func NewDocEventsCommand() *cobra.Command {
func newDocEventsCommand() *cobra.Command {
const (
writeDirFlag = "write-dir"
)
cmd := &cobra.Command{
Use: "events",
RunE: func(cmd *cobra.Command, args []string) error {

writeDir, err := cmd.Flags().GetString(writeDirFlag)
Expand Down Expand Up @@ -51,5 +49,12 @@ func NewDocEventsCommand() *cobra.Command {
},
}
cmd.Flags().String(writeDirFlag, "", "directory to write events to")

return cmd
}

func main() {
if err := newDocEventsCommand().Execute(); err != nil {
os.Exit(1)
}
}
18 changes: 12 additions & 6 deletions cmd/doc_flags.go → tools/docs/flags/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cmd
package main

import (
"fmt"
ledgercmd "github.com/formancehq/ledger/cmd"
"os"
"sort"
"strings"
"text/tabwriter"
Expand All @@ -10,17 +12,16 @@ import (
"github.com/spf13/pflag"
)

// todo: export in a /tools folder
func NewDocFlagsCommand() *cobra.Command {
cmd := &cobra.Command{
func newDocFlagsCommand() *cobra.Command {
return &cobra.Command{
Use: "flags",
RunE: func(cmd *cobra.Command, args []string) error {

w := tabwriter.NewWriter(cmd.OutOrStdout(), 0, 0, 1, ' ', tabwriter.Debug)

allKeys := make([]string, 0)

serveCommand := NewServeCommand()
serveCommand := ledgercmd.NewServeCommand()
serveCommand.Flags().VisitAll(func(f *pflag.Flag) {
allKeys = append(allKeys, f.Name)
})
Expand Down Expand Up @@ -49,5 +50,10 @@ func NewDocFlagsCommand() *cobra.Command {
return w.Flush()
},
}
return cmd
}

func main() {
if err := newDocFlagsCommand().Execute(); err != nil {
os.Exit(1)
}
}

0 comments on commit 13ddb5d

Please sign in to comment.