From 04e3dd13d71d9055f2c33194bfcb2614263d82db Mon Sep 17 00:00:00 2001 From: David Ragot Date: Mon, 8 Jan 2024 12:20:44 +0100 Subject: [PATCH 1/2] fix: enquote search path --- components/ledger/libs/bun/bunconnect/connect.go | 9 +++++---- components/ledger/libs/collectionutils/slice.go | 14 ++++++++++++-- libs/go-libs/bun/bunconnect/connect.go | 9 +++++---- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/components/ledger/libs/bun/bunconnect/connect.go b/components/ledger/libs/bun/bunconnect/connect.go index 217a0999fa..aacbf043d2 100644 --- a/components/ledger/libs/bun/bunconnect/connect.go +++ b/components/ledger/libs/bun/bunconnect/connect.go @@ -3,13 +3,14 @@ package bunconnect import ( "database/sql" "fmt" + "io" + "net/url" + "time" + "github.com/formancehq/stack/libs/go-libs/bun/bundebug" "github.com/uptrace/bun" "github.com/uptrace/bun/dialect/pgdialect" "github.com/uptrace/bun/extra/bunotel" - "io" - "net/url" - "time" ) type ConnectionOptions struct { @@ -64,7 +65,7 @@ func OpenDBWithSchema(connectionOptions ConnectionOptions, schema string, hooks } query := parsedConnectionParams.Query() - query.Set("search_path", schema) + query.Set("search_path", fmt.Sprintf(`"%s"`, schema)) parsedConnectionParams.RawQuery = query.Encode() connectionOptions.DatabaseSourceName = parsedConnectionParams.String() diff --git a/components/ledger/libs/collectionutils/slice.go b/components/ledger/libs/collectionutils/slice.go index 58d2faead6..087d9358bf 100644 --- a/components/ledger/libs/collectionutils/slice.go +++ b/components/ledger/libs/collectionutils/slice.go @@ -70,8 +70,10 @@ func Contains[T any](slice []T, t T) bool { type Set[T comparable] map[T]struct{} -func (s Set[T]) Put(t T) { - s[t] = struct{}{} +func (s Set[T]) Put(t ...T) { + for _, t2 := range t { + s[t2] = struct{}{} + } } func (s Set[T]) Contains(t T) bool { @@ -79,6 +81,14 @@ func (s Set[T]) Contains(t T) bool { return ok } +func (s Set[T]) ToSlice() []T { + ret := make([]T, 0) + for k := range s { + ret = append(ret, k) + } + return ret +} + func NewSet[T comparable]() Set[T] { return make(Set[T], 0) } diff --git a/libs/go-libs/bun/bunconnect/connect.go b/libs/go-libs/bun/bunconnect/connect.go index 217a0999fa..aacbf043d2 100644 --- a/libs/go-libs/bun/bunconnect/connect.go +++ b/libs/go-libs/bun/bunconnect/connect.go @@ -3,13 +3,14 @@ package bunconnect import ( "database/sql" "fmt" + "io" + "net/url" + "time" + "github.com/formancehq/stack/libs/go-libs/bun/bundebug" "github.com/uptrace/bun" "github.com/uptrace/bun/dialect/pgdialect" "github.com/uptrace/bun/extra/bunotel" - "io" - "net/url" - "time" ) type ConnectionOptions struct { @@ -64,7 +65,7 @@ func OpenDBWithSchema(connectionOptions ConnectionOptions, schema string, hooks } query := parsedConnectionParams.Query() - query.Set("search_path", schema) + query.Set("search_path", fmt.Sprintf(`"%s"`, schema)) parsedConnectionParams.RawQuery = query.Encode() connectionOptions.DatabaseSourceName = parsedConnectionParams.String() From 1c2663e000ea7e3d3958fc02742276302ec272b2 Mon Sep 17 00:00:00 2001 From: David Ragot Date: Mon, 8 Jan 2024 12:33:51 +0100 Subject: [PATCH 2/2] fix: add constraint on _system store --- .../internal/storage/systemstore/migrations.go | 14 ++++++++++++++ tests/integration/suite/ledger-create.go | 17 ++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/components/ledger/internal/storage/systemstore/migrations.go b/components/ledger/internal/storage/systemstore/migrations.go index 27ab83fc18..5a1f3149cf 100644 --- a/components/ledger/internal/storage/systemstore/migrations.go +++ b/components/ledger/internal/storage/systemstore/migrations.go @@ -60,6 +60,20 @@ func Migrate(ctx context.Context, db bun.IDB) error { return sqlutils.PostgresError(err) }, }, + migrations.Migration{ + Name: "Add ledger, bucket naming constraints 63 chars", + UpWithContext: func(ctx context.Context, tx bun.Tx) error { + _, err := tx.ExecContext(ctx, ` + alter table ledgers + alter column ledger type varchar(63), + alter column bucket type varchar(63); + `) + if err != nil { + return err + } + return nil + }, + }, ) return migrator.Up(ctx, db) } diff --git a/tests/integration/suite/ledger-create.go b/tests/integration/suite/ledger-create.go index 579509d721..f27fbe6ee0 100644 --- a/tests/integration/suite/ledger-create.go +++ b/tests/integration/suite/ledger-create.go @@ -1,13 +1,17 @@ package suite import ( + "net/http" + "strings" + "github.com/formancehq/formance-sdk-go/pkg/models/operations" "github.com/formancehq/formance-sdk-go/pkg/models/sdkerrors" + "github.com/formancehq/formance-sdk-go/pkg/models/shared" + "github.com/formancehq/stack/libs/go-libs/pointer" . "github.com/formancehq/stack/tests/integration/internal" "github.com/formancehq/stack/tests/integration/internal/modules" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" - "net/http" ) var _ = WithModules([]*Module{modules.Ledger}, func() { @@ -31,4 +35,15 @@ var _ = WithModules([]*Module{modules.Ledger}, func() { It("should fail", func() {}) }) }) + When("bucket naming convention depends on the database 63 bytes length (pg constraint)", func() { + It("should fail with > 63 characters in ledger or bucket name", func() { + _, err := Client().Ledger.V2CreateLedger(TestContext(), operations.V2CreateLedgerRequest{ + V2CreateLedgerRequest: &shared.V2CreateLedgerRequest{ + Bucket: pointer.For(strings.Repeat("a", 64)), + }, + Ledger: "default", + }) + Expect(err).To(HaveOccurred()) + }) + }) })