Skip to content
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ becomes the release notes of the next tag.

## [Unreleased]

### Fixed

- A removed or moved 0.x flag (`--server.max_cost`, `--server.tx_end`,
kebab-case plugin flags) names its replacement or the reason it was removed,
as the config keys and environment variables do, instead of a bare
`unknown flag`.
- Tests cover `413 body_too_large` for oversized bodies that are not JSON,
including chunked ones.

### Documentation

- Fix the quickstart (the `Location` and `unknown_column` samples, the
Expand Down
1 change: 1 addition & 0 deletions cmd/pdbr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func rootCmd() *cobra.Command {
SilenceUsage: true,
SilenceErrors: true,
}
root.SetFlagErrorFunc(func(_ *cobra.Command, err error) error { return config.ExplainFlagError(err) })
root.PersistentFlags().String("config", "", "path to config file (default: ./pdbr.yaml if present)")
root.AddCommand(serveCmd(), requestCmd(), schemaCmd(), configCmd(), pluginsCmd())
return root
Expand Down
20 changes: 20 additions & 0 deletions cmd/pdbr/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -226,3 +227,22 @@ func TestRequestIdentityLive(t *testing.T) {
t.Errorf("--role anonymous --claims user 1 = %s", got)
}
}

func TestOldFlagsNameTheirReplacement(t *testing.T) {
for args, want := range map[string]string{
"serve --server.tx_end=commit": "unknown flag: --server.tx_end: moved to --transactions.end",
"serve --server.max_cost=5": "unknown flag: --server.max_cost: moved to --limits.max_cost",
"serve --plugins.settings.advanced-filters.enabled=1": "moved to --plugins.settings.advanced_filters.enabled",
"serve --rls.enabled=false": "moved to --roles.switch",
"plugins list --transactions.mutations": "removed: every write runs in a transaction",
} {
cmd := rootCmd()
cmd.SetArgs(strings.Fields(args))
cmd.SetOut(io.Discard)
cmd.SetErr(io.Discard)
err := cmd.Execute()
if err == nil || !strings.Contains(err.Error(), want) {
t.Errorf("%s: err = %v, want %q", args, err, want)
}
}
}
4 changes: 2 additions & 2 deletions docs/upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ Loading a config or environment that uses an old key fails and names the
new one (`unknown key server.tx_end: moved to transactions.end`,
`unknown environment variable PDBR_SERVER_MAX__COST (server.max_cost):
moved to PDBR_LIMITS_MAX__COST`); flags are renamed the same way
(`--server.tx_end` is `--transactions.end`), but an old flag is only rejected
as `unknown flag` without a suggestion: use the table below.
(`--server.tx_end` is `--transactions.end`), and an old flag is rejected with
the same hint (`unknown flag: --server.tx_end: moved to --transactions.end`).

- **`server.base_path` defaults to `""`** (was `/api`): the API is mounted
at the root, as in PostgREST, so `GET /api/users` is now `GET /users` and
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/santhosh-tekuri/jsonschema/v6 v6.0.3
github.com/spf13/cobra v1.10.2
github.com/spf13/pflag v1.0.10
github.com/suprbdev/pdbcore v0.19.0
github.com/suprbdev/pdbcore v0.19.1
go.yaml.in/yaml/v3 v3.0.5
golang.org/x/sync v0.23.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
github.com/suprbdev/pdbcore v0.19.0 h1:0j+vkzyt1bKcREPh6OUT9vX69dzL5QMzAAJvd1Ck+KM=
github.com/suprbdev/pdbcore v0.19.0/go.mod h1:loM9g7/i1GhIY4CRmHeAiMucJ57nxTgxN0YNHc7tW7w=
github.com/suprbdev/pdbcore v0.19.1 h1:vTl8odNVm2Gi5nU+hsDr4rvY22cozzz0cKLPRp758dg=
github.com/suprbdev/pdbcore v0.19.1/go.mod h1:loM9g7/i1GhIY4CRmHeAiMucJ57nxTgxN0YNHc7tW7w=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.yaml.in/yaml/v2 v2.4.4 h1:tuyd0P+2Ont/d6e2rl3be67goVK4R6deVxCUX5vyPaQ=
Expand Down
9 changes: 9 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/suprbdev/pdbcore/auth"
"github.com/suprbdev/pdbcore/confx"
"github.com/suprbdev/pdbcore/confx/cliflags"
"github.com/suprbdev/pdbcore/contrib"

"github.com/suprbdev/pdbr/internal/plugins/advancedfilters"
Expand Down Expand Up @@ -211,6 +212,14 @@ var RemovedKeys = map[string]string{
"plugins.settings.nested_mutations.force_transactions": "nested writes always run in a transaction",
}

// ExplainFlagError adds the moved-to or removed hint to the error of an
// unknown flag that names an old config key (--server.tx_end), as loading
// the key from a file or PDBR_* variable does.
func ExplainFlagError(err error) error {
cfg := DefaultConfig()
return cliflags.Explain(err, &cfg, confx.WithMoved(MovedKeys), confx.WithRemoved(RemovedKeys))
}

// Load builds the effective config: defaults, then the optional YAML file,
// then PDBR_* env vars, then flags (highest precedence: koanf path to
// value, see confx.WithFlags), and validates it. Pass only the flags the
Expand Down
18 changes: 18 additions & 0 deletions test/e2e_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package e2e
import (
"context"
"errors"
"io"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -223,6 +224,23 @@ func TestErrorCodesNeedingConfig(t *testing.T) {
big := `{"email":"` + strings.Repeat("x", 100) + `"}`
runRows(t, srv, []errorRow{
{name: "content-length", method: "POST", path: "/api/users", body: big, status: 413, code: "body_too_large"},
{name: "not JSON", method: "POST", path: "/api/users", body: strings.Repeat("x", 100), hdr: map[string]string{"Content-Type": "application/json"}, status: 413, code: "body_too_large"},
{name: "not JSON text/plain", method: "POST", path: "/api/users", body: strings.Repeat("x", 100), hdr: map[string]string{"Content-Type": "text/plain"}, status: 413, code: "body_too_large"},
{name: "not JSON rpc", method: "POST", path: "/api/rpc/add_numbers", body: strings.Repeat("x", 100), hdr: map[string]string{"Content-Type": "application/json"}, status: 413, code: "body_too_large"},
})
t.Run("chunked not JSON", func(t *testing.T) {
req, err := http.NewRequest(http.MethodPost, srv+"/api/users", io.NopCloser(strings.NewReader(strings.Repeat("x", 100))))
if err != nil {
t.Fatal(err)
}
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
b, _ := io.ReadAll(res.Body)
wantProblem(t, res, b, 413, "body_too_large")
})
})
t.Run("timeout", func(t *testing.T) {
Expand Down
Loading