feat(init): add --database flag to configure existing server database (#2102)
Add --database flag to bd init so callers can specify a pre-existing server database name, overriding prefix-based naming. Prevents orphan database creation when an external tool (e.g. gastown) has already created the database. Also fixes display bug showing storage path instead of DB name in server mode, and exports ValidateDatabaseName for use by init validation. Co-Authored-By: Peter Chanthamynavong <peterkc@users.noreply.github.com> Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
362d82b04b
commit
a30e02a5ee
11 changed files with 395 additions and 14 deletions
|
|
@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
## [Unreleased]
|
||||
|
||||
### Fixed
|
||||
|
||||
- Prevent phantom catalog entries by respecting existing `dolt_database` config in init and migrate paths ([#2051](https://github.com/steveyegge/beads/issues/2051))
|
||||
- Replace `information_schema` queries with `SHOW COLUMNS`/`SHOW TABLES` for phantom resilience
|
||||
- Add phantom catalog detection to `bd doctor`
|
||||
- Fix `bd doctor --server` crash on phantom entries (replace `INFORMATION_SCHEMA.SCHEMATA` with `SHOW DATABASES`)
|
||||
|
||||
## [0.56.1] - 2026-02-23
|
||||
|
||||
### Fixed
|
||||
|
|
|
|||
126
cmd/bd/config_guard_test.go
Normal file
126
cmd/bd/config_guard_test.go
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
//go:build cgo
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
)
|
||||
|
||||
// TestConfigWinsOverConvention verifies SC-003: when metadata.json already
|
||||
// has a non-empty dolt_database value, the init guard logic uses it instead
|
||||
// of deriving beads_{prefix}.
|
||||
func TestConfigWinsOverConvention(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
configJSON string
|
||||
prefix string
|
||||
wantDB string
|
||||
}{
|
||||
"existing config wins over prefix": {
|
||||
configJSON: `{"backend":"dolt","dolt_database":"custom_beads"}`,
|
||||
prefix: "test",
|
||||
wantDB: "custom_beads",
|
||||
},
|
||||
"empty config falls through to prefix": {
|
||||
configJSON: `{"backend":"dolt"}`,
|
||||
prefix: "test",
|
||||
wantDB: "beads_test",
|
||||
},
|
||||
"no prefix uses default": {
|
||||
configJSON: `{"backend":"dolt"}`,
|
||||
prefix: "",
|
||||
wantDB: "beads",
|
||||
},
|
||||
"no config file falls through to prefix": {
|
||||
configJSON: "", // don't create file
|
||||
prefix: "myrig",
|
||||
wantDB: "beads_myrig",
|
||||
},
|
||||
"existing config with no prefix still wins": {
|
||||
configJSON: `{"backend":"dolt","dolt_database":"acf_beads"}`,
|
||||
prefix: "",
|
||||
wantDB: "acf_beads",
|
||||
},
|
||||
}
|
||||
|
||||
for name, tt := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create beads dir: %v", err)
|
||||
}
|
||||
|
||||
// Write metadata.json if provided
|
||||
if tt.configJSON != "" {
|
||||
configPath := filepath.Join(beadsDir, "metadata.json")
|
||||
if err := os.WriteFile(configPath, []byte(tt.configJSON), 0644); err != nil {
|
||||
t.Fatalf("failed to write config: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Simulate the guard logic from init.go:248-258
|
||||
dbName := "beads"
|
||||
if tt.prefix != "" {
|
||||
dbName = "beads_" + tt.prefix
|
||||
}
|
||||
// Guard: config wins over convention
|
||||
if existingCfg, loadErr := configfile.Load(beadsDir); loadErr == nil && existingCfg != nil {
|
||||
if existingCfg.DoltDatabase != "" {
|
||||
dbName = existingCfg.DoltDatabase
|
||||
}
|
||||
}
|
||||
|
||||
if dbName != tt.wantDB {
|
||||
t.Errorf("dbName = %q, want %q", dbName, tt.wantDB)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestMetadataSavePreservesExistingDoltDatabase verifies FR-022: when
|
||||
// metadata.json save path encounters an existing DoltDatabase value,
|
||||
// it does not overwrite it with beads_{prefix}.
|
||||
func TestMetadataSavePreservesExistingDoltDatabase(t *testing.T) {
|
||||
tests := map[string]struct {
|
||||
existingDB string
|
||||
prefix string
|
||||
wantDB string
|
||||
}{
|
||||
"existing value preserved": {
|
||||
existingDB: "custom_beads",
|
||||
prefix: "test",
|
||||
wantDB: "custom_beads",
|
||||
},
|
||||
"empty value gets prefix": {
|
||||
existingDB: "",
|
||||
prefix: "test",
|
||||
wantDB: "beads_test",
|
||||
},
|
||||
"no prefix no overwrite": {
|
||||
existingDB: "",
|
||||
prefix: "",
|
||||
wantDB: "",
|
||||
},
|
||||
}
|
||||
|
||||
for name, tt := range tests {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
cfg := &configfile.Config{
|
||||
DoltDatabase: tt.existingDB,
|
||||
}
|
||||
|
||||
// Simulate the guard logic from init.go:361
|
||||
if tt.prefix != "" && cfg.DoltDatabase == "" {
|
||||
cfg.DoltDatabase = "beads_" + tt.prefix
|
||||
}
|
||||
|
||||
if cfg.DoltDatabase != tt.wantDB {
|
||||
t.Errorf("DoltDatabase = %q, want %q", cfg.DoltDatabase, tt.wantDB)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -30,6 +30,10 @@ var initCmd = &cobra.Command{
|
|||
Long: `Initialize bd in the current directory by creating a .beads/ directory
|
||||
and database file. Optionally specify a custom issue prefix.
|
||||
|
||||
Use --database to specify an existing server database name, overriding the
|
||||
default prefix-based naming. This is useful when an external tool (e.g. gastown)
|
||||
has already created the database.
|
||||
|
||||
With --stealth: configures per-repository git settings for invisible beads usage:
|
||||
• .git/info/exclude to prevent beads files from being committed
|
||||
• Claude Code settings with bd onboard instruction
|
||||
|
|
@ -53,6 +57,14 @@ environment variable.`,
|
|||
serverHost, _ := cmd.Flags().GetString("server-host")
|
||||
serverPort, _ := cmd.Flags().GetInt("server-port")
|
||||
serverUser, _ := cmd.Flags().GetString("server-user")
|
||||
database, _ := cmd.Flags().GetString("database")
|
||||
|
||||
// Validate --database early, before any side effects
|
||||
if database != "" {
|
||||
if err := dolt.ValidateDatabaseName(database); err != nil {
|
||||
FatalError("invalid database name %q: %v", database, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Dolt is the only supported backend
|
||||
backend := configfile.BackendDolt
|
||||
|
|
@ -270,6 +282,12 @@ environment variable.`,
|
|||
} else {
|
||||
dbName = "beads"
|
||||
}
|
||||
// --database flag overrides all prefix-based naming. This allows callers
|
||||
// (e.g. gastown) to specify a pre-existing database name, preventing orphan
|
||||
// database creation when the database was already created externally.
|
||||
if database != "" {
|
||||
dbName = database
|
||||
}
|
||||
// Build config. Beads always uses dolt sql-server.
|
||||
// AutoStart is always enabled during init — we need a server to initialize the database.
|
||||
doltCfg := &dolt.Config{
|
||||
|
|
@ -366,10 +384,13 @@ environment variable.`,
|
|||
cfg.Database = "dolt"
|
||||
}
|
||||
|
||||
// Set prefix-based SQL database name to avoid cross-rig contamination (bd-u8rda).
|
||||
// Only set if not already configured — overwriting a user-renamed database
|
||||
// Set SQL database name. --database flag takes precedence over prefix-based
|
||||
// naming to avoid cross-rig contamination (bd-u8rda). Only set prefix-based
|
||||
// name if not already configured — overwriting a user-renamed database
|
||||
// creates phantom catalog entries that crash information_schema (GH#2051).
|
||||
if cfg.DoltDatabase == "" && prefix != "" {
|
||||
if database != "" {
|
||||
cfg.DoltDatabase = database
|
||||
} else if cfg.DoltDatabase == "" && prefix != "" {
|
||||
cfg.DoltDatabase = prefix
|
||||
}
|
||||
|
||||
|
|
@ -619,7 +640,7 @@ environment variable.`,
|
|||
}
|
||||
fmt.Printf(" Mode: %s\n", ui.RenderAccent("server"))
|
||||
fmt.Printf(" Server: %s\n", ui.RenderAccent(fmt.Sprintf("%s@%s:%d", user, host, port)))
|
||||
fmt.Printf(" Database: %s\n", ui.RenderAccent(storagePath))
|
||||
fmt.Printf(" Database: %s\n", ui.RenderAccent(dbName))
|
||||
fmt.Printf(" Issue prefix: %s\n", ui.RenderAccent(prefix))
|
||||
fmt.Printf(" Issues will be named: %s\n\n", ui.RenderAccent(prefix+"-<hash> (e.g., "+prefix+"-a3f2dd)"))
|
||||
fmt.Printf("Run %s to get started.\n\n", ui.RenderAccent("bd quickstart"))
|
||||
|
|
@ -667,6 +688,7 @@ func init() {
|
|||
initCmd.Flags().String("server-host", "", "Dolt server host (default: 127.0.0.1)")
|
||||
initCmd.Flags().Int("server-port", 0, "Dolt server port (default: 3307)")
|
||||
initCmd.Flags().String("server-user", "", "Dolt server MySQL user (default: root)")
|
||||
initCmd.Flags().String("database", "", "Use existing server database name (overrides prefix-based naming)")
|
||||
|
||||
rootCmd.AddCommand(initCmd)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,14 +5,17 @@ package main
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/beads"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/git"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
)
|
||||
|
|
@ -1511,3 +1514,224 @@ func TestInitDoltMetadataNoGit(t *testing.T) {
|
|||
t.Errorf("unexpected sqlite database created in dolt mode")
|
||||
}
|
||||
}
|
||||
|
||||
// buildBDOnce builds the bd binary once for subprocess tests in this file.
|
||||
// Uses sync.Once for efficiency when multiple tests need the binary.
|
||||
var (
|
||||
initTestBD string
|
||||
initTestBDOnce sync.Once
|
||||
initTestBDErr error
|
||||
)
|
||||
|
||||
func buildBDForInitTests(t *testing.T) string {
|
||||
t.Helper()
|
||||
initTestBDOnce.Do(func() {
|
||||
// Check if bd binary exists in repo root (../../bd from cmd/bd/)
|
||||
bdBinary := "bd"
|
||||
if runtime.GOOS == "windows" {
|
||||
bdBinary = "bd.exe"
|
||||
}
|
||||
repoRoot := filepath.Join("..", "..")
|
||||
existingBD := filepath.Join(repoRoot, bdBinary)
|
||||
if _, err := os.Stat(existingBD); err == nil {
|
||||
initTestBD, _ = filepath.Abs(existingBD)
|
||||
return
|
||||
}
|
||||
// Fall back to building
|
||||
tmpDir, err := os.MkdirTemp("", "bd-init-test-*")
|
||||
if err != nil {
|
||||
initTestBDErr = fmt.Errorf("failed to create temp dir: %w", err)
|
||||
return
|
||||
}
|
||||
initTestBD = filepath.Join(tmpDir, bdBinary)
|
||||
cmd := exec.Command("go", "build", "-o", initTestBD, ".")
|
||||
if out, err := cmd.CombinedOutput(); err != nil {
|
||||
initTestBDErr = fmt.Errorf("go build failed: %v\n%s", err, out)
|
||||
}
|
||||
})
|
||||
if initTestBDErr != nil {
|
||||
t.Fatalf("Failed to build bd binary: %v", initTestBDErr)
|
||||
}
|
||||
return initTestBD
|
||||
}
|
||||
|
||||
// TestInitDatabaseFlag tests the --database flag for bd init.
|
||||
// Uses subprocess execution because:
|
||||
// - init manipulates extensive Cobra global state that's difficult to reset
|
||||
// - FatalError calls os.Exit(1) for validation errors, which kills in-process tests
|
||||
//
|
||||
// Each subtest runs bd init in a temp directory and verifies metadata.json.
|
||||
func TestInitDatabaseFlag(t *testing.T) {
|
||||
skipIfNoDolt(t)
|
||||
bd := buildBDForInitTests(t)
|
||||
|
||||
t.Run("metadata_written", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Run init with --database to specify a pre-existing database name
|
||||
cmd := exec.Command(bd, "init", "--database", "myapp_production", "--quiet")
|
||||
cmd.Dir = tmpDir
|
||||
cmd.Env = append(os.Environ(), "BEADS_NO_DAEMON=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("bd init --database failed: %v\n%s", err, out)
|
||||
}
|
||||
|
||||
// Verify metadata.json contains the correct database name
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load metadata.json: %v", err)
|
||||
}
|
||||
if cfg == nil {
|
||||
t.Fatal("metadata.json not found")
|
||||
}
|
||||
|
||||
if cfg.DoltDatabase != "myapp_production" {
|
||||
t.Errorf("Expected DoltDatabase %q, got %q", "myapp_production", cfg.DoltDatabase)
|
||||
}
|
||||
if cfg.DoltMode != configfile.DoltModeServer {
|
||||
t.Errorf("Expected DoltMode %q, got %q", configfile.DoltModeServer, cfg.DoltMode)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("with_prefix", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Run init with both --database and --prefix
|
||||
// --database should override prefix for DB name, but prefix still sets issue_prefix
|
||||
cmd := exec.Command(bd, "init", "--database", "shared_db", "--prefix", "team-alpha", "--quiet")
|
||||
cmd.Dir = tmpDir
|
||||
cmd.Env = append(os.Environ(), "BEADS_NO_DAEMON=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("bd init --database --prefix failed: %v\n%s", err, out)
|
||||
}
|
||||
|
||||
// Verify metadata.json
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load metadata.json: %v", err)
|
||||
}
|
||||
if cfg == nil {
|
||||
t.Fatal("metadata.json not found")
|
||||
}
|
||||
|
||||
// --database overrides the SQL database name
|
||||
if cfg.DoltDatabase != "shared_db" {
|
||||
t.Errorf("Expected DoltDatabase %q (from --database), got %q", "shared_db", cfg.DoltDatabase)
|
||||
}
|
||||
|
||||
// Verify the database was opened and issue_prefix was set
|
||||
// by reopening the store and checking config
|
||||
dbPath := filepath.Join(beadsDir, "dolt")
|
||||
s, err := openExistingTestDB(t, dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to reopen database: %v", err)
|
||||
}
|
||||
defer s.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
issuePrefix, err := s.GetConfig(ctx, "issue_prefix")
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to get issue_prefix: %v", err)
|
||||
}
|
||||
if issuePrefix != "team-alpha" {
|
||||
t.Errorf("Expected issue_prefix %q (from --prefix), got %q", "team-alpha", issuePrefix)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("server_config_in_metadata", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Run init with --database
|
||||
cmd := exec.Command(bd, "init", "--database", "test_server_cfg", "--quiet")
|
||||
cmd.Dir = tmpDir
|
||||
cmd.Env = append(os.Environ(), "BEADS_NO_DAEMON=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("bd init --database failed: %v\n%s", err, out)
|
||||
}
|
||||
|
||||
// Verify metadata.json has both dolt_database and dolt_mode: server
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to load metadata.json: %v", err)
|
||||
}
|
||||
if cfg == nil {
|
||||
t.Fatal("metadata.json not found")
|
||||
}
|
||||
|
||||
if cfg.DoltDatabase != "test_server_cfg" {
|
||||
t.Errorf("Expected DoltDatabase %q, got %q", "test_server_cfg", cfg.DoltDatabase)
|
||||
}
|
||||
if cfg.DoltMode != configfile.DoltModeServer {
|
||||
t.Errorf("Expected DoltMode %q, got %q", configfile.DoltModeServer, cfg.DoltMode)
|
||||
}
|
||||
if cfg.Backend != configfile.BackendDolt {
|
||||
t.Errorf("Expected Backend %q, got %q", configfile.BackendDolt, cfg.Backend)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("validation_invalid_name", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Run init with an invalid database name (contains semicolon = SQL injection)
|
||||
cmd := exec.Command(bd, "init", "--database", "bad;name", "--quiet")
|
||||
cmd.Dir = tmpDir
|
||||
cmd.Env = append(os.Environ(), "BEADS_NO_DAEMON=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatal("Expected error for invalid database name, but command succeeded")
|
||||
}
|
||||
|
||||
outStr := string(out)
|
||||
if !strings.Contains(outStr, "invalid database name") {
|
||||
t.Errorf("Expected error to mention 'invalid database name', got: %s", outStr)
|
||||
}
|
||||
|
||||
// Verify no .beads directory was created (early validation prevents side effects)
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if _, err := os.Stat(beadsDir); err == nil {
|
||||
t.Error(".beads directory should not be created when validation fails")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("validation_name_with_spaces", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Database name with spaces should fail validation
|
||||
cmd := exec.Command(bd, "init", "--database", "my database", "--quiet")
|
||||
cmd.Dir = tmpDir
|
||||
cmd.Env = append(os.Environ(), "BEADS_NO_DAEMON=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatal("Expected error for database name with spaces, but command succeeded")
|
||||
}
|
||||
|
||||
outStr := string(out)
|
||||
if !strings.Contains(outStr, "invalid database name") {
|
||||
t.Errorf("Expected error to mention 'invalid database name', got: %s", outStr)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("validation_name_with_backtick", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
||||
// Database name with backtick injection should fail validation
|
||||
cmd := exec.Command(bd, "init", "--database", "db`; DROP DATABASE x; --", "--quiet")
|
||||
cmd.Dir = tmpDir
|
||||
cmd.Env = append(os.Environ(), "BEADS_NO_DAEMON=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatal("Expected error for database name with backtick injection, but command succeeded")
|
||||
}
|
||||
|
||||
outStr := string(out)
|
||||
if !strings.Contains(outStr, "invalid database name") {
|
||||
t.Errorf("Expected error to mention 'invalid database name', got: %s", outStr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
module bd-example-extension-go
|
||||
|
||||
go 1.24.0
|
||||
go 1.25.6
|
||||
|
||||
require github.com/steveyegge/beads v0.0.0-00010101000000-000000000000
|
||||
|
||||
|
|
@ -11,7 +11,7 @@ require (
|
|||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
|
||||
golang.org/x/sys v0.34.0 // indirect
|
||||
golang.org/x/sys v0.41.0 // indirect
|
||||
modernc.org/libc v1.66.3 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.11.0 // indirect
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
|||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
|
||||
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=
|
||||
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=
|
||||
modernc.org/cc/v4 v4.26.2 h1:991HMkLjJzYBIfha6ECZdjrIYz2/1ayr+FL8GN+CNzM=
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
module example.com/beads-library-demo
|
||||
|
||||
go 1.24.0
|
||||
go 1.25.6
|
||||
|
||||
require github.com/steveyegge/beads v0.0.0
|
||||
|
||||
|
|
@ -11,7 +11,7 @@ require (
|
|||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
|
||||
golang.org/x/sys v0.36.0 // indirect
|
||||
golang.org/x/sys v0.41.0 // indirect
|
||||
modernc.org/libc v1.66.3 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.11.0 // indirect
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
|||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
|
||||
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/sys v0.41.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/tools v0.37.0 h1:DVSRzp7FwePZW356yEAChSdNcQo6Nsp+fex1SUW09lE=
|
||||
golang.org/x/tools v0.37.0/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w=
|
||||
modernc.org/cc/v4 v4.26.2 h1:991HMkLjJzYBIfha6ECZdjrIYz2/1ayr+FL8GN+CNzM=
|
||||
|
|
|
|||
|
|
@ -1544,9 +1544,9 @@ func TestValidateDatabaseName(t *testing.T) {
|
|||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
err := validateDatabaseName(tt.dbName)
|
||||
err := ValidateDatabaseName(tt.dbName)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("validateDatabaseName(%q) error = %v, wantErr %v", tt.dbName, err, tt.wantErr)
|
||||
t.Errorf("ValidateDatabaseName(%q) error = %v, wantErr %v", tt.dbName, err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,9 +34,9 @@ func validateRef(ref string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
// validateDatabaseName checks if a database name is safe to use in queries.
|
||||
// ValidateDatabaseName checks if a database name is safe to use in queries.
|
||||
// Prevents SQL injection via backtick escaping in CREATE DATABASE statements.
|
||||
func validateDatabaseName(name string) error {
|
||||
func ValidateDatabaseName(name string) error {
|
||||
if name == "" {
|
||||
return fmt.Errorf("database name cannot be empty")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -633,11 +633,11 @@ func openServerConnection(ctx context.Context, cfg *Config) (*sql.DB, string, er
|
|||
defer func() { _ = initDB.Close() }()
|
||||
|
||||
// Validate database name to prevent SQL injection via backtick escaping
|
||||
if err := validateDatabaseName(cfg.Database); err != nil {
|
||||
if err := ValidateDatabaseName(cfg.Database); err != nil {
|
||||
_ = db.Close()
|
||||
return nil, "", fmt.Errorf("invalid database name %q: %w", cfg.Database, err)
|
||||
}
|
||||
_, err = initDB.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", cfg.Database)) //nolint:gosec // G201: cfg.Database validated by validateDatabaseName above
|
||||
_, err = initDB.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", cfg.Database)) //nolint:gosec // G201: cfg.Database validated by ValidateDatabaseName above
|
||||
if err != nil {
|
||||
// Dolt may return error 1007 even with IF NOT EXISTS - ignore if database already exists
|
||||
errLower := strings.ToLower(err.Error())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue