Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/unify sql store #94

Merged
merged 7 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (
"encoding/json"
"fmt"
"github.com/numary/ledger/api/controllers"
"github.com/numary/ledger/storage/postgres"
"github.com/numary/ledger/storage/sqlite"
"github.com/numary/ledger/storage/sqlstorage"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"go.uber.org/fx"
Expand Down Expand Up @@ -41,22 +40,28 @@ var (
return errors.Wrap(err, "creating storage directory")
}

if viper.GetBool("debug") {
logrus.StandardLogger().Level = logrus.DebugLevel
}

var (
driver storage.Driver
)

switch viper.GetString("storage.driver") {
case "sqlite":
storage.RegisterDriver("sqlite", sqlite.NewDriver(
viper.GetString("storage.dir"),
viper.GetString("storage.sqlite.db_name"),
))
driver = sqlstorage.NewOpenCloseDBDriver(sqlstorage.SQLite, func(name string) string {
return sqlstorage.SQLiteFileConnString(path.Join(
viper.GetString("storage.dir"),
fmt.Sprintf("%s_%s.db", viper.GetString("storage.sqlite.db_name"), name),
))
})
case "postgres":
storage.RegisterDriver("postgres", postgres.NewDriver(
viper.GetString("storage.postgres.conn_string"),
))
driver = sqlstorage.NewCachedDBDriver(sqlstorage.PostgreSQL, viper.GetString("storage.postgres.conn_string"))
default:
return fmt.Errorf("unknown storage driver %s", viper.GetString("storage.driver"))
}
if viper.GetBool("debug") {
logrus.StandardLogger().Level = logrus.DebugLevel
}
storage.RegisterDriver(viper.GetString("storage.driver"), driver)
return nil
},
}
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.16

require (
github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-contrib/cors v1.3.1
github.com/gin-contrib/logger v0.2.0
github.com/gin-gonic/gin v1.7.4
Expand All @@ -14,10 +15,13 @@ require (
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-sqlite3 v1.14.7
github.com/numary/machine v0.0.0-20210831114934-e54c99840e08
github.com/ory/dockertest/v3 v3.8.1
github.com/pborman/uuid v1.2.1
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.8.1
github.com/stretchr/testify v1.7.0
github.com/swaggo/swag v1.7.4
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/dig v1.13.0 // indirect
Expand Down
68 changes: 68 additions & 0 deletions go.sum

Large diffs are not rendered by default.

38 changes: 13 additions & 25 deletions ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import (
"errors"
"flag"
"fmt"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/numary/ledger/storage/sqlite"
"github.com/numary/ledger/storage/sqlstorage"
"github.com/sirupsen/logrus"
"math/rand"
"os"
"path"
"reflect"
"testing"

Expand All @@ -20,12 +18,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/numary/ledger/core"
"github.com/numary/ledger/ledger/query"
"github.com/numary/ledger/storage/postgres"
"go.uber.org/fx"

// Register sql drivers
_ "github.com/numary/ledger/storage/postgres"
_ "github.com/numary/ledger/storage/sqlite"
)

func with(f func(l *Ledger)) {
Expand Down Expand Up @@ -65,25 +58,20 @@ func TestMain(m *testing.M) {

switch os.Getenv("NUMARY_STORAGE_DRIVER") {
case "sqlite":
storage.RegisterDriver("sqlite", sqlite.NewDriver(os.TempDir(), "ledger"))
err := os.Remove(path.Join(os.TempDir(), "ledger_test.db"))
if err != nil {
panic(err)
}
storage.RegisterDriver("sqlite", sqlstorage.NewOpenCloseDBDriver(
sqlstorage.SQLite,
func(name string) string {
return fmt.Sprintf("file:%s/%s.db?_journal=WAL", os.TempDir(), name)
}),
)
case "postgres":
connString := os.Getenv("NUMARY_STORAGE_POSTGRES_CONN_STRING")
storage.RegisterDriver("postgres", postgres.NewDriver(connString))

// @gfyrag: Why this test?
pool, err := pgxpool.Connect(context.Background(), connString)
if err != nil {
panic(err)
}
store, err := postgres.NewStore("test", pool)
if err != nil {
panic(err)
}
store.DropTest()
storage.RegisterDriver("postgres", sqlstorage.NewOpenCloseDBDriver(
sqlstorage.PostgreSQL,
func(name string) string {
return connString
}),
)
}

m.Run()
Expand Down
4 changes: 3 additions & 1 deletion storage/driver.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package storage

import "context"
import (
"context"
)

type Driver interface {
Initialize(ctx context.Context) error
Expand Down
107 changes: 0 additions & 107 deletions storage/postgres/accounts.go

This file was deleted.

99 changes: 0 additions & 99 deletions storage/postgres/aggregations.go

This file was deleted.

Loading