fix(setup): offer the bundled database as an explicit choice on a re-run (#1388)
With an existing database_url, setup prefilled the URL, so a blank answer kept it and the bundled socket database was unreachable without typing its URL. The re-run database step now presents a choice menu: keep the existing URL (still the default), switch to the bundled database (socket URL plus a generated PGPASSWORD written to the env file), or enter a different URL. Fresh installs and -database-url are unchanged; a blank answer anywhere still keeps the existing URL. Pinned at the setup API level with fake prompter/sink: TestRunParamsDatabaseChoice.
This commit is contained in:
2 files changed
+251
-7
No files matched your search
@@ -136,24 +136,56 @@ func RunParams(ctx context.Context, p Prompter, discovery ModelDiscovery, secret
|
|||||||
}
|
}
|
||||||
|
|
||||||
// stepDatabase returns the PostgreSQL URL. A blank answer on a fresh install
|
// stepDatabase returns the PostgreSQL URL. A blank answer on a fresh install
|
||||||
// means the bundled database: no network, reached through its Unix socket, with
|
// means the bundled database. On a re-run with an existing URL the bundled
|
||||||
// a generated password that goes only to the env file as PGPASSWORD, which pgx
|
// database is an explicit choice rather than something reachable only by
|
||||||
// and Compose both read, so config.toml holds no secret.
|
// typing its socket URL; keeping the existing URL is the first option, so
|
||||||
|
// the default answer is unchanged.
|
||||||
func stepDatabase(ctx context.Context, p Prompter, secrets SecretSink, existing, param string) (string, error) {
|
func stepDatabase(ctx context.Context, p Prompter, secrets SecretSink, existing, param string) (string, error) {
|
||||||
if strings.TrimSpace(param) != "" {
|
if strings.TrimSpace(param) != "" {
|
||||||
return param, nil
|
return param, nil
|
||||||
}
|
}
|
||||||
prompt := "PostgreSQL 18 URL (blank for the bundled database with a generated password): "
|
if strings.TrimSpace(existing) != "" {
|
||||||
if existing != "" {
|
choice, err := p.Select(ctx, "Database:", []string{
|
||||||
prompt = "PostgreSQL 18 URL: "
|
"Keep the existing database URL",
|
||||||
|
"Bundled database (Unix socket, generated password in the env file)",
|
||||||
|
"Enter a different PostgreSQL 18 URL",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("setup: database choice: %w", err)
|
||||||
}
|
}
|
||||||
url, err := p.ReadLine(ctx, prompt, existing)
|
switch choice {
|
||||||
|
case 0:
|
||||||
|
return existing, nil
|
||||||
|
case 1:
|
||||||
|
return bundledDatabase(secrets)
|
||||||
|
default:
|
||||||
|
url, err := p.ReadLine(ctx, "PostgreSQL 18 URL: ", "")
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("setup: database url: %w", err)
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(url) == "" {
|
||||||
|
// A blank answer keeps the existing URL, as it always has.
|
||||||
|
return existing, nil
|
||||||
|
}
|
||||||
|
return url, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
prompt := "PostgreSQL 18 URL (blank for the bundled database with a generated password): "
|
||||||
|
url, err := p.ReadLine(ctx, prompt, "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("setup: database url: %w", err)
|
return "", fmt.Errorf("setup: database url: %w", err)
|
||||||
}
|
}
|
||||||
if strings.TrimSpace(url) != "" {
|
if strings.TrimSpace(url) != "" {
|
||||||
return url, nil
|
return url, nil
|
||||||
}
|
}
|
||||||
|
return bundledDatabase(secrets)
|
||||||
|
}
|
||||||
|
|
||||||
|
// bundledDatabase switches to the bundled database: no network, reached
|
||||||
|
// through its Unix socket, with a generated password that goes only to the
|
||||||
|
// env file as PGPASSWORD, which pgx and Compose both read, so config.toml
|
||||||
|
// holds no secret.
|
||||||
|
func bundledDatabase(secrets SecretSink) (string, error) {
|
||||||
pw := make([]byte, 24)
|
pw := make([]byte, 24)
|
||||||
if _, err := rand.Read(pw); err != nil {
|
if _, err := rand.Read(pw); err != nil {
|
||||||
return "", fmt.Errorf("setup: generate database password: %w", err)
|
return "", fmt.Errorf("setup: generate database password: %w", err)
|
||||||
|
|||||||
@@ -0,0 +1,212 @@
|
|||||||
|
package setup
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||||
|
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite"
|
||||||
|
)
|
||||||
|
|
||||||
|
// fakePrompter answers prompts from maps keyed by the exact prompt text and
|
||||||
|
// records what it was asked, so a test can pin both the answers and that the
|
||||||
|
// bundled database was actually offered.
|
||||||
|
type fakePrompter struct {
|
||||||
|
selectAnswers map[string]int
|
||||||
|
lineAnswers map[string]string
|
||||||
|
confirmAnswer bool
|
||||||
|
|
||||||
|
selects []recordedSelect
|
||||||
|
lines []recordedLine
|
||||||
|
}
|
||||||
|
|
||||||
|
type recordedSelect struct {
|
||||||
|
prompt string
|
||||||
|
options []string
|
||||||
|
}
|
||||||
|
|
||||||
|
type recordedLine struct {
|
||||||
|
prompt string
|
||||||
|
defaultValue string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakePrompter) Select(_ context.Context, prompt string, options []string) (int, error) {
|
||||||
|
f.selects = append(f.selects, recordedSelect{prompt: prompt, options: options})
|
||||||
|
if ans, ok := f.selectAnswers[prompt]; ok {
|
||||||
|
return ans, nil
|
||||||
|
}
|
||||||
|
return 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakePrompter) ReadLine(_ context.Context, prompt, defaultValue string) (string, error) {
|
||||||
|
f.lines = append(f.lines, recordedLine{prompt: prompt, defaultValue: defaultValue})
|
||||||
|
if ans, ok := f.lineAnswers[prompt]; ok {
|
||||||
|
return ans, nil
|
||||||
|
}
|
||||||
|
return defaultValue, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *fakePrompter) ReadSecret(context.Context, string) (string, error) { return "", nil }
|
||||||
|
|
||||||
|
func (f *fakePrompter) Confirm(context.Context, string, bool) (bool, error) {
|
||||||
|
return f.confirmAnswer, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// fakeSink records Put calls by engine/key.
|
||||||
|
type fakeSink struct {
|
||||||
|
puts map[string]string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *fakeSink) Put(engine, key, value string) error {
|
||||||
|
if s.puts == nil {
|
||||||
|
s.puts = map[string]string{}
|
||||||
|
}
|
||||||
|
s.puts[engine+"/"+key] = value
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *fakeSink) Commit() error { return nil }
|
||||||
|
|
||||||
|
// databasePrompt is the re-run database choice; bundledChoice is the index of
|
||||||
|
// the bundled database option in that menu.
|
||||||
|
const (
|
||||||
|
databasePrompt = "Database:"
|
||||||
|
bundledChoice = 1
|
||||||
|
differentURL = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
func bundledURL() string {
|
||||||
|
return "postgres://archie@/archie?host=" + configuration.DefaultPostgresSocketDir()
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestRunParamsDatabaseChoice pins the re-run database step at the setup API
|
||||||
|
// level: with an existing database_url the flow must offer the bundled
|
||||||
|
// database as an explicit choice, switching to it must generate PGPASSWORD
|
||||||
|
// into the sink, and keeping must leave the existing URL untouched.
|
||||||
|
func TestRunParamsDatabaseChoice(t *testing.T) {
|
||||||
|
const existing = "postgres://operator:secret@db.internal:5432/archie"
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
// existing is the prefilled database_url; empty means fresh install.
|
||||||
|
existing string
|
||||||
|
// param is Params.DatabaseURL (the -database-url flag).
|
||||||
|
param string
|
||||||
|
selectAnswers map[string]int
|
||||||
|
lineAnswers map[string]string
|
||||||
|
wantURL string
|
||||||
|
wantPassword bool
|
||||||
|
wantChoice bool // a database Select menu must be presented
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "re-run keep existing is the default choice",
|
||||||
|
existing: existing,
|
||||||
|
selectAnswers: map[string]int{databasePrompt: 0},
|
||||||
|
wantURL: existing,
|
||||||
|
wantChoice: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "re-run switch to the bundled database",
|
||||||
|
existing: existing,
|
||||||
|
selectAnswers: map[string]int{databasePrompt: bundledChoice},
|
||||||
|
wantURL: bundledURL(),
|
||||||
|
wantPassword: true,
|
||||||
|
wantChoice: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "re-run enter a different URL",
|
||||||
|
existing: existing,
|
||||||
|
selectAnswers: map[string]int{databasePrompt: differentURL},
|
||||||
|
lineAnswers: map[string]string{"PostgreSQL 18 URL: ": "postgres://other.internal/archie"},
|
||||||
|
wantURL: "postgres://other.internal/archie",
|
||||||
|
wantChoice: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "re-run blank answer on the different-URL prompt keeps existing",
|
||||||
|
existing: existing,
|
||||||
|
selectAnswers: map[string]int{databasePrompt: differentURL},
|
||||||
|
lineAnswers: map[string]string{"PostgreSQL 18 URL: ": ""},
|
||||||
|
wantURL: existing,
|
||||||
|
wantChoice: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "fresh install blank answer means the bundled database",
|
||||||
|
existing: "",
|
||||||
|
lineAnswers: map[string]string{"PostgreSQL 18 URL (blank for the bundled database with a generated password): ": ""},
|
||||||
|
wantURL: bundledURL(),
|
||||||
|
wantPassword: true,
|
||||||
|
wantChoice: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "database-url flag wins without prompting",
|
||||||
|
existing: existing,
|
||||||
|
param: "postgres://flag.internal/archie",
|
||||||
|
wantURL: "postgres://flag.internal/archie",
|
||||||
|
wantChoice: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
p := &fakePrompter{
|
||||||
|
selectAnswers: tt.selectAnswers,
|
||||||
|
lineAnswers: tt.lineAnswers,
|
||||||
|
confirmAnswer: false,
|
||||||
|
}
|
||||||
|
sink := &fakeSink{}
|
||||||
|
params := Params{
|
||||||
|
DatabaseURL: tt.param,
|
||||||
|
BotUser: "archie-bot",
|
||||||
|
ForgeType: "none",
|
||||||
|
Provider: "ollama",
|
||||||
|
Model: "llama3",
|
||||||
|
}
|
||||||
|
edits, err := RunParams(context.Background(), p, nil, sink, ExistingValues{DatabaseURL: tt.existing}, params)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("RunParams: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var gotURL string
|
||||||
|
for _, e := range edits {
|
||||||
|
if e.Table == "" && e.Key == "database_url" {
|
||||||
|
gotURL = e.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if gotURL != tomlwrite.String(tt.wantURL) {
|
||||||
|
t.Errorf("database_url = %s, want %s", gotURL, tomlwrite.String(tt.wantURL))
|
||||||
|
}
|
||||||
|
|
||||||
|
password, hasPassword := sink.puts["env/PGPASSWORD"]
|
||||||
|
if hasPassword != tt.wantPassword {
|
||||||
|
t.Fatalf("PGPASSWORD stored = %v, want %v", hasPassword, tt.wantPassword)
|
||||||
|
}
|
||||||
|
if tt.wantPassword && len(password) != 48 {
|
||||||
|
t.Errorf("PGPASSWORD = %q, want a 48-character hex password", password)
|
||||||
|
}
|
||||||
|
|
||||||
|
var offered []string
|
||||||
|
for _, sel := range p.selects {
|
||||||
|
if sel.prompt == databasePrompt {
|
||||||
|
offered = sel.options
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if tt.wantChoice {
|
||||||
|
if offered == nil {
|
||||||
|
t.Fatalf("no database choice was presented on a re-run; prompts seen: %+v", p.selects)
|
||||||
|
}
|
||||||
|
bundled := false
|
||||||
|
for _, opt := range offered {
|
||||||
|
if strings.Contains(strings.ToLower(opt), "bundled") {
|
||||||
|
bundled = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !bundled {
|
||||||
|
t.Errorf("database choice options %v do not offer the bundled database", offered)
|
||||||
|
}
|
||||||
|
} else if offered != nil {
|
||||||
|
t.Errorf("database choice was presented (%v) where the flow should not ask", offered)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user