Skip to content

Commit b03e923

Browse files
committed
feat(postgres): honor --sandbox-binary for binary discovery
Binary discovery for PostgreSQL was hardcoded to ~/opt/postgresql/<version> (FindBinary in postgresql.go and basedirFromVersion), so the pg path ignored the --sandbox-binary flag that the MySQL deploy path already honors. In deploySandboxPostgreSQL, when --sandbox-binary is explicitly provided, the binaries are now expected at <sandbox-binary>/<version>/bin/postgres and that directory is passed to the provider as config.Options["basedir"], which resolveBasedir already consumes (sandbox.go:150). The basedir then drives bin/lib/share for initdb and the generated scripts. The --sandbox-binary flag is shared (default ~/opt/mysql), so it is applied to PostgreSQL ONLY when flags.Changed() — otherwise the strict historical behavior (~/opt/postgresql/<version> via FindBinary) is preserved, including the existing 'Run: dbdeployer unpack ...' hint. The Provider interface is unchanged. Added TestResolveBasedir locking the Options["basedir"] override contract that the cmd layer now relies on (override honored; empty/absent falls back to ~/opt/postgresql/<version>).
1 parent 51203a3 commit b03e923

2 files changed

Lines changed: 53 additions & 2 deletions

File tree

cmd/deploy_postgresql.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,26 @@ func deploySandboxPostgreSQL(cmd *cobra.Command, args []string) {
4040
common.Exitf(1, "invalid version: %s", err)
4141
}
4242

43-
if _, err := p.FindBinary(version); err != nil {
43+
// Binary discovery. The --sandbox-binary flag is shared with the MySQL
44+
// path and defaults to ~/opt/mysql, so it is only applied to PostgreSQL
45+
// when explicitly provided. When set, binaries are expected at
46+
// <sandbox-binary>/<version>/bin/postgres and that directory becomes the
47+
// sandbox basedir (bin/lib/share). When not set, fall back to the strict
48+
// historical behavior: ~/opt/postgresql/<version>.
49+
options := map[string]string{}
50+
if flags.Changed(globals.SandboxBinaryLabel) {
51+
sandboxBinary, _ := flags.GetString(globals.SandboxBinaryLabel)
52+
sbAbs, err := common.AbsolutePath(sandboxBinary)
53+
if err != nil {
54+
common.Exitf(1, "error defining absolute path for --%s: %s", globals.SandboxBinaryLabel, err)
55+
}
56+
basedir := path.Join(sbAbs, version)
57+
binPath := path.Join(basedir, "bin", "postgres")
58+
if !common.FileExists(binPath) {
59+
common.Exitf(1, "PostgreSQL binary not found at %s", binPath)
60+
}
61+
options["basedir"] = basedir
62+
} else if _, err := p.FindBinary(version); err != nil {
4463
common.Exitf(1, "PostgreSQL binaries not found: %s\nRun: dbdeployer unpack --provider=postgresql <server.deb> <client.deb>", err)
4564
}
4665

@@ -98,7 +117,7 @@ func deploySandboxPostgreSQL(cmd *cobra.Command, args []string) {
98117
Host: "127.0.0.1",
99118
DbUser: dbUser,
100119
DbPassword: dbPassword,
101-
Options: map[string]string{},
120+
Options: options,
102121
}
103122

104123
if _, err := p.CreateSandbox(config); err != nil {

providers/postgresql/postgresql_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package postgresql
22

33
import (
4+
"os"
5+
"path/filepath"
46
"strings"
57
"testing"
68

@@ -278,3 +280,33 @@ func TestGenerateCheckRecoveryScriptDbUser(t *testing.T) {
278280
t.Error("recovery script missing replica ports")
279281
}
280282
}
283+
284+
// TestResolveBasedir locks in the contract that deploy_postgresql.go relies on:
285+
// an explicit Options["basedir"] override (set when --sandbox-binary is given)
286+
// is honored as-is, and absent/empty values fall back to ~/opt/postgresql/<ver>.
287+
func TestResolveBasedir(t *testing.T) {
288+
p := NewPostgreSQLProvider()
289+
home, _ := os.UserHomeDir()
290+
wantDefault := filepath.Join(home, "opt", "postgresql", "18.4")
291+
292+
// Explicit override is returned verbatim.
293+
got, err := p.resolveBasedir(providers.SandboxConfig{
294+
Version: "18.4",
295+
Options: map[string]string{"basedir": "/custom/root/18.4"},
296+
})
297+
if err != nil || got != "/custom/root/18.4" {
298+
t.Errorf("override: got (%q, %v), want /custom/root/18.4", got, err)
299+
}
300+
301+
// No Options: falls back to ~/opt/postgresql/<version>.
302+
got, err = p.resolveBasedir(providers.SandboxConfig{Version: "18.4", Options: map[string]string{}})
303+
if err != nil || got != wantDefault {
304+
t.Errorf("fallback: got (%q, %v), want %q", got, err, wantDefault)
305+
}
306+
307+
// Empty basedir value is treated as unset (falls back).
308+
got, err = p.resolveBasedir(providers.SandboxConfig{Version: "18.4", Options: map[string]string{"basedir": ""}})
309+
if err != nil || got != wantDefault {
310+
t.Errorf("empty basedir: got (%q, %v), want %q", got, err, wantDefault)
311+
}
312+
}

0 commit comments

Comments
 (0)