Skip to content

Commit

Permalink
fix: bucket naming length (#1092)
Browse files Browse the repository at this point in the history
Co-authored-by: David Ragot <[email protected]>
  • Loading branch information
Dav-14 and David Ragot authored Jan 8, 2024
1 parent 40d46a0 commit 34f706d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
14 changes: 14 additions & 0 deletions components/ledger/internal/storage/systemstore/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
9 changes: 5 additions & 4 deletions components/ledger/libs/bun/bunconnect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
14 changes: 12 additions & 2 deletions components/ledger/libs/collectionutils/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,25 @@ 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 {
_, ok := s[t]
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)
}
9 changes: 5 additions & 4 deletions libs/go-libs/bun/bunconnect/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
17 changes: 16 additions & 1 deletion tests/integration/suite/ledger-create.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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())
})
})
})

0 comments on commit 34f706d

Please sign in to comment.