Merge pull request #4881 from gastownhall/chore/simplify-storage-backends-v2
Simplify the supported backends: consolidate storage on Dolt
This commit is contained in:
commit
8dc1998214
163 changed files with 3855 additions and 13408 deletions
|
|
@ -29,7 +29,7 @@
|
|||
|
||||
# Test specific package
|
||||
./scripts/test.sh ./cmd/bd/...
|
||||
./scripts/test.sh ./internal/storage/sqlite/...
|
||||
./scripts/test.sh ./internal/storage/embeddeddolt/...
|
||||
|
||||
# Test specific feature
|
||||
./scripts/test.sh -run TestCreate ./cmd/bd/...
|
||||
|
|
|
|||
63
.github/workflows/conformance.yml
vendored
63
.github/workflows/conformance.yml
vendored
|
|
@ -1,12 +1,7 @@
|
|||
name: Conformance
|
||||
|
||||
# Runs the two-tier storage-backend conformance suite against REAL databases —
|
||||
# the same scripts/conformance.sh developers run locally, so local == CI. Tier 1
|
||||
# is the in-process store suite plus the wedge gates; Tier 2 is the end-to-end
|
||||
# `bd init` + CLI differential (every SQL backend compared to the Dolt reference).
|
||||
# Postgres and MySQL run as service containers; Dolt (embedded) and SQLite are
|
||||
# in-process. The deep bts-rs 523-scenario oracle stays on-demand (CONFORMANCE_DEEP),
|
||||
# not in this per-PR job.
|
||||
# Runs the same two-tier storage conformance suite developers run locally:
|
||||
# the in-process store corpus and the real-binary CLI harness.
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
|
|
@ -23,40 +18,8 @@ env:
|
|||
|
||||
jobs:
|
||||
conformance:
|
||||
name: Storage backend conformance (real DBs)
|
||||
name: Storage backend conformance (embedded Dolt oracle)
|
||||
runs-on: ubuntu-latest
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16
|
||||
env:
|
||||
POSTGRES_USER: bts
|
||||
POSTGRES_PASSWORD: bts
|
||||
POSTGRES_DB: bdwedge
|
||||
# Code-point collation parity with Dolt (utf8mb4_0900_bin). The pg backend
|
||||
# provisions per-workspace SCHEMAS inside this one database and can't set
|
||||
# per-column COLLATE "C" (it breaks the shared recursive CTEs; see
|
||||
# internal/storage/postgres/schema.go), so byte-order parity has to be
|
||||
# guaranteed at DB-creation time via a C-locale cluster.
|
||||
POSTGRES_INITDB_ARGS: "--lc-collate=C --lc-ctype=C"
|
||||
ports:
|
||||
- 5432:5432
|
||||
options: >-
|
||||
--health-cmd "pg_isready -U bts -d bdwedge"
|
||||
--health-interval 5s
|
||||
--health-timeout 5s
|
||||
--health-retries 20
|
||||
mysql:
|
||||
image: mysql:8
|
||||
env:
|
||||
MYSQL_ROOT_PASSWORD: bts
|
||||
MYSQL_DATABASE: bdwedge
|
||||
ports:
|
||||
- 3306:3306
|
||||
options: >-
|
||||
--health-cmd "mysqladmin ping -uroot -pbts --silent"
|
||||
--health-interval 5s
|
||||
--health-timeout 5s
|
||||
--health-retries 30
|
||||
steps:
|
||||
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v6
|
||||
|
||||
|
|
@ -65,23 +28,5 @@ jobs:
|
|||
with:
|
||||
go-version-file: 'go.mod'
|
||||
|
||||
# The MySQL image's root only auths from localhost inside the container; the
|
||||
# backend connects over TCP as a distinct host, so create a TCP-reachable user
|
||||
# matching BEADS_MYSQL_TEST_URL.
|
||||
- name: Provision MySQL user
|
||||
run: |
|
||||
mysql -h 127.0.0.1 -P 3306 -uroot -pbts -e "
|
||||
CREATE USER IF NOT EXISTS 'bts'@'%' IDENTIFIED BY 'bts';
|
||||
GRANT ALL PRIVILEGES ON *.* TO 'bts'@'%' WITH GRANT OPTION;
|
||||
FLUSH PRIVILEGES;"
|
||||
|
||||
- name: Run conformance (Dolt + Postgres + MySQL + SQLite)
|
||||
env:
|
||||
BEADS_PG_TEST_URL: "postgres://bts:bts@127.0.0.1:5432/bdwedge"
|
||||
BEADS_PG_PASSWORD: "bts"
|
||||
BEADS_MYSQL_TEST_URL: "bts:bts@tcp(127.0.0.1:3306)/"
|
||||
# Hard-fail (Tier 1 run_sql_conformance + Tier 2 e2e) if either real backend
|
||||
# is absent, so removing/renaming a *_TEST_URL can't silently drop coverage
|
||||
# while the gate still reports green.
|
||||
BEADS_CONFORMANCE_REQUIRE: "postgres,mysql"
|
||||
- name: Run conformance (embedded Dolt oracle)
|
||||
run: ./scripts/conformance.sh
|
||||
|
|
|
|||
|
|
@ -158,6 +158,14 @@ linters:
|
|||
linters:
|
||||
- gosec
|
||||
text: "G705"
|
||||
# G705: XSS via taint in CLI progress/warning/error output — every flagged
|
||||
# sink is fmt.Fprint* to the terminal (os.Stderr or cobra's stdout/stderr
|
||||
# writers), never a browser context (surfaced by the gosec bundled with
|
||||
# golangci-lint v2.10)
|
||||
- path: 'cmd/bd/(ado|doctor|errors|github|gitlab|hooks|jira|linear|main|notion|sync_push_pull)\.go'
|
||||
linters:
|
||||
- gosec
|
||||
text: "G705"
|
||||
# errcheck: fmt.Fprintf errors are noise in CLI help text generation
|
||||
- path: 'cmd/bd/help_all\.go'
|
||||
linters:
|
||||
|
|
@ -174,11 +182,6 @@ linters:
|
|||
linters:
|
||||
- unparam
|
||||
text: 'reimportConflicts|resolveConflictsByTimestamp'
|
||||
# unparam: readOnly param kept for API symmetry with fix/sqlite_open.go
|
||||
- path: 'cmd/bd/doctor/sqlite_open\.go'
|
||||
linters:
|
||||
- unparam
|
||||
text: 'readOnly'
|
||||
# unparam: childrenMap return computed for future parent-child display
|
||||
- path: 'cmd/bd/list\.go'
|
||||
linters:
|
||||
|
|
|
|||
12
CHANGELOG.md
12
CHANGELOG.md
|
|
@ -9,6 +9,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
### Changed
|
||||
|
||||
- **Storage backend scope simplified** (bd-sadcd). The recently merged direct
|
||||
PostgreSQL and MySQL adapters have been rolled back before entering a tagged
|
||||
release. Supporting additional general-purpose server databases introduced
|
||||
dialect, credential, schema-lifecycle, migration, CI, and operational
|
||||
complexity at odds with our goal of keeping Beads as simple as possible and
|
||||
consuming as few resources as possible. The storage interface, shared issue
|
||||
core, SQLite implementation, and conformance harness remain; embedded Dolt,
|
||||
Dolt server, and SQLite are the supported storage paths. Dolt server mode and
|
||||
its use of the MySQL wire protocol are unchanged. Existing PostgreSQL or
|
||||
MySQL workspaces stop before their configured databases are opened or
|
||||
modified; see the [migration
|
||||
guide](docs/architecture/storage-backends.md#existing-postgresql-or-mysql-workspaces).
|
||||
- **Cross-type blocking dependencies are now allowed** (bd-wg7ve,
|
||||
[#4034](https://github.com/gastownhall/beads/pull/4034)).
|
||||
`bd dep add <task> <epic>` — gating a work item on an epic (program)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
# PROPOSAL: Pluggable Storage Backends
|
||||
|
||||
**Status:** Draft for review (survived one adversarial review round; defects folded in)
|
||||
**Status:** Historical — partially implemented, then superseded on 2026-07-16
|
||||
**Date:** 2026-07-02
|
||||
**Informed by:** the bts-rs Rust spike (a from-scratch Rust reimplementation
|
||||
of the gc-contract surface that validated a backend-agnostic storage seam at 299/299 byte-parity
|
||||
|
|
@ -8,6 +8,21 @@ across two radically different backends), a five-dimension recon of this repo's
|
|||
architecture, and a two-reviewer adversarial verification pass. All file:line citations verified
|
||||
against `worktree-beads-new`.
|
||||
|
||||
> **Superseded scope:** This proposal is retained as the design record for the
|
||||
> storage interface, shared issue core, SQLite adapter, and differential
|
||||
> conformance work that grew from it. Those pieces remain. The direct
|
||||
> PostgreSQL and MySQL adapters were rolled back before entering a tagged
|
||||
> release because supporting additional general-purpose server databases adds
|
||||
> substantial dialect, credential, schema-lifecycle, migration, CI, and
|
||||
> operational complexity. Our goal is to keep Beads as simple as possible and
|
||||
> consume as few resources as possible. The supported paths are now embedded
|
||||
> Dolt, Dolt server, and SQLite; Dolt server's use of the MySQL wire protocol is
|
||||
> unchanged. See [Storage Backends](docs/architecture/storage-backends.md) for
|
||||
> the current design.
|
||||
|
||||
Everything below this notice is retained as historical design material, not as
|
||||
current backend support or migration guidance.
|
||||
|
||||
---
|
||||
|
||||
## 1. End state
|
||||
|
|
|
|||
16
beads.go
16
beads.go
|
|
@ -11,6 +11,7 @@ import (
|
|||
"context"
|
||||
|
||||
"github.com/steveyegge/beads/internal/beads"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
|
|
@ -19,6 +20,15 @@ import (
|
|||
// Storage is the interface for beads storage operations
|
||||
type Storage = beads.Storage
|
||||
|
||||
func configuredBackendUnavailable(backend string) error {
|
||||
switch backend {
|
||||
case configfile.BackendPostgres, configfile.BackendMySQL, configfile.BackendSQLite:
|
||||
return configfile.RemovedBackendError(backend)
|
||||
default:
|
||||
return configfile.UnknownBackendError(backend)
|
||||
}
|
||||
}
|
||||
|
||||
// Transaction provides atomic multi-operation support within a database transaction.
|
||||
// Use Storage.RunInTransaction() to obtain a Transaction instance.
|
||||
type Transaction = beads.Transaction
|
||||
|
|
@ -64,9 +74,9 @@ func Open(ctx context.Context, dbPath string) (Storage, error) {
|
|||
return dolt.New(ctx, &dolt.Config{Path: dbPath, CreateIfMissing: true})
|
||||
}
|
||||
|
||||
// OpenFromConfig opens a beads database using configuration from metadata.json.
|
||||
// Unlike Open, this respects Dolt server mode settings and database name
|
||||
// configuration, connecting to the Dolt SQL server when dolt_mode is "server".
|
||||
// OpenFromConfig opens the Dolt implementation using configuration from
|
||||
// metadata.json. Unlike Open, this respects Dolt server mode settings and database
|
||||
// name configuration.
|
||||
// beadsDir is the path to the .beads directory.
|
||||
func OpenFromConfig(ctx context.Context, beadsDir string) (Storage, error) {
|
||||
return dolt.NewFromConfigWithOptions(ctx, beadsDir, &dolt.Config{CreateIfMissing: true})
|
||||
|
|
|
|||
17
beads_cgo.go
17
beads_cgo.go
|
|
@ -4,6 +4,7 @@ package beads
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
|
|
@ -14,15 +15,25 @@ import (
|
|||
// for the given .beads directory. It reads metadata.json to determine the
|
||||
// configured mode:
|
||||
//
|
||||
// - Embedded mode (default): Opens via the CGo embedded Dolt engine.
|
||||
// - Server mode: Connects to an external dolt sql-server via OpenFromConfig.
|
||||
// - Embedded Dolt (default): Opens via the CGo embedded Dolt engine.
|
||||
// - Dolt server: Connects to a dolt sql-server via OpenFromConfig.
|
||||
//
|
||||
// The returned Storage must be closed when no longer needed.
|
||||
//
|
||||
// beadsDir is the path to the .beads directory.
|
||||
func OpenBestAvailable(ctx context.Context, beadsDir string) (Storage, error) {
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err == nil && cfg != nil && cfg.IsDoltServerMode() {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loading storage metadata: %w", err)
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
if !configfile.IsSupportedBackend(cfg.Backend) {
|
||||
return nil, configuredBackendUnavailable(cfg.Backend)
|
||||
}
|
||||
|
||||
if cfg.IsDoltServerMode() {
|
||||
store, err := dolt.NewFromConfig(ctx, beadsDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
|
|
@ -11,13 +11,23 @@ import (
|
|||
)
|
||||
|
||||
// OpenBestAvailable opens a beads database using the best available backend
|
||||
// for the given .beads directory. In non-CGO builds, only server mode is
|
||||
// supported; embedded mode returns an error directing the user to server mode.
|
||||
// for the given .beads directory. In non-CGO builds, only Dolt server mode is
|
||||
// supported; embedded Dolt returns an error directing the user to server mode.
|
||||
//
|
||||
// beadsDir is the path to the .beads directory.
|
||||
func OpenBestAvailable(ctx context.Context, beadsDir string) (Storage, error) {
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err == nil && cfg != nil && cfg.IsDoltServerMode() {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loading storage metadata: %w", err)
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
if !configfile.IsSupportedBackend(cfg.Backend) {
|
||||
return nil, configuredBackendUnavailable(cfg.Backend)
|
||||
}
|
||||
|
||||
if cfg.IsDoltServerMode() {
|
||||
store, err := dolt.NewFromConfig(ctx, beadsDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
|||
45
cmd/bd/backend_support.go
Normal file
45
cmd/bd/backend_support.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
)
|
||||
|
||||
func validateConfiguredBackend(cfg *configfile.Config) error {
|
||||
if cfg == nil {
|
||||
return nil
|
||||
}
|
||||
switch cfg.Backend {
|
||||
case configfile.BackendPostgres, configfile.BackendMySQL, configfile.BackendSQLite:
|
||||
return configfile.RemovedBackendError(cfg.Backend)
|
||||
case "", configfile.BackendDolt:
|
||||
return nil
|
||||
default:
|
||||
return configfile.UnknownBackendError(cfg.Backend)
|
||||
}
|
||||
}
|
||||
|
||||
func requireDoltBackend(cfg *configfile.Config) error {
|
||||
if err := validateConfiguredBackend(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
if cfg != nil && cfg.GetBackend() != configfile.BackendDolt {
|
||||
return fmt.Errorf("not using Dolt backend (configured backend %q)", cfg.GetBackend())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadDoltBackendConfig(beadsDir string) (*configfile.Config, error) {
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("loading config: %w", err)
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
if err := requireDoltBackend(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
|
@ -191,12 +191,21 @@ Examples:
|
|||
// name (e.g. dolt_database). Without this, server-mode rigs get the
|
||||
// default name "beads" instead of their configured name. (GH#3029)
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil || cfg == nil {
|
||||
cfg = findParentConfig(beadsDir)
|
||||
if err != nil {
|
||||
return HandleError("failed to load %s: %v; no storage database was opened or modified; fix or restore metadata.json and retry", configfile.ConfigPath(beadsDir), err)
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg, err = findParentConfig(beadsDir)
|
||||
if err != nil {
|
||||
return HandleError("failed to load ancestor storage metadata: %v; no storage database was opened or modified; fix or restore metadata.json and retry", err)
|
||||
}
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
if err := requireBootstrapDoltBackend(cfg); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
|
||||
resolvedCfg, repairMsg, err := applyBootstrapMetadataRepair(beadsDir, cfg, !dryRun)
|
||||
if err != nil {
|
||||
|
|
@ -270,6 +279,13 @@ func noWorkspaceBootstrapPayload() map[string]interface{} {
|
|||
}
|
||||
}
|
||||
|
||||
func requireBootstrapDoltBackend(cfg *configfile.Config) error {
|
||||
if err := validateConfiguredBackend(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func detectBootstrapAction(beadsDir string, cfg *configfile.Config) BootstrapPlan {
|
||||
plan := BootstrapPlan{
|
||||
BeadsDir: beadsDir,
|
||||
|
|
@ -513,6 +529,9 @@ func confirmPrompt(message string, nonInteractive bool) bool {
|
|||
}
|
||||
|
||||
func executeBootstrapPlan(plan BootstrapPlan, cfg *configfile.Config, nonInteractive bool) error {
|
||||
if err := requireBootstrapDoltBackend(cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
if !confirmPrompt("Proceed?", nonInteractive) {
|
||||
fmt.Fprintf(os.Stderr, "Aborted.\n")
|
||||
return nil
|
||||
|
|
@ -955,8 +974,10 @@ func isNonInteractiveBootstrap(flagValue bool) bool {
|
|||
// findParentConfig walks up from beadsDir's parent looking for a
|
||||
// .beads/metadata.json in ancestor directories. This handles the case where a
|
||||
// rig subdirectory (its own git repo) doesn't have a local .beads but its
|
||||
// parent workspace does. Returns nil if no parent config is found.
|
||||
func findParentConfig(beadsDir string) *configfile.Config {
|
||||
// parent workspace does. Returns nil if no parent config is found. A malformed
|
||||
// or unreadable ancestor metadata file is authoritative and returned as an error;
|
||||
// bootstrap must not skip it and select a more distant workspace or defaults.
|
||||
func findParentConfig(beadsDir string) (*configfile.Config, error) {
|
||||
// Start from the parent of beadsDir's enclosing directory.
|
||||
// beadsDir is typically "<project>/.beads", so we start from <project>'s parent.
|
||||
start := filepath.Dir(filepath.Dir(beadsDir))
|
||||
|
|
@ -964,8 +985,12 @@ func findParentConfig(beadsDir string) *configfile.Config {
|
|||
|
||||
for dir := start; dir != "/" && dir != "."; {
|
||||
candidate := filepath.Join(dir, ".beads")
|
||||
if cfg, err := configfile.Load(candidate); err == nil && cfg != nil {
|
||||
return cfg
|
||||
cfg, err := configfile.Load(candidate)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", configfile.ConfigPath(candidate), err)
|
||||
}
|
||||
if cfg != nil {
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// Don't search above $HOME
|
||||
|
|
@ -979,7 +1004,7 @@ func findParentConfig(beadsDir string) *configfile.Config {
|
|||
}
|
||||
dir = parent
|
||||
}
|
||||
return nil
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
|
|||
295
cmd/bd/bootstrap_backend_guard_test.go
Normal file
295
cmd/bd/bootstrap_backend_guard_test.go
Normal file
|
|
@ -0,0 +1,295 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
)
|
||||
|
||||
func TestBootstrapRejectsRemovedBackendsBeforeWorkspaceWrites(t *testing.T) {
|
||||
bd := buildBDForInitTests(t)
|
||||
|
||||
for _, backend := range []string{configfile.BackendPostgres, configfile.BackendMySQL, configfile.BackendSQLite} {
|
||||
backend := backend
|
||||
for _, args := range [][]string{{"bootstrap", "--dry-run"}, {"bootstrap", "--yes"}} {
|
||||
args := args
|
||||
mode := "execute"
|
||||
if len(args) > 1 && args[1] == "--dry-run" {
|
||||
mode = "dry-run"
|
||||
}
|
||||
|
||||
t.Run(backend+"/"+mode, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
beadsDir := filepath.Join(root, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
||||
t.Fatalf("create .beads: %v", err)
|
||||
}
|
||||
|
||||
metadata := []byte(fmt.Sprintf("{\n \"database\": \"legacy.db\",\n \"backend\": %q,\n \"project_id\": \"legacy-project\"\n}\n", backend))
|
||||
metadataPath := filepath.Join(beadsDir, configfile.ConfigFileName)
|
||||
if err := os.WriteFile(metadataPath, metadata, 0o600); err != nil {
|
||||
t.Fatalf("write metadata.json: %v", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(bd, args...)
|
||||
cmd.Dir = root
|
||||
cmd.Env = bootstrapBackendGuardEnv(root, beadsDir)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Errorf("bd %s unexpectedly succeeded for removed backend %q:\n%s", strings.Join(args, " "), backend, out)
|
||||
}
|
||||
|
||||
message := strings.ToLower(string(out))
|
||||
for _, want := range removedBackendWantSubstrings(backend) {
|
||||
if !strings.Contains(message, want) {
|
||||
t.Errorf("bd %s error for %q missing %q:\n%s", strings.Join(args, " "), backend, want, message)
|
||||
}
|
||||
}
|
||||
|
||||
after, readErr := os.ReadFile(metadataPath)
|
||||
if readErr != nil {
|
||||
t.Fatalf("read metadata.json after rejected bootstrap: %v", readErr)
|
||||
}
|
||||
if !bytes.Equal(after, metadata) {
|
||||
t.Errorf("bd %s rewrote metadata.json for removed backend %q:\nbefore:\n%s\nafter:\n%s", strings.Join(args, " "), backend, metadata, after)
|
||||
}
|
||||
assertNoBootstrapStorageArtifacts(t, beadsDir)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapRejectsUnknownBackendBeforeWorkspaceWrites(t *testing.T) {
|
||||
bd := buildBDForInitTests(t)
|
||||
|
||||
for _, args := range [][]string{{"bootstrap", "--dry-run"}, {"bootstrap", "--yes"}} {
|
||||
mode := "execute"
|
||||
if args[1] == "--dry-run" {
|
||||
mode = "dry-run"
|
||||
}
|
||||
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
beadsDir := filepath.Join(root, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
||||
t.Fatalf("create .beads: %v", err)
|
||||
}
|
||||
|
||||
metadata := []byte("{\n \"backend\": \"mystery\",\n \"project_id\": \"legacy-project\"\n}\n")
|
||||
metadataPath := filepath.Join(beadsDir, configfile.ConfigFileName)
|
||||
if err := os.WriteFile(metadataPath, metadata, 0o600); err != nil {
|
||||
t.Fatalf("write metadata.json: %v", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(bd, args...)
|
||||
cmd.Dir = root
|
||||
cmd.Env = bootstrapBackendGuardEnv(root, beadsDir)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Errorf("bd %s unexpectedly accepted an unknown backend:\n%s", strings.Join(args, " "), out)
|
||||
}
|
||||
message := string(out)
|
||||
for _, want := range []string{"not recognized", "no storage database was opened or modified", "dolt"} {
|
||||
if !strings.Contains(message, want) {
|
||||
t.Errorf("bd %s error missing %q:\n%s", strings.Join(args, " "), want, message)
|
||||
}
|
||||
}
|
||||
|
||||
after, readErr := os.ReadFile(metadataPath)
|
||||
if readErr != nil {
|
||||
t.Fatalf("read metadata.json after rejected bootstrap: %v", readErr)
|
||||
}
|
||||
if !bytes.Equal(after, metadata) {
|
||||
t.Errorf("bd %s rewrote unknown-backend metadata:\nbefore:\n%s\nafter:\n%s", strings.Join(args, " "), metadata, after)
|
||||
}
|
||||
assertNoBootstrapStorageArtifacts(t, beadsDir)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapRejectsCorruptMetadataBeforeWorkspaceWrites(t *testing.T) {
|
||||
bd := buildBDForInitTests(t)
|
||||
|
||||
for _, args := range [][]string{{"bootstrap", "--dry-run"}, {"bootstrap", "--yes"}} {
|
||||
mode := "execute"
|
||||
if args[1] == "--dry-run" {
|
||||
mode = "dry-run"
|
||||
}
|
||||
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
beadsDir := filepath.Join(root, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
||||
t.Fatalf("create .beads: %v", err)
|
||||
}
|
||||
|
||||
metadata := []byte("{\n")
|
||||
metadataPath := filepath.Join(beadsDir, configfile.ConfigFileName)
|
||||
if err := os.WriteFile(metadataPath, metadata, 0o600); err != nil {
|
||||
t.Fatalf("write corrupt metadata.json: %v", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(bd, args...)
|
||||
cmd.Dir = root
|
||||
cmd.Env = bootstrapBackendGuardEnv(root, beadsDir)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatalf("bd %s unexpectedly ignored corrupt metadata:\n%s", strings.Join(args, " "), out)
|
||||
}
|
||||
message := strings.ToLower(string(out))
|
||||
for _, want := range []string{"metadata.json", "no storage database was opened or modified"} {
|
||||
if !strings.Contains(message, want) {
|
||||
t.Errorf("bd %s error missing %q:\n%s", strings.Join(args, " "), want, out)
|
||||
}
|
||||
}
|
||||
|
||||
after, readErr := os.ReadFile(metadataPath)
|
||||
if readErr != nil {
|
||||
t.Fatalf("read corrupt metadata after rejected bootstrap: %v", readErr)
|
||||
}
|
||||
if !bytes.Equal(after, metadata) {
|
||||
t.Errorf("bd %s rewrote corrupt metadata:\nbefore: %q\nafter: %q", strings.Join(args, " "), metadata, after)
|
||||
}
|
||||
assertNoBootstrapStorageArtifacts(t, beadsDir)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapDoesNotConvertExistingSQLiteWorkspace(t *testing.T) {
|
||||
bd := buildBDForInitTests(t)
|
||||
|
||||
for _, args := range [][]string{{"bootstrap", "--dry-run"}, {"bootstrap", "--yes"}} {
|
||||
args := args
|
||||
mode := "execute"
|
||||
if len(args) > 1 && args[1] == "--dry-run" {
|
||||
mode = "dry-run"
|
||||
}
|
||||
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
for _, gitArgs := range [][]string{
|
||||
{"init", "-q"},
|
||||
{"config", "user.email", "test@test.com"},
|
||||
{"config", "user.name", "Test"},
|
||||
{"config", "core.hooksPath", ".git/hooks"},
|
||||
} {
|
||||
runGitForBootstrapTest(t, root, gitArgs...)
|
||||
}
|
||||
|
||||
// Hand-seed a workspace created by an earlier build's SQLite backend:
|
||||
// metadata selecting the removed backend plus its database file. The
|
||||
// guard must reject it without reading or rewriting either.
|
||||
beadsDir := filepath.Join(root, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
||||
t.Fatalf("create .beads: %v", err)
|
||||
}
|
||||
metadataPath := filepath.Join(beadsDir, configfile.ConfigFileName)
|
||||
metadataBefore := []byte("{\n \"database\": \"beads.db\",\n \"backend\": \"sqlite\",\n \"sqlite_path\": \"beads.db\",\n \"project_id\": \"sqlite-guard\"\n}\n")
|
||||
if err := os.WriteFile(metadataPath, metadataBefore, 0o600); err != nil {
|
||||
t.Fatalf("write metadata.json: %v", err)
|
||||
}
|
||||
databasePath := filepath.Join(beadsDir, "beads.db")
|
||||
databaseContent := []byte("sqlite-guard database placeholder")
|
||||
if err := os.WriteFile(databasePath, databaseContent, 0o600); err != nil {
|
||||
t.Fatalf("write beads.db: %v", err)
|
||||
}
|
||||
databaseBefore := sha256.Sum256(databaseContent)
|
||||
localVersionBefore, hadLocalVersion := readOptionalBootstrapGuardFile(t, filepath.Join(beadsDir, ".local_version"))
|
||||
|
||||
cmd := exec.Command(bd, args...)
|
||||
cmd.Dir = root
|
||||
cmd.Env = bootstrapBackendGuardEnv(root, beadsDir)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Errorf("bd %s unexpectedly accepted an existing SQLite workspace:\n%s", strings.Join(args, " "), out)
|
||||
}
|
||||
|
||||
message := strings.ToLower(string(out))
|
||||
if !strings.Contains(message, "sqlite") {
|
||||
t.Errorf("bd %s should name the rejected backend:\n%s", strings.Join(args, " "), out)
|
||||
}
|
||||
for _, want := range removedBackendWantSubstrings(configfile.BackendSQLite) {
|
||||
if !strings.Contains(message, want) {
|
||||
t.Errorf("bd %s error missing %q:\n%s", strings.Join(args, " "), want, out)
|
||||
}
|
||||
}
|
||||
|
||||
metadataAfter := readBootstrapGuardFile(t, metadataPath)
|
||||
if !bytes.Equal(metadataAfter, metadataBefore) {
|
||||
t.Errorf("bd %s rewrote SQLite metadata.json:\nbefore:\n%s\nafter:\n%s", strings.Join(args, " "), metadataBefore, metadataAfter)
|
||||
}
|
||||
databaseAfter := sha256.Sum256(readBootstrapGuardFile(t, databasePath))
|
||||
if databaseAfter != databaseBefore {
|
||||
t.Errorf("bd %s modified the existing SQLite database", strings.Join(args, " "))
|
||||
}
|
||||
localVersionAfter, hasLocalVersion := readOptionalBootstrapGuardFile(t, filepath.Join(beadsDir, ".local_version"))
|
||||
if hasLocalVersion != hadLocalVersion || !bytes.Equal(localVersionAfter, localVersionBefore) {
|
||||
t.Errorf("bd %s created or modified SQLite workspace version tracking", strings.Join(args, " "))
|
||||
}
|
||||
for _, name := range []string{"embeddeddolt", "dolt"} {
|
||||
path := filepath.Join(beadsDir, name)
|
||||
if _, statErr := os.Stat(path); !os.IsNotExist(statErr) {
|
||||
t.Errorf("bd %s converted SQLite by creating Dolt state at %s (stat error: %v)", strings.Join(args, " "), path, statErr)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func bootstrapBackendGuardEnv(home, beadsDir string) []string {
|
||||
env := make([]string, 0, len(os.Environ())+7)
|
||||
for _, entry := range os.Environ() {
|
||||
if strings.HasPrefix(entry, "BEADS_") || strings.HasPrefix(entry, "BD_") || strings.HasPrefix(entry, "HOME=") {
|
||||
continue
|
||||
}
|
||||
env = append(env, entry)
|
||||
}
|
||||
return append(env,
|
||||
"HOME="+home,
|
||||
"BEADS_DIR="+beadsDir,
|
||||
"BEADS_DOLT_AUTO_START=0",
|
||||
"BEADS_NO_DAEMON=1",
|
||||
"BEADS_TEST_IGNORE_REPO_CONFIG=1",
|
||||
"BD_DISABLE_METRICS=1",
|
||||
"BD_DISABLE_EVENT_FLUSH=1",
|
||||
"BD_NON_INTERACTIVE=1",
|
||||
)
|
||||
}
|
||||
|
||||
func assertNoBootstrapStorageArtifacts(t *testing.T, beadsDir string) {
|
||||
t.Helper()
|
||||
for _, name := range []string{"embeddeddolt", "dolt", "beads.db", ".local_version"} {
|
||||
path := filepath.Join(beadsDir, name)
|
||||
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||
t.Errorf("rejected bootstrap created local state at %s (stat error: %v)", path, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readBootstrapGuardFile(t *testing.T, path string) []byte {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v", path, err)
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
func readOptionalBootstrapGuardFile(t *testing.T, path string) ([]byte, bool) {
|
||||
t.Helper()
|
||||
data, err := os.ReadFile(path)
|
||||
if os.IsNotExist(err) {
|
||||
return nil, false
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v", path, err)
|
||||
}
|
||||
return data, true
|
||||
}
|
||||
|
|
@ -919,7 +919,10 @@ func TestBootstrapRigSubdirUsesParentDBName(t *testing.T) {
|
|||
cfg, cfgErr := configfile.Load(synthesizedDir)
|
||||
if cfgErr != nil || cfg == nil {
|
||||
// This is the fix path: search parent directories for metadata.json
|
||||
cfg = findParentConfig(synthesizedDir)
|
||||
cfg, cfgErr = findParentConfig(synthesizedDir)
|
||||
if cfgErr != nil {
|
||||
t.Fatalf("findParentConfig: %v", cfgErr)
|
||||
}
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
|
|
@ -939,6 +942,37 @@ func TestBootstrapRigSubdirUsesParentDBName(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestFindParentConfigDoesNotSkipCorruptNearestAncestor(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
higherBeadsDir := filepath.Join(root, ".beads")
|
||||
if err := os.MkdirAll(higherBeadsDir, 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(higherBeadsDir, "metadata.json"), []byte(`{"dolt_database":"wrong_higher_database"}`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
nearestBeadsDir := filepath.Join(root, "workspace", ".beads")
|
||||
if err := os.MkdirAll(nearestBeadsDir, 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
corrupt := []byte("{\n")
|
||||
metadataPath := filepath.Join(nearestBeadsDir, "metadata.json")
|
||||
if err := os.WriteFile(metadataPath, corrupt, 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
synthesizedDir := filepath.Join(root, "workspace", "rig", ".beads")
|
||||
if cfg, err := findParentConfig(synthesizedDir); err == nil {
|
||||
t.Fatalf("findParentConfig skipped corrupt nearest metadata and selected higher database %q", cfg.GetDoltDatabase())
|
||||
} else if !strings.Contains(err.Error(), filepath.Join("workspace", ".beads", "metadata.json")) {
|
||||
t.Fatalf("findParentConfig error = %v, want nearest metadata path", err)
|
||||
}
|
||||
if after, err := os.ReadFile(metadataPath); err != nil || string(after) != string(corrupt) {
|
||||
t.Fatalf("corrupt nearest metadata changed: got %q, err %v", after, err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestDetectBootstrapAction_SharedServerEnvUsesSharedPath verifies that when
|
||||
// BEADS_DOLT_SHARED_SERVER=1 is set but cfg.DoltMode is the default (embedded),
|
||||
// detectBootstrapAction looks in the shared-server directory — not embeddeddolt/.
|
||||
|
|
@ -1052,7 +1086,10 @@ func TestDetectBootstrapAction_WorktreeSynthesizedDirPrefersSyncOverDefaultShare
|
|||
synthesizedDir := filepath.Join(localBare, ".beads")
|
||||
cfg, cfgErr := configfile.Load(synthesizedDir)
|
||||
if cfgErr != nil || cfg == nil {
|
||||
cfg = findParentConfig(synthesizedDir)
|
||||
cfg, cfgErr = findParentConfig(synthesizedDir)
|
||||
if cfgErr != nil {
|
||||
t.Fatalf("findParentConfig: %v", cfgErr)
|
||||
}
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
|
|
|
|||
|
|
@ -84,10 +84,17 @@ This command checks:
|
|||
- .beads/.gitignore up to date
|
||||
- Metadata.json version tracking (LastBdVersion field)
|
||||
|
||||
Storage Availability:
|
||||
Full diagnostics, --perf, --deep, --server, --migration, and
|
||||
--check=validate currently require Dolt server mode. Embedded Dolt
|
||||
supports --check=artifacts, --check=conventions, and
|
||||
--check=pollution. --check-health has a limited hook-health fallback.
|
||||
Unsupported combinations return a notice without changing storage.
|
||||
|
||||
Performance Mode (--perf):
|
||||
Run performance diagnostics on your database:
|
||||
- Times key operations (bd ready, bd list, bd show, etc.)
|
||||
- Collects system info (OS, arch, SQLite version, database stats)
|
||||
- Collects system info (OS, arch, database stats)
|
||||
- Generates CPU profile for analysis
|
||||
- Outputs shareable report for bug reports
|
||||
|
||||
|
|
@ -123,18 +130,12 @@ Server Mode (--server):
|
|||
- Schema compatible: Can query beads tables?
|
||||
- Connection pool: Pool health metrics
|
||||
|
||||
Migration Validation Mode (--migration):
|
||||
Run Dolt migration validation checks with machine-parseable output.
|
||||
Use --migration=pre before migration to verify readiness:
|
||||
- JSONL file exists and is valid (parseable, no corruption)
|
||||
- All JSONL issues are present in SQLite (or explains discrepancies)
|
||||
- No blocking issues prevent migration
|
||||
Use --migration=post after migration to verify completion:
|
||||
- Dolt database exists and is healthy
|
||||
- All issues from JSONL are present in Dolt
|
||||
- No data was lost during migration
|
||||
- Dolt database has no locks or uncommitted changes
|
||||
Combine with --json for machine-parseable output for automation.
|
||||
Legacy Dolt Migration Validation Mode (--migration):
|
||||
Retained for older SQLite-to-Dolt migration workflows and available only in
|
||||
Dolt server mode. It is not the migration path for removed backends
|
||||
(PostgreSQL, MySQL, SQLite); those fail closed with export/import guidance.
|
||||
Combine
|
||||
with --json for machine-parseable diagnostic output.
|
||||
|
||||
Agent Mode (--agent):
|
||||
Output diagnostics designed for AI agent consumption. Instead of terse
|
||||
|
|
@ -181,9 +182,9 @@ Examples:
|
|||
bd doctor --check=validate --fix # Auto-fix data-integrity issues
|
||||
bd doctor --deep # Full graph integrity validation
|
||||
bd doctor --server # Dolt server mode health checks
|
||||
bd doctor --migration=pre # Validate readiness for Dolt migration
|
||||
bd doctor --migration=post # Validate Dolt migration completed
|
||||
bd doctor --migration=pre --json # Machine-parseable migration validation`,
|
||||
bd doctor --migration=pre # Legacy Dolt-server migration diagnostic
|
||||
bd doctor --migration=post # Legacy Dolt-server completion diagnostic
|
||||
bd doctor --migration=pre --json # Machine-parseable legacy diagnostic`,
|
||||
SilenceUsage: true,
|
||||
SilenceErrors: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
|
@ -194,11 +195,6 @@ Examples:
|
|||
}
|
||||
}()
|
||||
|
||||
if usesProxiedServer() {
|
||||
fmt.Fprintln(os.Stderr, "Note: 'bd doctor' is not yet supported in proxied-server mode.")
|
||||
return nil
|
||||
}
|
||||
|
||||
var checkPath string
|
||||
if len(args) > 0 {
|
||||
checkPath = args[0]
|
||||
|
|
@ -212,6 +208,14 @@ Examples:
|
|||
if err != nil {
|
||||
return HandleError("failed to resolve path: %v", err)
|
||||
}
|
||||
if err := validateDoctorWorkspaceBackend(absPath); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
|
||||
if usesProxiedServer() {
|
||||
fmt.Fprintln(os.Stderr, "Note: 'bd doctor' is not yet supported in proxied-server mode.")
|
||||
return nil
|
||||
}
|
||||
|
||||
if doctorFix && isOrchestratorRoot(absPath) {
|
||||
return HandleErrorWithHint(
|
||||
|
|
@ -340,7 +344,7 @@ func init() {
|
|||
doctorCmd.Flags().BoolVar(&doctorOrchestrator, "orchestrator", false, "Running in orchestrator multi-workspace mode (routes.jsonl is expected, higher duplicate tolerance)")
|
||||
doctorCmd.Flags().IntVar(&orchestratorDuplicatesThreshold, "orchestrator-duplicates-threshold", 1000, "Duplicate tolerance threshold for orchestrator mode (wisps are ephemeral)")
|
||||
doctorCmd.Flags().BoolVar(&doctorServer, "server", false, "Run Dolt server mode health checks (connectivity, version, schema)")
|
||||
doctorCmd.Flags().StringVar(&doctorMigration, "migration", "", "Run Dolt migration validation: 'pre' (before migration) or 'post' (after migration)")
|
||||
doctorCmd.Flags().StringVar(&doctorMigration, "migration", "", "Run legacy Dolt-server migration diagnostics: 'pre' or 'post'")
|
||||
doctorCmd.Flags().BoolVar(&doctorAgent, "agent", false, "Agent-facing diagnostic mode: rich context for AI agents (ZFC-compliant)")
|
||||
}
|
||||
|
||||
|
|
@ -348,6 +352,19 @@ func shouldSkipDoctorNetworkChecks() bool {
|
|||
return jsonOutput || !ui.IsTerminal()
|
||||
}
|
||||
|
||||
// validateDoctorWorkspaceBackend keeps doctor diagnostics read-only when metadata
|
||||
// selects a removed or unknown implementation or cannot be parsed. Doctor contains
|
||||
// direct diagnostic store paths and may run under shared-server mode, so corrupt
|
||||
// metadata must be rejected before version tracking or any database check begins.
|
||||
func validateDoctorWorkspaceBackend(path string) error {
|
||||
beadsDir := doctor.ResolveBeadsDirForRepo(path)
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to load %s: %w; no storage database was opened or modified; fix or restore metadata.json and retry", configfile.ConfigPath(beadsDir), err)
|
||||
}
|
||||
return validateConfiguredBackend(cfg)
|
||||
}
|
||||
|
||||
// printEmbeddedUnsupported reports that a doctor variant is not yet wired up
|
||||
// for embedded mode. Emits a structured payload to stderr when --json or
|
||||
// --agent is set so downstream tooling can detect the gap without parsing
|
||||
|
|
|
|||
|
|
@ -43,9 +43,8 @@ func RunDeepValidation(path string) DeepValidationResult {
|
|||
check := DoctorCheck{
|
||||
Name: "Deep Validation",
|
||||
Status: StatusWarning,
|
||||
Message: "SQLite backend detected",
|
||||
Message: fmt.Sprintf("N/A (deep validation requires Dolt; configured backend is %q)", backend),
|
||||
Category: CategoryMaintenance,
|
||||
Fix: "Run 'bd init' to set up Dolt backend",
|
||||
}
|
||||
result.AllChecks = append(result.AllChecks, check)
|
||||
return result
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
|
|
@ -45,6 +46,26 @@ func TestRunDeepValidation_EmptyBeadsDir(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRunDeepValidationSQLiteIsNotAMigrationWarning(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := (&configfile.Config{Backend: configfile.BackendSQLite, SQLitePath: "beads.db"}).Save(beadsDir); err != nil {
|
||||
t.Fatalf("save SQLite config: %v", err)
|
||||
}
|
||||
|
||||
result := RunDeepValidation(tmpDir)
|
||||
if len(result.AllChecks) != 1 {
|
||||
t.Fatalf("SQLite deep-validation result = %#v, want one N/A check", result)
|
||||
}
|
||||
check := result.AllChecks[0]
|
||||
if check.Status != StatusWarning || !strings.Contains(check.Message, "sqlite") || check.Fix != "" {
|
||||
t.Fatalf("SQLite deep-validation check = %#v, want non-Dolt N/A without migration fix", check)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCheckParentConsistency_OrphanedDeps verifies detection of orphaned parent-child deps
|
||||
func TestCheckParentConsistency_OrphanedDeps(t *testing.T) {
|
||||
store := newTestDoltStore(t, "bd")
|
||||
|
|
|
|||
|
|
@ -101,9 +101,10 @@ func openDoltConn(beadsDir string) (*doltConn, error) {
|
|||
return &doltConn{db: db, cfg: cfg, port: port}, nil
|
||||
}
|
||||
|
||||
// GetBackend returns the configured backend type from configuration.
|
||||
// It checks config.yaml first (storage-backend key), then falls back to metadata.json.
|
||||
// Returns "dolt" (default) or "sqlite" (legacy).
|
||||
// GetBackend returns the configured backend type from metadata.json. An absent
|
||||
// or empty backend field reads as "dolt" (the default); removed-backend
|
||||
// tombstones ("postgres", "mysql", "sqlite") are returned verbatim so callers
|
||||
// can label them without opening anything.
|
||||
// hq-3446fc.17: Use dolt.GetBackendFromConfig for consistent backend detection.
|
||||
func GetBackend(beadsDir string) string {
|
||||
return dolt.GetBackendFromConfig(beadsDir)
|
||||
|
|
@ -126,13 +127,13 @@ func runDoltHealthChecksInternal(path string) []DoctorCheck {
|
|||
|
||||
if !IsDoltBackend(beadsDir) {
|
||||
return []DoctorCheck{
|
||||
{Name: "Dolt Connection", Status: StatusOK, Message: "N/A (SQLite backend)", Category: CategoryCore},
|
||||
{Name: "Dolt Schema", Status: StatusOK, Message: "N/A (SQLite backend)", Category: CategoryCore},
|
||||
{Name: "Dolt Issue Count", Status: StatusOK, Message: "N/A (SQLite backend)", Category: CategoryData},
|
||||
{Name: "Dolt Status", Status: StatusOK, Message: "N/A (SQLite backend)", Category: CategoryData},
|
||||
{Name: "Dolt Lock Health", Status: StatusOK, Message: "N/A (SQLite backend)", Category: CategoryRuntime},
|
||||
{Name: "Phantom Databases", Status: StatusOK, Message: "N/A (SQLite backend)", Category: CategoryData},
|
||||
{Name: "Shared Server", Status: StatusOK, Message: "N/A (SQLite backend)", Category: CategoryRuntime},
|
||||
{Name: "Dolt Connection", Status: StatusOK, Message: "N/A (non-Dolt backend)", Category: CategoryCore},
|
||||
{Name: "Dolt Schema", Status: StatusOK, Message: "N/A (non-Dolt backend)", Category: CategoryCore},
|
||||
{Name: "Dolt Issue Count", Status: StatusOK, Message: "N/A (non-Dolt backend)", Category: CategoryData},
|
||||
{Name: "Dolt Status", Status: StatusOK, Message: "N/A (non-Dolt backend)", Category: CategoryData},
|
||||
{Name: "Dolt Lock Health", Status: StatusOK, Message: "N/A (non-Dolt backend)", Category: CategoryRuntime},
|
||||
{Name: "Phantom Databases", Status: StatusOK, Message: "N/A (non-Dolt backend)", Category: CategoryData},
|
||||
{Name: "Shared Server", Status: StatusOK, Message: "N/A (non-Dolt backend)", Category: CategoryRuntime},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -274,7 +274,7 @@ func runBDDoctor(t *testing.T, bdPath, path string) (e2eDoctorResult, string, er
|
|||
return result, string(out), execErr
|
||||
}
|
||||
|
||||
// TestE2E_DoctorSQLiteBackend was removed: SQLite backend no longer exists.
|
||||
// TestE2E_DoctorSQLiteBackend is covered by the backend-neutral doctor tests.
|
||||
// GetBackend() always returns "dolt" after the dolt-native cleanup (bd-yqpwy).
|
||||
|
||||
// TestE2E_DoctorDoltBackendNoDB was removed: the embedded Dolt driver
|
||||
|
|
|
|||
|
|
@ -508,7 +508,7 @@ func CheckFreshClone(repoPath string) DoctorCheck {
|
|||
return freshCloneServerUnreachableResult(dbName, host, port, result.Err)
|
||||
}
|
||||
default:
|
||||
// SQLite (default): check configured .db file path.
|
||||
// Non-Dolt (legacy sqlite-era metadata): check configured .db file path.
|
||||
var dbPath string
|
||||
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil && cfg.Database != "" {
|
||||
dbPath = cfg.DatabasePath(beadsDir)
|
||||
|
|
|
|||
|
|
@ -148,7 +148,7 @@ func CheckMigrationReadiness(path string) (DoctorCheck, MigrationValidationResul
|
|||
Status: status,
|
||||
Message: message,
|
||||
Detail: strings.Join(result.Warnings, "\n"),
|
||||
Fix: "Run 'bd migrate dolt' to start migration",
|
||||
Fix: "Follow 'bd help init-safety' to reinitialize with Dolt, then import and verify this JSONL export",
|
||||
Category: CategoryMaintenance,
|
||||
}, result
|
||||
}
|
||||
|
|
@ -195,7 +195,7 @@ func CheckMigrationCompletion(path string) (DoctorCheck, MigrationValidationResu
|
|||
Status: StatusError,
|
||||
Message: "Not using Dolt backend",
|
||||
Detail: fmt.Sprintf("Current backend: %s", result.Backend),
|
||||
Fix: "Run 'bd migrate dolt' to migrate to Dolt",
|
||||
Fix: "Follow 'bd help init-safety' to reinitialize with Dolt, then import and verify the issue export",
|
||||
Category: CategoryMaintenance,
|
||||
}, result
|
||||
}
|
||||
|
|
@ -302,7 +302,7 @@ func CheckMigrationCompletion(path string) (DoctorCheck, MigrationValidationResu
|
|||
Status: StatusError,
|
||||
Message: fmt.Sprintf("Migration incomplete: %d error(s)", len(result.Errors)),
|
||||
Detail: strings.Join(result.Errors, "\n"),
|
||||
Fix: "Re-run 'bd migrate dolt' or check for data issues",
|
||||
Fix: "Check the export/import results; follow 'bd help init-safety' before reinitializing again",
|
||||
Category: CategoryMaintenance,
|
||||
}, result
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import (
|
|||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
@ -135,13 +136,14 @@ func TestCheckMigrationReadinessResult_NoJSONL(t *testing.T) {
|
|||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create .beads: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"backend":"sqlite","sqlite_path":"issues.db"}`), 0644); err != nil {
|
||||
t.Fatalf("failed to create SQLite metadata: %v", err)
|
||||
}
|
||||
|
||||
check, _ := CheckMigrationReadiness(tmpDir)
|
||||
|
||||
// With Dolt-only backend, GetBackend defaults to BackendDolt,
|
||||
// so migration readiness returns OK ("Already using Dolt backend")
|
||||
if check.Status != StatusOK {
|
||||
t.Errorf("status = %q, want %q (Dolt-only backend returns OK)", check.Status, StatusOK)
|
||||
if check.Status != StatusError {
|
||||
t.Errorf("status = %q, want %q", check.Status, StatusError)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -156,6 +158,9 @@ func TestCheckMigrationReadinessResult_ValidJSONL(t *testing.T) {
|
|||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create .beads: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"backend":"sqlite","sqlite_path":"issues.db"}`), 0644); err != nil {
|
||||
t.Fatalf("failed to create SQLite metadata: %v", err)
|
||||
}
|
||||
|
||||
// Create valid JSONL
|
||||
jsonl := `{"id":"bd-001","title":"Test 1"}` + "\n" + `{"id":"bd-002","title":"Test 2"}`
|
||||
|
|
@ -165,12 +170,15 @@ func TestCheckMigrationReadinessResult_ValidJSONL(t *testing.T) {
|
|||
|
||||
check, _ := CheckMigrationReadiness(tmpDir)
|
||||
|
||||
// With Dolt-only backend, GetBackend defaults to BackendDolt,
|
||||
// so migration readiness returns OK ("Already using Dolt backend")
|
||||
// without validating JSONL (no migration needed)
|
||||
if check.Status == StatusError {
|
||||
t.Errorf("status = %q, did not want error", check.Status)
|
||||
}
|
||||
if !strings.Contains(check.Fix, "bd help init-safety") {
|
||||
t.Errorf("fix must point to safe reinitialization guidance, got %q", check.Fix)
|
||||
}
|
||||
if strings.Contains(check.Fix, "bd migrate dolt") {
|
||||
t.Errorf("fix must not recommend removed backend-conversion command, got %q", check.Fix)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckMigrationCompletionResult_NoBeadsDir(t *testing.T) {
|
||||
|
|
@ -206,6 +214,9 @@ func TestCheckMigrationCompletionResult_NotDoltBackend(t *testing.T) {
|
|||
if err := os.MkdirAll(beadsDir, 0755); err != nil {
|
||||
t.Fatalf("failed to create .beads: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"backend":"sqlite","sqlite_path":"issues.db"}`), 0644); err != nil {
|
||||
t.Fatalf("failed to create SQLite metadata: %v", err)
|
||||
}
|
||||
|
||||
// Create JSONL (no Dolt backend present)
|
||||
jsonl := `{"id":"bd-001","title":"Test 1"}`
|
||||
|
|
@ -226,6 +237,12 @@ func TestCheckMigrationCompletionResult_NotDoltBackend(t *testing.T) {
|
|||
if len(result.Errors) == 0 {
|
||||
t.Error("expected errors in result")
|
||||
}
|
||||
if !strings.Contains(check.Fix, "bd help init-safety") {
|
||||
t.Errorf("fix must point to safe reinitialization guidance, got %q", check.Fix)
|
||||
}
|
||||
if strings.Contains(check.Fix, "bd migrate dolt") {
|
||||
t.Errorf("fix must not recommend removed backend-conversion command, got %q", check.Fix)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrationValidationResult_JSONSerialization(t *testing.T) {
|
||||
|
|
|
|||
|
|
@ -49,7 +49,8 @@ func RunDoltPerformanceDiagnostics(path string, enableProfiling bool) (*DoltPerf
|
|||
|
||||
// Verify this is a Dolt backend
|
||||
if !IsDoltBackend(beadsDir) {
|
||||
return nil, fmt.Errorf("SQLite backend is no longer supported. Migrate to Dolt with 'bd migrate'")
|
||||
backend, _ := getBackendAndBeadsDir(path)
|
||||
return nil, fmt.Errorf("Dolt server performance diagnostics do not apply to the configured backend %q", backend)
|
||||
}
|
||||
|
||||
metrics := &DoltPerfMetrics{
|
||||
|
|
|
|||
|
|
@ -40,3 +40,24 @@ func TestRunDoltPerformanceDiagnostics_RequiresServer(t *testing.T) {
|
|||
t.Errorf("expected server/database error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunDoltPerformanceDiagnosticsDeclineNonDoltBackend(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg := &configfile.Config{Backend: configfile.BackendSQLite, SQLitePath: "beads.db"}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("save SQLite config: %v", err)
|
||||
}
|
||||
|
||||
_, err := RunDoltPerformanceDiagnostics(tmpDir, false)
|
||||
if err == nil {
|
||||
t.Fatal("expected Dolt-only diagnostics to decline a non-Dolt workspace")
|
||||
}
|
||||
message := err.Error()
|
||||
if !strings.Contains(message, "Dolt") || !strings.Contains(message, `"sqlite"`) {
|
||||
t.Fatalf("diagnostic error should explain the backend mismatch: %v", err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,9 +61,8 @@ func RunServerHealthChecks(path string) ServerHealthResult {
|
|||
result.Checks = append(result.Checks, DoctorCheck{
|
||||
Name: "Server Config",
|
||||
Status: StatusWarning,
|
||||
Message: fmt.Sprintf("Backend is '%s', not Dolt", cfg.GetBackend()),
|
||||
Detail: "Server mode health checks are only relevant for Dolt backend",
|
||||
Fix: "Set backend: dolt in metadata.json to use Dolt server mode",
|
||||
Message: fmt.Sprintf("Server checks require Dolt; configured backend is %q", cfg.GetBackend()),
|
||||
Detail: "Dolt server health checks do not apply to this backend",
|
||||
Category: CategoryFederation,
|
||||
})
|
||||
result.OverallOK = false
|
||||
|
|
|
|||
33
cmd/bd/doctor/server_backend_test.go
Normal file
33
cmd/bd/doctor/server_backend_test.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package doctor
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
)
|
||||
|
||||
func TestRunServerHealthChecksSQLiteIsNotAMigrationWarning(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := (&configfile.Config{Backend: configfile.BackendSQLite, SQLitePath: "beads.db"}).Save(beadsDir); err != nil {
|
||||
t.Fatalf("save SQLite config: %v", err)
|
||||
}
|
||||
|
||||
// The sqlite backend is a removed-backend tombstone: server checks report a
|
||||
// clearly-attributed non-Dolt warning with no migration fix, and never edit
|
||||
// the workspace.
|
||||
result := RunServerHealthChecks(tmpDir)
|
||||
if result.OverallOK || len(result.Checks) != 1 {
|
||||
t.Fatalf("SQLite server-health result = %#v, want one non-Dolt warning", result)
|
||||
}
|
||||
check := result.Checks[0]
|
||||
if check.Status != StatusWarning || !strings.Contains(check.Message, "sqlite") || check.Fix != "" {
|
||||
t.Fatalf("SQLite server-health check = %#v, want non-Dolt warning without migration fix", check)
|
||||
}
|
||||
}
|
||||
61
cmd/bd/doctor_backend_guard_test.go
Normal file
61
cmd/bd/doctor_backend_guard_test.go
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
//go:build cgo
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDoctorRejectsCorruptMetadataBeforeSharedServerChecks(t *testing.T) {
|
||||
bd := buildBDUnderTest(t)
|
||||
root := t.TempDir()
|
||||
beadsDir := filepath.Join(root, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
||||
t.Fatalf("create .beads: %v", err)
|
||||
}
|
||||
|
||||
metadata := []byte("{\n")
|
||||
metadataPath := filepath.Join(beadsDir, "metadata.json")
|
||||
if err := os.WriteFile(metadataPath, metadata, 0o600); err != nil {
|
||||
t.Fatalf("write corrupt metadata.json: %v", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(bd, "doctor", "--json")
|
||||
cmd.Dir = root
|
||||
cmd.Env = append(removedBackendTestEnv(beadsDir),
|
||||
"BEADS_DOLT_SHARED_SERVER=1",
|
||||
"BEADS_DOLT_SERVER_PORT=1",
|
||||
"BEADS_DOLT_AUTO_START=0",
|
||||
"BEADS_NO_DAEMON=1",
|
||||
"BD_DISABLE_METRICS=1",
|
||||
)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatalf("doctor unexpectedly ran shared-server checks with corrupt metadata:\n%s", out)
|
||||
}
|
||||
|
||||
message := strings.ToLower(string(out))
|
||||
for _, want := range []string{"metadata.json", "parsing config", "no storage database was opened or modified"} {
|
||||
if !strings.Contains(message, want) {
|
||||
t.Errorf("doctor corrupt-metadata error missing %q:\n%s", want, out)
|
||||
}
|
||||
}
|
||||
|
||||
after, readErr := os.ReadFile(metadataPath)
|
||||
if readErr != nil {
|
||||
t.Fatalf("read metadata after refusal: %v", readErr)
|
||||
}
|
||||
if !bytes.Equal(after, metadata) {
|
||||
t.Fatalf("doctor rewrote corrupt metadata:\nbefore: %q\nafter: %q", metadata, after)
|
||||
}
|
||||
for _, name := range []string{".local_version", "embeddeddolt", "dolt", "beads.db", "dolt-server.pid", "dolt-server.port"} {
|
||||
if _, statErr := os.Stat(filepath.Join(beadsDir, name)); !os.IsNotExist(statErr) {
|
||||
t.Fatalf("doctor created %s before rejecting corrupt metadata (stat error: %v)", name, statErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -144,7 +144,7 @@ func TestValidateCheck_DetectsOrphanedDeps(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestValidateCheck_GitConflicts_DoltClean(t *testing.T) {
|
||||
// Since GetBackend() always returns "dolt" (SQLite removed in 87493ce9),
|
||||
// When metadata is absent, GetBackend() defaults to "dolt",
|
||||
// the Git Conflicts check now queries dolt_conflicts (GH-2249).
|
||||
// Verify it reports OK for a clean Dolt database.
|
||||
tmpDir, store := setupValidateTestDB(t, "val")
|
||||
|
|
|
|||
132
cmd/bd/dolt.go
132
cmd/bd/dolt.go
|
|
@ -106,6 +106,13 @@ Examples:
|
|||
bd dolt set data-dir /home/user/.beads-dolt/myproject`,
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
beadsDir := selectedDoltBeadsDir()
|
||||
if beadsDir == "" {
|
||||
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
||||
}
|
||||
if _, err := loadDoltBackendConfig(beadsDir); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
if !usesSQLServer() {
|
||||
return HandleError("'bd dolt set' is not supported in embedded mode (no Dolt server)")
|
||||
}
|
||||
|
|
@ -129,6 +136,13 @@ This verifies that:
|
|||
|
||||
Use this before switching to server mode to ensure the server is running.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
beadsDir := selectedDoltBeadsDir()
|
||||
if beadsDir == "" {
|
||||
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
||||
}
|
||||
if _, err := loadDoltBackendConfig(beadsDir); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
if !usesSQLServer() {
|
||||
return HandleError("'bd dolt test' is not supported in embedded mode (no Dolt server)")
|
||||
}
|
||||
|
|
@ -488,30 +502,6 @@ The remote must already exist (see 'bd dolt remote add').`,
|
|||
},
|
||||
}
|
||||
|
||||
// errExplicitCommitUnsupported returns a typed *storage.ErrUnsupported when the
|
||||
// open store has no Dolt commit graph (Postgres/MySQL/SQLite), and nil for Dolt.
|
||||
// The SQL backends deliberately expose a no-op Commit so internal write paths can
|
||||
// call store.Commit() opportunistically without erroring; an EXPLICIT `bd dolt
|
||||
// commit` / `bd vc commit` must not ride that no-op and print a fake "Committed."
|
||||
// with no commit graph behind it. Internal opportunistic auto-commit is skipped
|
||||
// separately in PostRun for these backends, so gating only the explicit commands
|
||||
// keeps that path untouched.
|
||||
func errExplicitCommitUnsupported(st storage.DoltStorage, op string) error {
|
||||
ncg, ok := storage.UnwrapStore(st).(storage.NonCommitGraphBackend)
|
||||
if !ok || !ncg.CommitGraphUnsupported() {
|
||||
return nil
|
||||
}
|
||||
backend := "non-dolt"
|
||||
if beadsDir := selectedDoltBeadsDir(); beadsDir != "" {
|
||||
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil {
|
||||
if b := cfg.GetBackend(); b != "" && b != configfile.BackendDolt {
|
||||
backend = b
|
||||
}
|
||||
}
|
||||
}
|
||||
return &storage.ErrUnsupported{Op: op, Backend: backend}
|
||||
}
|
||||
|
||||
var doltCommitCmd = &cobra.Command{
|
||||
Use: "commit",
|
||||
Short: "Create a Dolt commit from pending changes",
|
||||
|
|
@ -533,9 +523,6 @@ For more options (--stdin, custom messages), see: bd vc commit`,
|
|||
if st == nil {
|
||||
return HandleError("no store available")
|
||||
}
|
||||
if err := errExplicitCommitUnsupported(st, "dolt commit"); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
msg, _ := cmd.Flags().GetString("message")
|
||||
if msg == "" {
|
||||
msg = fmt.Sprintf("bd: dolt commit (auto-commit) by %s", getActor())
|
||||
|
|
@ -566,13 +553,16 @@ project path. PID and logs are stored in .beads/.
|
|||
The server auto-starts transparently when needed, so manual start is rarely
|
||||
required. Use this command for explicit control or diagnostics.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if !usesSQLServer() {
|
||||
return HandleError("'bd dolt start' is not supported in embedded mode (no Dolt server)")
|
||||
}
|
||||
beadsDir := selectedDoltBeadsDir()
|
||||
if beadsDir == "" {
|
||||
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
||||
}
|
||||
if _, err := loadDoltBackendConfig(beadsDir); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
if !usesSQLServer() {
|
||||
return HandleError("'bd dolt start' is not supported in embedded mode (no Dolt server)")
|
||||
}
|
||||
serverDir := doltserver.ResolveServerDir(beadsDir)
|
||||
|
||||
state, err := doltserver.Start(serverDir)
|
||||
|
|
@ -609,13 +599,16 @@ var doltStopCmd = &cobra.Command{
|
|||
This sends a graceful shutdown signal. The server will restart automatically
|
||||
on the next bd command unless auto-start is disabled.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if !usesSQLServer() {
|
||||
return HandleError("'bd dolt stop' is not supported in embedded mode (no Dolt server)")
|
||||
}
|
||||
beadsDir := selectedDoltBeadsDir()
|
||||
if beadsDir == "" {
|
||||
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
||||
}
|
||||
if _, err := loadDoltBackendConfig(beadsDir); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
if !usesSQLServer() {
|
||||
return HandleError("'bd dolt stop' is not supported in embedded mode (no Dolt server)")
|
||||
}
|
||||
|
||||
if usesProxiedServer() {
|
||||
rootDir, err := resolveProxiedServerRootPath(beadsDir)
|
||||
|
|
@ -659,10 +652,20 @@ endpoint via SQL and reports reachability, server version, and database.`,
|
|||
if beadsDir == "" {
|
||||
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
||||
}
|
||||
// A non-Dolt backend (postgres/mysql/sqlite) has no Dolt engine at all;
|
||||
cfg, cfgErr := configfile.Load(beadsDir)
|
||||
if cfgErr != nil {
|
||||
return HandleError("loading config: %v", cfgErr)
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
if err := validateConfiguredBackend(cfg); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
// A non-Dolt backend (SQLite or a removed-backend tombstone) has no Dolt engine;
|
||||
// report the backend rather than misdescribing an embedded Dolt server
|
||||
// (parity with `bd dolt show`, which already special-cases this).
|
||||
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil && cfg.GetBackend() != configfile.BackendDolt {
|
||||
if cfg.GetBackend() != configfile.BackendDolt {
|
||||
fmt.Printf("Backend: %s (no Dolt engine)\n", cfg.GetBackend())
|
||||
return nil
|
||||
}
|
||||
|
|
@ -679,19 +682,10 @@ endpoint via SQL and reports reachability, server version, and database.`,
|
|||
// systemd manages the server lifecycle, be-0eyj)
|
||||
//
|
||||
// IsAutoStartDisabled reads the active (globally-bound) config and
|
||||
// BEADS_DOLT_AUTO_START env, not the per-beadsDir cfg loaded just
|
||||
// below. That coupling is intentional and consistent with every
|
||||
// BEADS_DOLT_AUTO_START env, not the per-beadsDir cfg loaded above.
|
||||
// That coupling is intentional and consistent with every
|
||||
// other call site of IsAutoStartDisabled in this package — both
|
||||
// resolve against the same active workspace at command time.
|
||||
cfg, cfgErr := configfile.Load(beadsDir)
|
||||
if cfgErr != nil {
|
||||
// Don't silently swallow. A corrupted or missing metadata.json
|
||||
// would otherwise mask the externally-managed routing for both
|
||||
// the remote-host and auto-start-disabled-local cases, falling
|
||||
// through to the PID-file path with a misleading "not running"
|
||||
// — which is the exact failure mode this PR addresses.
|
||||
fmt.Fprintf(os.Stderr, "Warning: cannot load .beads config (%v); falling back to PID-file status path\n", cfgErr)
|
||||
}
|
||||
if cfg != nil && shouldUseExternalDoltStatus(cfg, doltserver.IsAutoStartDisabled(), doltserver.IsSharedServerMode()) {
|
||||
runExternalDoltStatus(beadsDir, cfg)
|
||||
return nil
|
||||
|
|
@ -942,10 +936,15 @@ servers are preserved.`,
|
|||
SilenceUsage: true,
|
||||
SilenceErrors: true,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
beadsDir := selectedDoltBeadsDir()
|
||||
if beadsDir != "" {
|
||||
if _, err := loadDoltBackendConfig(beadsDir); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
}
|
||||
if !usesSQLServer() {
|
||||
return HandleError("'bd dolt killall' is not supported in embedded mode (no Dolt server)")
|
||||
}
|
||||
beadsDir := selectedDoltBeadsDir()
|
||||
if beadsDir == "" {
|
||||
beadsDir = "." // best effort
|
||||
}
|
||||
|
|
@ -1007,6 +1006,13 @@ Stale database prefixes: testdb_*, beads_test*, beads_pt*, beads_vr*, doctest_*,
|
|||
These waste server memory and can degrade performance under concurrent load.
|
||||
Use --dry-run to see what would be dropped without actually dropping.`,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
beadsDir := selectedDoltBeadsDir()
|
||||
if beadsDir == "" {
|
||||
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
||||
}
|
||||
if _, err := loadDoltBackendConfig(beadsDir); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
if !usesSQLServer() {
|
||||
return HandleError("'bd dolt clean-databases' is not supported in embedded mode (no Dolt server)")
|
||||
}
|
||||
|
|
@ -1450,6 +1456,9 @@ func showDoltConfig(testConnection bool) error {
|
|||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
if err := validateConfiguredBackend(cfg); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
|
||||
backend := cfg.GetBackend()
|
||||
embedded := !usesSQLServer()
|
||||
|
|
@ -1548,16 +1557,9 @@ func setDoltConfig(key, value string, updateConfig bool) error {
|
|||
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
||||
}
|
||||
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
cfg, err := loadDoltBackendConfig(beadsDir)
|
||||
if err != nil {
|
||||
return HandleError("loading config: %v", err)
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
|
||||
if cfg.GetBackend() != configfile.BackendDolt {
|
||||
return HandleError("not using Dolt backend")
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
|
||||
var yamlKey string
|
||||
|
|
@ -1709,16 +1711,9 @@ func testDoltConnection() error {
|
|||
return HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
||||
}
|
||||
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
cfg, err := loadDoltBackendConfig(beadsDir)
|
||||
if err != nil {
|
||||
return HandleError("loading config: %v", err)
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
|
||||
if cfg.GetBackend() != configfile.BackendDolt {
|
||||
return HandleError("not using Dolt backend")
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
|
||||
host := cfg.GetDoltServerHost()
|
||||
|
|
@ -1878,12 +1873,9 @@ func openDoltServerConnection() (*sql.DB, func(), error) {
|
|||
return nil, nil, HandleErrorWithHint(activeWorkspaceNotFoundError(), diagHint())
|
||||
}
|
||||
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
cfg, err := loadDoltBackendConfig(beadsDir)
|
||||
if err != nil {
|
||||
return nil, nil, HandleError("loading config: %v", err)
|
||||
}
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
return nil, nil, HandleError("%v", err)
|
||||
}
|
||||
|
||||
host := cfg.GetDoltServerHost()
|
||||
|
|
|
|||
487
cmd/bd/init.go
487
cmd/bd/init.go
|
|
@ -3,20 +3,17 @@ package main
|
|||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/steveyegge/beads/cmd/bd/doctor"
|
||||
"github.com/steveyegge/beads/cmd/bd/setup"
|
||||
"github.com/steveyegge/beads/internal/beads"
|
||||
|
|
@ -28,11 +25,7 @@ import (
|
|||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/proxy"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
beadsmysql "github.com/steveyegge/beads/internal/storage/mysql"
|
||||
"github.com/steveyegge/beads/internal/storage/pgdialect"
|
||||
"github.com/steveyegge/beads/internal/storage/postgres"
|
||||
"github.com/steveyegge/beads/internal/storage/schema"
|
||||
beadssqlite "github.com/steveyegge/beads/internal/storage/sqlite"
|
||||
"github.com/steveyegge/beads/internal/templates/agents"
|
||||
"github.com/steveyegge/beads/internal/ui"
|
||||
"github.com/steveyegge/beads/internal/utils"
|
||||
|
|
@ -48,9 +41,8 @@ var initCmd = &cobra.Command{
|
|||
Long: `Initialize bd in the current directory by creating a .beads/ directory
|
||||
and its storage (a Dolt database by default). Optionally specify a custom issue prefix.
|
||||
|
||||
Dolt is the default backend and the only one with version control (history,
|
||||
branching, sync). Select an alternative with --backend=<postgres|mysql|sqlite>;
|
||||
see docs/architecture/storage-backends.md for the trade-offs and setup.
|
||||
Dolt is the default and only supported storage backend, with full version
|
||||
control (history, branching, sync).
|
||||
|
||||
Use --database to specify an existing server database name, overriding the
|
||||
default prefix-based naming. This is useful when an external tool (e.g. an orchestrator)
|
||||
|
|
@ -105,12 +97,6 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
serverUser, _ := cmd.Flags().GetString("server-user")
|
||||
database, _ := cmd.Flags().GetString("database")
|
||||
destroyToken, _ := cmd.Flags().GetString("destroy-token")
|
||||
// Postgres backend flags (backend="postgres")
|
||||
pgURL, _ := cmd.Flags().GetString("pg-url")
|
||||
pgSchema, _ := cmd.Flags().GetString("pg-schema")
|
||||
mysqlURL, _ := cmd.Flags().GetString("mysql-url")
|
||||
mysqlDatabase, _ := cmd.Flags().GetString("mysql-database")
|
||||
sqlitePath, _ := cmd.Flags().GetString("sqlite-path")
|
||||
|
||||
// --force is a deprecated alias for --reinit-local. They share
|
||||
// semantics for the local data-safety guard; both refuse remote
|
||||
|
|
@ -249,49 +235,21 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
externalConfig = &cfg
|
||||
}
|
||||
|
||||
// Backend selection: dolt (default) + the pluggable SQL-family backends
|
||||
// (postgres, mysql, sqlite). SQLite was removed under the one-backend
|
||||
// assumption (#3151) and re-added as a pluggable leaf backend.
|
||||
isPostgres := backendFlag == configfile.BackendPostgres
|
||||
isMySQL := backendFlag == configfile.BackendMySQL
|
||||
isSQLite := backendFlag == configfile.BackendSQLite
|
||||
if backendFlag != "" && backendFlag != configfile.BackendDolt && !isPostgres && !isMySQL && !isSQLite {
|
||||
return fmt.Errorf("unknown backend %q: supported backends are \"dolt\" (default), \"postgres\", \"mysql\", and \"sqlite\"", backendFlag)
|
||||
}
|
||||
if isPostgres || isMySQL || isSQLite {
|
||||
// A non-Dolt SQL backend is a plain local workspace: only a small set of
|
||||
// init-local flags apply. Reject any other init-local flag by ALLOWLIST — a
|
||||
// denylist silently ignores Dolt-only flags added later, the exact footgun
|
||||
// this block exists to prevent. Inherited/global flags (--json, --dir, …)
|
||||
// are not init-local and are left untouched.
|
||||
pgAllowedFlags := map[string]bool{
|
||||
"backend": true,
|
||||
"pg-url": true, "pg-schema": true,
|
||||
"mysql-url": true, "mysql-database": true,
|
||||
"sqlite-path": true,
|
||||
"prefix": true, "quiet": true,
|
||||
"skip-hooks": true, "skip-agents": true,
|
||||
"reinit-local": true, "force": true, "init-if-missing": true,
|
||||
"non-interactive": true,
|
||||
// Backend selection: Dolt is the only supported backend.
|
||||
if !configfile.IsSupportedBackend(backendFlag) {
|
||||
switch backendFlag {
|
||||
case configfile.BackendPostgres, configfile.BackendMySQL:
|
||||
return fmt.Errorf("storage backend %q is no longer supported: %s; the supported backend is \"dolt\" (default)", backendFlag, configfile.RemovedBackendRationale)
|
||||
case configfile.BackendSQLite:
|
||||
return fmt.Errorf("storage backend %q is no longer supported: %s; the supported backend is \"dolt\" (default)", backendFlag, configfile.RemovedSQLiteRationale)
|
||||
}
|
||||
var rejected []string
|
||||
cmd.Flags().Visit(func(f *pflag.Flag) {
|
||||
// Only police init's own flags; inherited/global flags (--json, …)
|
||||
// are not this command's concern. cmd.Flags() is the authoritative
|
||||
// parsed set that carries the Changed state.
|
||||
if cmd.InheritedFlags().Lookup(f.Name) != nil {
|
||||
return
|
||||
}
|
||||
if !pgAllowedFlags[f.Name] {
|
||||
rejected = append(rejected, "--"+f.Name)
|
||||
}
|
||||
})
|
||||
if len(rejected) > 0 {
|
||||
sort.Strings(rejected)
|
||||
return fmt.Errorf("bd init --backend=%s does not support %s (a non-Dolt SQL backend is a plain local workspace: no Dolt server, sync, remote, or wizard)", backendFlag, strings.Join(rejected, ", "))
|
||||
return fmt.Errorf("unknown backend %q: the supported backend is \"dolt\" (default)", backendFlag)
|
||||
}
|
||||
for _, legacyFlag := range removedBackendInitFlags {
|
||||
if cmd.Flags().Changed(legacyFlag.name) {
|
||||
return fmt.Errorf("--%s belonged to %s: %s; use --backend=dolt (the default)", legacyFlag.name, legacyFlag.origin, legacyFlag.rationale)
|
||||
}
|
||||
}
|
||||
|
||||
// Validate --database format early, before any side effects.
|
||||
if database != "" {
|
||||
if err := dolt.ValidateDatabaseName(database); err != nil {
|
||||
|
|
@ -321,8 +279,7 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
return fmt.Errorf("--team requires interactive prompts and cannot be used with --non-interactive")
|
||||
}
|
||||
|
||||
// Non-Dolt backends (postgres/mysql/sqlite) were dispatched and returned
|
||||
// earlier; this is the Dolt init path.
|
||||
// Dolt is the only supported backend.
|
||||
backend := configfile.BackendDolt
|
||||
|
||||
// Also treat BEADS_DOLT_SERVER_MODE=1 env var as --server.
|
||||
|
|
@ -815,41 +772,6 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
|
||||
ctx := rootCtx
|
||||
|
||||
// Postgres proof-wedge: finalize a plain local workspace backed by Postgres.
|
||||
// Everything below provisions Dolt (embedded/server DB dir, migrations, remote
|
||||
// gate, sync, remotes) — none of which Postgres has an analog for — so the PG
|
||||
// path provisions the schema + seeds + metadata.json here and returns. The
|
||||
// shared setup above (.beads/, .gitignore, git init) already ran.
|
||||
if isPostgres {
|
||||
return runInitPostgres(ctx, initPostgresInput{
|
||||
beadsDir: beadsDir,
|
||||
prefix: prefix,
|
||||
pgURL: pgURL,
|
||||
pgSchema: pgSchema,
|
||||
quiet: quiet,
|
||||
jsonOutput: jsonOutput,
|
||||
})
|
||||
}
|
||||
if isMySQL {
|
||||
return runInitMySQL(ctx, initMySQLInput{
|
||||
beadsDir: beadsDir,
|
||||
prefix: prefix,
|
||||
mysqlURL: mysqlURL,
|
||||
mysqlDatabase: mysqlDatabase,
|
||||
quiet: quiet,
|
||||
jsonOutput: jsonOutput,
|
||||
})
|
||||
}
|
||||
if isSQLite {
|
||||
return runInitSQLite(ctx, initSQLiteInput{
|
||||
beadsDir: beadsDir,
|
||||
prefix: prefix,
|
||||
sqlitePath: sqlitePath,
|
||||
quiet: quiet,
|
||||
jsonOutput: jsonOutput,
|
||||
})
|
||||
}
|
||||
|
||||
// Create Dolt storage backend
|
||||
storagePath := doltserver.ResolveDoltDir(beadsDir)
|
||||
// Respect existing config's database name to avoid creating phantom catalog
|
||||
|
|
@ -1829,293 +1751,6 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
},
|
||||
}
|
||||
|
||||
// initPostgresInput carries the resolved inputs the Postgres init arm needs from
|
||||
// the shared prologue of the init command.
|
||||
type initPostgresInput struct {
|
||||
beadsDir string
|
||||
prefix string
|
||||
pgURL string
|
||||
pgSchema string
|
||||
quiet bool
|
||||
jsonOutput bool
|
||||
}
|
||||
|
||||
// runInitPostgres finalizes a Postgres-backed workspace. It is the Postgres analog
|
||||
// of the Dolt store-open + config-seed + metadata block (init.go), minus every
|
||||
// version-control / server / sync step Postgres has no analog for: it provisions the
|
||||
// schema (DDL + config-default seeds, idempotent), seeds the issue prefix and project
|
||||
// identity, and writes metadata.json with the password-stripped DSN. The password is
|
||||
// supplied at open time via BEADS_PG_PASSWORD and never lands on disk.
|
||||
func runInitPostgres(ctx context.Context, in initPostgresInput) error {
|
||||
dsn := in.pgURL
|
||||
if dsn == "" {
|
||||
dsn = os.Getenv("BEADS_POSTGRES_URL")
|
||||
}
|
||||
if dsn == "" {
|
||||
return fmt.Errorf("bd init --backend=postgres requires --pg-url=<dsn> (or BEADS_POSTGRES_URL); a password may be included for init but is never persisted — set BEADS_PG_PASSWORD for later commands")
|
||||
}
|
||||
schema := in.pgSchema
|
||||
if schema == "" {
|
||||
return fmt.Errorf("bd init --backend=postgres requires --pg-schema=<name> (the per-workspace schema for search_path isolation)")
|
||||
}
|
||||
|
||||
// Redact BEFORE provisioning: a connection string whose password we cannot safely
|
||||
// strip fails the whole init before any schema is created, and the password is
|
||||
// guaranteed never to reach metadata.json (RedactPassword verifies with pgx that
|
||||
// no password survives, failing closed).
|
||||
persistDSN, err := pgdialect.RedactPassword(dsn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot safely persist the Postgres connection: %w", err)
|
||||
}
|
||||
|
||||
store, err := postgres.Provision(ctx, dsn, schema)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize Postgres workspace: %w", err)
|
||||
}
|
||||
defer func() { _ = store.Close() }()
|
||||
|
||||
// Load any existing metadata.json up front so we can adopt its project identity.
|
||||
cfg, _ := configfile.Load(in.beadsDir)
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
|
||||
// Seed the issue prefix, only if unset — a shared schema may already carry one.
|
||||
// Matches the Dolt init path (dots normalized to underscores for valid IDs).
|
||||
if in.prefix != "" {
|
||||
if existing, _ := store.GetConfig(ctx, "issue_prefix"); existing == "" {
|
||||
issuePrefix := strings.ReplaceAll(in.prefix, ".", "_")
|
||||
if err := store.SetConfig(ctx, "issue_prefix", issuePrefix); err != nil {
|
||||
return fmt.Errorf("failed to set issue prefix: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Project identity: the DB is authoritative when it already carries one; otherwise
|
||||
// adopt the metadata.json id (a re-init or committed clone) or mint a fresh UUID.
|
||||
// Write it to BOTH the DB and metadata.json so the two can never diverge (GH#2372) —
|
||||
// the Dolt path keeps them in lockstep the same way.
|
||||
projectID, _ := store.GetMetadata(ctx, "_project_id")
|
||||
if projectID == "" {
|
||||
if cfg.ProjectID != "" {
|
||||
projectID = cfg.ProjectID
|
||||
} else {
|
||||
projectID = configfile.GenerateProjectID()
|
||||
}
|
||||
if err := store.SetMetadata(ctx, "_project_id", projectID); err != nil {
|
||||
return fmt.Errorf("failed to write project ID: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Persist metadata.json: backend + password-free DSN + schema + the DB's identity.
|
||||
cfg.Backend = configfile.BackendPostgres
|
||||
cfg.PostgresDSN = persistDSN
|
||||
cfg.PostgresSchema = schema
|
||||
cfg.ProjectID = projectID
|
||||
if err := cfg.Save(in.beadsDir); err != nil {
|
||||
return fmt.Errorf("failed to write metadata.json: %w", err)
|
||||
}
|
||||
|
||||
switch {
|
||||
case in.jsonOutput:
|
||||
out := map[string]any{
|
||||
"status": "initialized",
|
||||
"backend": configfile.BackendPostgres,
|
||||
"schema": schema,
|
||||
"prefix": in.prefix,
|
||||
}
|
||||
b, _ := json.MarshalIndent(out, "", " ")
|
||||
fmt.Println(string(b))
|
||||
case !in.quiet:
|
||||
fmt.Printf("%s bd initialized with the Postgres backend\n", ui.RenderPass("✓"))
|
||||
fmt.Printf(" Schema: %s\n", schema)
|
||||
if in.prefix != "" {
|
||||
fmt.Printf(" Prefix: %s\n", in.prefix)
|
||||
}
|
||||
fmt.Println(" History: not tracked (Postgres backend has no version control; use Dolt for history)")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// initMySQLInput carries the resolved inputs the MySQL init arm needs.
|
||||
type initMySQLInput struct {
|
||||
beadsDir string
|
||||
prefix string
|
||||
mysqlURL string
|
||||
mysqlDatabase string
|
||||
quiet bool
|
||||
jsonOutput bool
|
||||
}
|
||||
|
||||
// runInitMySQL finalizes a MySQL-backed workspace: it creates the per-workspace
|
||||
// database, applies the schema (config seeds on first provision), seeds the issue
|
||||
// prefix and project identity, and writes metadata.json with the password-stripped
|
||||
// DSN. The password is supplied at open time via BEADS_MYSQL_PASSWORD and never lands
|
||||
// on disk. MySQL isolates by database where Postgres isolates by schema.
|
||||
func runInitMySQL(ctx context.Context, in initMySQLInput) error {
|
||||
dsn := in.mysqlURL
|
||||
if dsn == "" {
|
||||
dsn = os.Getenv("BEADS_MYSQL_URL")
|
||||
}
|
||||
if dsn == "" {
|
||||
return fmt.Errorf("bd init --backend=mysql requires --mysql-url=<dsn> (or BEADS_MYSQL_URL), e.g. user:pass@tcp(host:3306)/ ; a password may be included for init but is never persisted — set BEADS_MYSQL_PASSWORD for later commands")
|
||||
}
|
||||
database := in.mysqlDatabase
|
||||
if database == "" {
|
||||
return fmt.Errorf("bd init --backend=mysql requires --mysql-database=<name> (the per-workspace database)")
|
||||
}
|
||||
|
||||
// Redact BEFORE provisioning so a DSN we cannot safely persist fails before any
|
||||
// side effect, and the password is guaranteed never to reach metadata.json.
|
||||
persistDSN, err := beadsmysql.RedactPassword(dsn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot safely persist the MySQL connection: %w", err)
|
||||
}
|
||||
|
||||
store, err := beadsmysql.Provision(ctx, dsn, database)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize MySQL workspace: %w", err)
|
||||
}
|
||||
defer func() { _ = store.Close() }()
|
||||
|
||||
cfg, _ := configfile.Load(in.beadsDir)
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
|
||||
if in.prefix != "" {
|
||||
if existing, _ := store.GetConfig(ctx, "issue_prefix"); existing == "" {
|
||||
issuePrefix := strings.ReplaceAll(in.prefix, ".", "_")
|
||||
if err := store.SetConfig(ctx, "issue_prefix", issuePrefix); err != nil {
|
||||
return fmt.Errorf("failed to set issue prefix: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
projectID, _ := store.GetMetadata(ctx, "_project_id")
|
||||
if projectID == "" {
|
||||
if cfg.ProjectID != "" {
|
||||
projectID = cfg.ProjectID
|
||||
} else {
|
||||
projectID = configfile.GenerateProjectID()
|
||||
}
|
||||
if err := store.SetMetadata(ctx, "_project_id", projectID); err != nil {
|
||||
return fmt.Errorf("failed to write project ID: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
cfg.Backend = configfile.BackendMySQL
|
||||
cfg.MySQLDSN = persistDSN
|
||||
cfg.MySQLDatabase = database
|
||||
cfg.ProjectID = projectID
|
||||
if err := cfg.Save(in.beadsDir); err != nil {
|
||||
return fmt.Errorf("failed to write metadata.json: %w", err)
|
||||
}
|
||||
|
||||
switch {
|
||||
case in.jsonOutput:
|
||||
out := map[string]any{
|
||||
"status": "initialized",
|
||||
"backend": configfile.BackendMySQL,
|
||||
"database": database,
|
||||
"prefix": in.prefix,
|
||||
}
|
||||
b, _ := json.MarshalIndent(out, "", " ")
|
||||
fmt.Println(string(b))
|
||||
case !in.quiet:
|
||||
fmt.Printf("%s bd initialized with the MySQL backend\n", ui.RenderPass("✓"))
|
||||
fmt.Printf(" Database: %s\n", database)
|
||||
if in.prefix != "" {
|
||||
fmt.Printf(" Prefix: %s\n", in.prefix)
|
||||
}
|
||||
fmt.Println(" History: not tracked (MySQL backend has no version control; use Dolt for history)")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// initSQLiteInput carries the resolved inputs the SQLite init arm needs.
|
||||
type initSQLiteInput struct {
|
||||
beadsDir string
|
||||
prefix string
|
||||
sqlitePath string
|
||||
quiet bool
|
||||
jsonOutput bool
|
||||
}
|
||||
|
||||
// runInitSQLite finalizes a SQLite-backed workspace: it provisions the database file
|
||||
// (default beads.db inside the beads dir), seeds the issue prefix and project identity,
|
||||
// and writes metadata.json. SQLite is file-based and embedded (pure-Go), so there is no
|
||||
// server, DSN, or password.
|
||||
func runInitSQLite(ctx context.Context, in initSQLiteInput) error {
|
||||
relPath := in.sqlitePath
|
||||
if relPath == "" {
|
||||
relPath = "beads.db"
|
||||
}
|
||||
dbPath := relPath
|
||||
if !filepath.IsAbs(dbPath) {
|
||||
dbPath = filepath.Join(in.beadsDir, dbPath)
|
||||
}
|
||||
|
||||
store, err := beadssqlite.Provision(ctx, dbPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize SQLite workspace: %w", err)
|
||||
}
|
||||
defer func() { _ = store.Close() }()
|
||||
|
||||
cfg, _ := configfile.Load(in.beadsDir)
|
||||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
|
||||
if in.prefix != "" {
|
||||
if existing, _ := store.GetConfig(ctx, "issue_prefix"); existing == "" {
|
||||
issuePrefix := strings.ReplaceAll(in.prefix, ".", "_")
|
||||
if err := store.SetConfig(ctx, "issue_prefix", issuePrefix); err != nil {
|
||||
return fmt.Errorf("failed to set issue prefix: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
projectID, _ := store.GetMetadata(ctx, "_project_id")
|
||||
if projectID == "" {
|
||||
if cfg.ProjectID != "" {
|
||||
projectID = cfg.ProjectID
|
||||
} else {
|
||||
projectID = configfile.GenerateProjectID()
|
||||
}
|
||||
if err := store.SetMetadata(ctx, "_project_id", projectID); err != nil {
|
||||
return fmt.Errorf("failed to write project ID: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
cfg.Backend = configfile.BackendSQLite
|
||||
cfg.SQLitePath = relPath
|
||||
cfg.ProjectID = projectID
|
||||
if err := cfg.Save(in.beadsDir); err != nil {
|
||||
return fmt.Errorf("failed to write metadata.json: %w", err)
|
||||
}
|
||||
|
||||
switch {
|
||||
case in.jsonOutput:
|
||||
out := map[string]any{
|
||||
"status": "initialized",
|
||||
"backend": configfile.BackendSQLite,
|
||||
"path": relPath,
|
||||
"prefix": in.prefix,
|
||||
}
|
||||
b, _ := json.MarshalIndent(out, "", " ")
|
||||
fmt.Println(string(b))
|
||||
case !in.quiet:
|
||||
fmt.Printf("%s bd initialized with the SQLite backend\n", ui.RenderPass("✓"))
|
||||
fmt.Printf(" Database: %s\n", relPath)
|
||||
if in.prefix != "" {
|
||||
fmt.Printf(" Prefix: %s\n", in.prefix)
|
||||
}
|
||||
fmt.Println(" History: not tracked (SQLite backend has no version control; use Dolt for history)")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
initCmd.Flags().StringP("prefix", "p", "", "Issue prefix (default: current directory name)")
|
||||
initCmd.Flags().BoolP("quiet", "q", false, "Suppress output (quiet mode)")
|
||||
|
|
@ -2140,13 +1775,16 @@ func init() {
|
|||
initCmd.Flags().Bool("non-interactive", false, "Skip all interactive prompts (auto-detected in CI or non-TTY environments)")
|
||||
initCmd.Flags().String("role", "", "Set beads role without prompting: \"maintainer\" or \"contributor\"")
|
||||
|
||||
// Backend selection: dolt (default) plus the SQL-family backends (postgres/mysql/sqlite).
|
||||
initCmd.Flags().String("backend", "", "Storage backend: dolt (default), postgres, mysql, or sqlite. See docs/architecture/storage-backends.md.")
|
||||
initCmd.Flags().String("pg-url", "", "Postgres connection URL (with --backend=postgres). A password may be included for init but is never persisted; set BEADS_PG_PASSWORD for later commands. Falls back to BEADS_POSTGRES_URL.")
|
||||
initCmd.Flags().String("pg-schema", "", "Postgres schema for this workspace's tables (with --backend=postgres; provides search_path isolation)")
|
||||
initCmd.Flags().String("mysql-url", "", "MySQL server DSN (with --backend=mysql), e.g. user:pass@tcp(host:3306)/ . A password may be included for init but is never persisted; set BEADS_MYSQL_PASSWORD for later commands. Falls back to BEADS_MYSQL_URL.")
|
||||
initCmd.Flags().String("mysql-database", "", "MySQL database for this workspace (with --backend=mysql; MySQL's isolation unit)")
|
||||
initCmd.Flags().String("sqlite-path", "", "SQLite database file (with --backend=sqlite; relative to the beads dir, default beads.db)")
|
||||
// Backend selection: Dolt is the default and only supported backend.
|
||||
initCmd.Flags().String("backend", "", "Storage backend: dolt (default). Removed backends (postgres, mysql, sqlite) print migration guidance.")
|
||||
// Keep the short-lived removed-backend flags as hidden parser tombstones. A
|
||||
// legacy invocation can then reach the backend rollback guidance instead of
|
||||
// failing early with an opaque "unknown flag" error. The current backend rejects
|
||||
// these flags explicitly above; they are never consumed as connection data.
|
||||
for _, legacyFlag := range removedBackendInitFlags {
|
||||
initCmd.Flags().String(legacyFlag.name, "", legacyFlag.usage)
|
||||
_ = initCmd.Flags().MarkHidden(legacyFlag.name)
|
||||
}
|
||||
|
||||
// Dolt server connection flags
|
||||
initCmd.Flags().Bool("server", false, "Use external dolt sql-server instead of embedded engine")
|
||||
|
|
@ -2179,6 +1817,19 @@ func init() {
|
|||
rootCmd.AddCommand(initCmd)
|
||||
}
|
||||
|
||||
var removedBackendInitFlags = []struct {
|
||||
name string
|
||||
usage string
|
||||
origin string
|
||||
rationale string
|
||||
}{
|
||||
{name: "pg-url", usage: "Legacy PostgreSQL connection URL (removed backend compatibility only)", origin: "the removed PostgreSQL/MySQL initialization paths", rationale: configfile.RemovedBackendRationale},
|
||||
{name: "pg-schema", usage: "Legacy PostgreSQL schema (removed backend compatibility only)", origin: "the removed PostgreSQL/MySQL initialization paths", rationale: configfile.RemovedBackendRationale},
|
||||
{name: "mysql-url", usage: "Legacy MySQL connection URL (removed backend compatibility only)", origin: "the removed PostgreSQL/MySQL initialization paths", rationale: configfile.RemovedBackendRationale},
|
||||
{name: "mysql-database", usage: "Legacy MySQL database (removed backend compatibility only)", origin: "the removed PostgreSQL/MySQL initialization paths", rationale: configfile.RemovedBackendRationale},
|
||||
{name: "sqlite-path", usage: "Legacy SQLite database file (removed backend compatibility only)", origin: "the removed SQLite initialization path", rationale: configfile.RemovedSQLiteRationale},
|
||||
}
|
||||
|
||||
// migrateOldDatabases detects and migrates old database files to beads.db
|
||||
func migrateOldDatabases(targetPath string, quiet bool) error {
|
||||
targetDir := filepath.Dir(targetPath)
|
||||
|
|
@ -2267,50 +1918,6 @@ func alreadyInitialized(format string, args ...any) error {
|
|||
// A returned error that matches errWorkspaceAlreadyInitialized means a database
|
||||
// already exists (the benign, idempotent-skip case); any other error is
|
||||
// operational and must not be treated as success.
|
||||
// checkExistingSQLBackend returns an "already initialized" guard error when cfg
|
||||
// selects a non-Dolt SQL backend (Postgres/MySQL/SQLite), each of which is marked
|
||||
// by metadata.json alone with no local DB directory to inspect. It returns nil for
|
||||
// the Dolt backend so the caller runs the directory/server checks instead. Split
|
||||
// out of checkExistingBeadsDataAt so the added SQL-backend branches don't inflate
|
||||
// that function's complexity.
|
||||
func checkExistingSQLBackend(cfg *configfile.Config) error {
|
||||
switch cfg.GetBackend() {
|
||||
case configfile.BackendPostgres:
|
||||
return alreadyInitialized(`
|
||||
%s This workspace is already initialized with the Postgres backend (schema %q).
|
||||
|
||||
To use it:
|
||||
Just run bd commands normally (e.g., %s)
|
||||
|
||||
To re-initialize (Postgres provisioning is idempotent):
|
||||
bd init --backend=postgres --reinit-local ...
|
||||
|
||||
Aborting.`, ui.RenderWarn("⚠"), cfg.GetPostgresSchema(), ui.RenderAccent("bd list"))
|
||||
case configfile.BackendMySQL:
|
||||
return alreadyInitialized(`
|
||||
%s This workspace is already initialized with the MySQL backend (database %q).
|
||||
|
||||
To use it:
|
||||
Just run bd commands normally (e.g., %s)
|
||||
|
||||
To re-initialize (MySQL provisioning is idempotent):
|
||||
bd init --backend=mysql --reinit-local ...
|
||||
|
||||
Aborting.`, ui.RenderWarn("⚠"), cfg.GetMySQLDatabase(), ui.RenderAccent("bd list"))
|
||||
case configfile.BackendSQLite:
|
||||
return alreadyInitialized(`
|
||||
%s This workspace is already initialized with the SQLite backend (%q).
|
||||
|
||||
To use it:
|
||||
Just run bd commands normally (e.g., %s)
|
||||
|
||||
To re-initialize (SQLite provisioning is idempotent):
|
||||
bd init --backend=sqlite --reinit-local ...
|
||||
|
||||
Aborting.`, ui.RenderWarn("⚠"), cfg.GetSQLitePath(), ui.RenderAccent("bd list"))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func checkExistingBeadsDataAt(beadsDir string, prefix string) error {
|
||||
// Check if .beads directory exists
|
||||
|
|
@ -2319,20 +1926,24 @@ func checkExistingBeadsDataAt(beadsDir string, prefix string) error {
|
|||
}
|
||||
|
||||
// metadata.json is authoritative for the configured backend, so resolve it once
|
||||
// and dispatch. A non-Dolt SQL backend (Postgres/MySQL/SQLite) is marked by
|
||||
// metadata alone — there is no local DB directory to inspect — so a plain
|
||||
// and dispatch. Removed-backend tombstones are marked by metadata
|
||||
// alone — there is no local Dolt directory to inspect — so a plain
|
||||
// `bd init` (which defaults to Dolt) must not silently repoint a live SQL
|
||||
// workspace to a fresh embedded Dolt DB and orphan its issues. --reinit-local
|
||||
// /--force bypass this (handled by the caller). A config that fails to load falls
|
||||
// through to the redirect/database-file checks below.
|
||||
// /--force bypass this (handled by the caller). Invalid metadata must fail closed:
|
||||
// without an explicit reinitialization request, init may not overwrite the only
|
||||
// marker for an external or otherwise nonlocal database.
|
||||
cfg, cfgErr := configfile.Load(beadsDir)
|
||||
if cfgErr == nil && cfg != nil {
|
||||
if guardErr := checkExistingSQLBackend(cfg); guardErr != nil {
|
||||
if cfgErr != nil {
|
||||
return fmt.Errorf("failed to load %s: %w; refusing to reinitialize automatically (restore the metadata or use --reinit-local after safeguarding existing data)", configfile.ConfigPath(beadsDir), cfgErr)
|
||||
}
|
||||
if cfg != nil {
|
||||
if guardErr := validateConfiguredBackend(cfg); guardErr != nil {
|
||||
return guardErr
|
||||
}
|
||||
}
|
||||
|
||||
if cfgErr == nil && cfg != nil && cfg.GetBackend() == configfile.BackendDolt {
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendDolt {
|
||||
if cfg.IsDoltProxiedServerMode() {
|
||||
proxiedRoot, rootErr := resolveProxiedServerRootPath(beadsDir)
|
||||
if rootErr != nil {
|
||||
|
|
|
|||
|
|
@ -1377,24 +1377,18 @@ func TestEmbeddedInit(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("sql_backend_flags", func(t *testing.T) {
|
||||
// SQLite is a supported pluggable backend now: init succeeds and records it.
|
||||
_, beadsDir, _ := bdInit(t, bd, "--prefix", "sqlt", "--backend", "sqlite")
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to load metadata.json: %v", err)
|
||||
}
|
||||
if cfg.Backend != configfile.BackendSQLite {
|
||||
t.Errorf("backend: got %q, want %q", cfg.Backend, configfile.BackendSQLite)
|
||||
// The SQLite backend was rolled back with the other alternative
|
||||
// backends: init fails closed with its own rationale.
|
||||
sqliteOut := bdInitFail(t, bd, "--backend", "sqlite")
|
||||
if !strings.Contains(sqliteOut, "no longer supported") || !strings.Contains(sqliteOut, "single engine") {
|
||||
t.Errorf("sqlite should report the backend rollback: %s", sqliteOut)
|
||||
}
|
||||
|
||||
// Postgres is recognized, not an unknown backend: it fails only because the
|
||||
// required connection config is absent (bdEnv strips any ambient DSN).
|
||||
pgOut := bdInitFail(t, bd, "--backend", "postgres")
|
||||
if strings.Contains(pgOut, "unknown backend") {
|
||||
t.Errorf("postgres should be recognized, got unknown-backend error: %s", pgOut)
|
||||
}
|
||||
if !strings.Contains(pgOut, "--pg-url") {
|
||||
t.Errorf("postgres init should request --pg-url, got: %s", pgOut)
|
||||
for _, backend := range []string{"postgres", "mysql"} {
|
||||
out := bdInitFail(t, bd, "--backend", backend)
|
||||
if !strings.Contains(out, "no longer supported") {
|
||||
t.Errorf("%s should report the backend rollback: %s", backend, out)
|
||||
}
|
||||
}
|
||||
|
||||
// A genuinely unsupported backend is still rejected.
|
||||
|
|
|
|||
|
|
@ -2369,10 +2369,9 @@ func initBackendTestEnv(beadsDir string) []string {
|
|||
func TestInitBackendFlag(t *testing.T) {
|
||||
bd := buildBDForInitTests(t)
|
||||
|
||||
// SQLite is a supported pluggable backend now (it was removed under the
|
||||
// one-backend assumption in #3151 and re-added as a leaf backend): init
|
||||
// succeeds and metadata.json records backend=sqlite.
|
||||
t.Run("sqlite_initializes_and_persists_backend", func(t *testing.T) {
|
||||
// The SQLite backend was rolled back with the other alternative backends:
|
||||
// init fails closed with migration guidance and writes no workspace state.
|
||||
t.Run("sqlite_is_no_longer_supported", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
|
||||
|
|
@ -2380,45 +2379,41 @@ func TestInitBackendFlag(t *testing.T) {
|
|||
cmd.Dir = tmpDir
|
||||
cmd.Env = initBackendTestEnv(beadsDir)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
t.Fatalf("bd init --backend=sqlite should succeed: %v\n%s", err, out)
|
||||
}
|
||||
|
||||
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 after --backend=sqlite init")
|
||||
}
|
||||
if cfg.Backend != configfile.BackendSQLite {
|
||||
t.Errorf("Expected backend %q, got %q", configfile.BackendSQLite, cfg.Backend)
|
||||
}
|
||||
})
|
||||
|
||||
// Postgres is a recognized backend now: it must not be rejected as unknown,
|
||||
// and it fails only because the required connection config is absent.
|
||||
t.Run("postgres_is_recognized_and_requires_config", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
|
||||
cmd := exec.Command(bd, "init", "--backend", "postgres", "--quiet")
|
||||
cmd.Dir = tmpDir
|
||||
cmd.Env = initBackendTestEnv(beadsDir)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatal("Expected non-zero exit for --backend=postgres without connection config")
|
||||
t.Fatalf("Expected non-zero exit for --backend=sqlite:\n%s", out)
|
||||
}
|
||||
|
||||
outStr := string(out)
|
||||
if strings.Contains(outStr, "unknown backend") {
|
||||
t.Errorf("postgres must be a recognized backend, not reported unknown: %s", outStr)
|
||||
if !strings.Contains(outStr, "no longer supported") || !strings.Contains(outStr, "single engine") {
|
||||
t.Errorf("Expected rollback guidance for sqlite, got: %s", outStr)
|
||||
}
|
||||
if !strings.Contains(outStr, "--pg-url") {
|
||||
t.Errorf("Expected missing --pg-url guidance for postgres, got: %s", outStr)
|
||||
if _, statErr := os.Stat(beadsDir); !os.IsNotExist(statErr) {
|
||||
t.Fatalf("rejected sqlite init created workspace state (stat error: %v)", statErr)
|
||||
}
|
||||
})
|
||||
|
||||
for _, backend := range []string{"postgres", "mysql"} {
|
||||
t.Run(backend+"_is_no_longer_supported", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
|
||||
cmd := exec.Command(bd, "init", "--backend", backend, "--quiet")
|
||||
cmd.Dir = tmpDir
|
||||
cmd.Env = initBackendTestEnv(beadsDir)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatalf("Expected non-zero exit for --backend=%s", backend)
|
||||
}
|
||||
|
||||
outStr := string(out)
|
||||
if !strings.Contains(outStr, "no longer supported") {
|
||||
t.Errorf("Expected rollback guidance for %s, got: %s", backend, outStr)
|
||||
}
|
||||
if !strings.Contains(outStr, `"dolt"`) {
|
||||
t.Errorf("Expected supported backend guidance for %s, got: %s", backend, outStr)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// A genuinely unsupported backend value is still rejected up front.
|
||||
t.Run("unknown_backend_errors", func(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
|
|
|
|||
232
cmd/bd/main.go
232
cmd/bd/main.go
|
|
@ -33,11 +33,7 @@ import (
|
|||
"github.com/steveyegge/beads/internal/routing"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
mysqlstore "github.com/steveyegge/beads/internal/storage/mysql"
|
||||
"github.com/steveyegge/beads/internal/storage/pgdialect"
|
||||
pgstore "github.com/steveyegge/beads/internal/storage/postgres"
|
||||
"github.com/steveyegge/beads/internal/storage/schema"
|
||||
sqlitestore "github.com/steveyegge/beads/internal/storage/sqlite"
|
||||
"github.com/steveyegge/beads/internal/storage/uow"
|
||||
"github.com/steveyegge/beads/internal/telemetry"
|
||||
"github.com/steveyegge/beads/internal/ui"
|
||||
|
|
@ -992,9 +988,13 @@ var rootCmd = &cobra.Command{
|
|||
|
||||
if dbPath == "" {
|
||||
if bd := beads.FindBeadsDir(); bd != "" {
|
||||
if cfg, _ := configfile.Load(bd); cfg != nil && (cfg.IsDoltProxiedServerMode() || cfg.GetBackend() == configfile.BackendPostgres || cfg.GetBackend() == configfile.BackendMySQL || cfg.GetBackend() == configfile.BackendSQLite) {
|
||||
// A non-Dolt SQL (or proxied-server) workspace has no local Dolt
|
||||
// database file; the .beads dir with metadata.json IS the workspace.
|
||||
cfg, cfgErr := configfile.Load(bd)
|
||||
if cfgErr != nil || cfg != nil && (cfg.IsDoltProxiedServerMode() || !configfile.IsSupportedBackend(cfg.Backend)) {
|
||||
// Proxied-server and removed-backend workspaces may have no
|
||||
// local Dolt database file. Invalid or unknown metadata likewise must
|
||||
// reach config validation instead of becoming a generic "no database"
|
||||
// result. metadata.json identifies the workspace so store selection can
|
||||
// route or reject it explicitly.
|
||||
dbPath = bd
|
||||
}
|
||||
}
|
||||
|
|
@ -1076,6 +1076,18 @@ var rootCmd = &cobra.Command{
|
|||
return HandleError("%v", err)
|
||||
}
|
||||
|
||||
// Resolve the backend before version tracking, migration, server startup, or
|
||||
// any store construction. PostgreSQL/MySQL values are retained as metadata
|
||||
// tombstones so an existing workspace fails closed instead of falling through
|
||||
// to a new, empty Dolt database.
|
||||
cfg, cfgErr := configfile.Load(beadsDir)
|
||||
if cfgErr != nil {
|
||||
return HandleError("failed to load beads config from %s: %v (refusing to fall back to the embedded store; fix or restore metadata.json and retry)", beadsDir, cfgErr)
|
||||
}
|
||||
if backendErr := validateConfiguredBackend(cfg); backendErr != nil {
|
||||
return HandleError("%v", backendErr)
|
||||
}
|
||||
|
||||
// Set actor for audit trail
|
||||
actor = getActorWithGit()
|
||||
// Attach actor to the command span now that we have it.
|
||||
|
|
@ -1134,10 +1146,6 @@ var rootCmd = &cobra.Command{
|
|||
// result set and exit 0 (false-empty), which readers misinterpret as
|
||||
// "no work". Absent metadata.json (cfg == nil, cfgErr == nil) keeps
|
||||
// the fresh-repo embedded default below.
|
||||
cfg, cfgErr := configfile.Load(beadsDir)
|
||||
if cfgErr != nil {
|
||||
return HandleError("failed to load beads config from %s: %v (refusing to fall back to the embedded store; fix or restore metadata.json and retry)", beadsDir, cfgErr)
|
||||
}
|
||||
if cfg != nil {
|
||||
warnSharedServerEmbeddedMismatch(cfg)
|
||||
doltCfg.ProxiedServer = cfg.IsDoltProxiedServerMode()
|
||||
|
|
@ -1179,9 +1187,7 @@ var rootCmd = &cobra.Command{
|
|||
// command must not run (or fail) embedded opens even when the env var is set.
|
||||
// Dolt-only: the gateway credential command mints a Dolt server
|
||||
// username. IsSharedServerMode() forces ServerMode true with no backend
|
||||
// guard, so without this check a shared-server config in config.yaml
|
||||
// would run (and fail closed) the command for postgres/mysql/sqlite
|
||||
// workspaces, which never present a server username.
|
||||
// guard, so non-Dolt metadata must not try to resolve a server username.
|
||||
if doltCfg.ServerMode && cfg.GetBackend() == configfile.BackendDolt {
|
||||
if _, credErr := dolt.ApplyGatewayCredential(rootCtx, cfg, doltCfg); credErr != nil {
|
||||
return HandleError("resolving dolt credential command: %v", credErr)
|
||||
|
|
@ -1275,19 +1281,7 @@ var rootCmd = &cobra.Command{
|
|||
// Removing them WILL cause unrecoverable data corruption and data loss.
|
||||
// Dolt manages these files itself; external interference is never safe.
|
||||
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendPostgres {
|
||||
// Postgres backend: open via the SQL-family bundle, bypassing the
|
||||
// Dolt open path (the doltCfg above is built but unused here).
|
||||
store, err = pgstore.NewFromConfig(rootCtx, beadsDir)
|
||||
} else if cfg != nil && cfg.GetBackend() == configfile.BackendMySQL {
|
||||
// MySQL backend: same SQL-family bundle, isolation by database.
|
||||
store, err = mysqlstore.NewFromConfig(rootCtx, beadsDir)
|
||||
} else if cfg != nil && cfg.GetBackend() == configfile.BackendSQLite {
|
||||
// SQLite backend: pure-Go file-based SQL-family bundle.
|
||||
store, err = sqlitestore.NewFromConfig(rootCtx, beadsDir)
|
||||
} else {
|
||||
store, err = newDoltStore(rootCtx, doltCfg)
|
||||
}
|
||||
store, err = newDoltStore(rootCtx, doltCfg)
|
||||
|
||||
// Track final read-only state for staleness checks (GH#1089)
|
||||
storeIsReadOnly = doltCfg.ReadOnly
|
||||
|
|
@ -1394,72 +1388,60 @@ var rootCmd = &cobra.Command{
|
|||
uowProvider = nil
|
||||
}
|
||||
} else {
|
||||
// Slice 5c: a NonCommitGraphBackend (Postgres, SQLite, ...) has no Dolt
|
||||
// commit graph, so skip the Dolt-only maintenance tail. UnwrapStore reaches
|
||||
// the concrete store past the HookFiringStore decorator; a store WITHOUT the
|
||||
// marker (every Dolt variant) leaves skipMaintenance false and runs the tail
|
||||
// unchanged. store.Close below stays unconditional.
|
||||
skipMaintenance := false
|
||||
if ncg, ok := storage.UnwrapStore(store).(storage.NonCommitGraphBackend); ok && ncg.CommitGraphUnsupported() {
|
||||
skipMaintenance = true
|
||||
// Dolt auto-commit: after a successful write command (and after final flush),
|
||||
// create a Dolt commit so changes don't remain only in the working set.
|
||||
if commandDidWrite.Load() && !commandDidExplicitDoltCommit {
|
||||
if err := maybeAutoCommit(rootCtx, doltAutoCommitParams{Command: cmd.Name()}); err != nil {
|
||||
return HandleError("dolt auto-commit failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if !skipMaintenance {
|
||||
// Dolt auto-commit: after a successful write command (and after final flush),
|
||||
// create a Dolt commit so changes don't remain only in the working set.
|
||||
if commandDidWrite.Load() && !commandDidExplicitDoltCommit {
|
||||
if err := maybeAutoCommit(rootCtx, doltAutoCommitParams{Command: cmd.Name()}); err != nil {
|
||||
return HandleError("dolt auto-commit failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Tip metadata auto-commit: if a tip was shown, create a separate Dolt commit for the
|
||||
// tip_*_last_shown metadata updates. This may happen even for otherwise read-only commands.
|
||||
if commandDidWriteTipMetadata && len(commandTipIDsShown) > 0 {
|
||||
// Only applies when dolt auto-commit is enabled and backend is versioned (Dolt).
|
||||
if mode, err := getDoltAutoCommitMode(); err != nil {
|
||||
return HandleError("dolt tip auto-commit failed: %v", err)
|
||||
} else if mode == doltAutoCommitOn {
|
||||
// Apply tip metadata writes now (deferred in recordTipShown for Dolt).
|
||||
for tipID := range commandTipIDsShown {
|
||||
key := fmt.Sprintf("tip_%s_last_shown", tipID)
|
||||
value := time.Now().Format(time.RFC3339)
|
||||
if err := store.SetLocalMetadata(rootCtx, key, value); err != nil {
|
||||
return HandleError("dolt tip auto-commit failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
ids := make([]string, 0, len(commandTipIDsShown))
|
||||
for tipID := range commandTipIDsShown {
|
||||
ids = append(ids, tipID)
|
||||
}
|
||||
msg := formatDoltAutoCommitMessage("tip", getActor(), ids)
|
||||
if err := maybeAutoCommit(rootCtx, doltAutoCommitParams{Command: "tip", MessageOverride: msg}); err != nil {
|
||||
// Tip metadata auto-commit: if a tip was shown, create a separate Dolt commit for the
|
||||
// tip_*_last_shown metadata updates. This may happen even for otherwise read-only commands.
|
||||
if commandDidWriteTipMetadata && len(commandTipIDsShown) > 0 {
|
||||
// Only applies when dolt auto-commit is enabled and backend is versioned (Dolt).
|
||||
if mode, err := getDoltAutoCommitMode(); err != nil {
|
||||
return HandleError("dolt tip auto-commit failed: %v", err)
|
||||
} else if mode == doltAutoCommitOn {
|
||||
// Apply tip metadata writes now (deferred in recordTipShown for Dolt).
|
||||
for tipID := range commandTipIDsShown {
|
||||
key := fmt.Sprintf("tip_%s_last_shown", tipID)
|
||||
value := time.Now().Format(time.RFC3339)
|
||||
if err := store.SetLocalMetadata(rootCtx, key, value); err != nil {
|
||||
return HandleError("dolt tip auto-commit failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Auto-backup: sync a Dolt-native backup if enabled and due
|
||||
maybeAutoBackup(rootCtx)
|
||||
|
||||
// Auto-export: write git-tracked JSONL for portability if enabled and due.
|
||||
// Read-only commands must not perform post-run maintenance writes or emit
|
||||
// sync guidance after machine-readable output.
|
||||
if shouldRunPostCommandAutoExport(cmd) {
|
||||
if err := maybeAutoExport(rootCtx, commandAllowsEmptyAutoExport(cmd)); err != nil {
|
||||
return HandleError("%v", err)
|
||||
ids := make([]string, 0, len(commandTipIDsShown))
|
||||
for tipID := range commandTipIDsShown {
|
||||
ids = append(ids, tipID)
|
||||
}
|
||||
msg := formatDoltAutoCommitMessage("tip", getActor(), ids)
|
||||
if err := maybeAutoCommit(rootCtx, doltAutoCommitParams{Command: "tip", MessageOverride: msg}); err != nil {
|
||||
return HandleError("dolt tip auto-commit failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Auto-push: push to Dolt remote if enabled and due.
|
||||
// Skip for read-only commands to avoid unnecessary network operations
|
||||
// and metadata writes on commands like bd list/show/ready (GH#2191).
|
||||
if !isReadOnlyCommand(cmd.Name()) {
|
||||
maybeAutoPush(rootCtx)
|
||||
// Auto-backup: sync a Dolt-native backup if enabled and due
|
||||
maybeAutoBackup(rootCtx)
|
||||
|
||||
// Auto-export: write git-tracked JSONL for portability if enabled and due.
|
||||
// Read-only commands must not perform post-run maintenance writes or emit
|
||||
// sync guidance after machine-readable output.
|
||||
if shouldRunPostCommandAutoExport(cmd) {
|
||||
if err := maybeAutoExport(rootCtx, commandAllowsEmptyAutoExport(cmd)); err != nil {
|
||||
return HandleError("%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Auto-push: push to Dolt remote if enabled and due.
|
||||
// Skip for read-only commands to avoid unnecessary network operations
|
||||
// and metadata writes on commands like bd list/show/ready (GH#2191).
|
||||
if !isReadOnlyCommand(cmd.Name()) {
|
||||
maybeAutoPush(rootCtx)
|
||||
}
|
||||
|
||||
// Signal that store is closing (prevents background flush from accessing closed store)
|
||||
storeMutex.Lock()
|
||||
storeActive = false
|
||||
|
|
@ -1572,7 +1554,7 @@ func checkBlockedEnvVars() error {
|
|||
for _, name := range blockedEnvVars {
|
||||
if os.Getenv(name) != "" {
|
||||
return fmt.Errorf("%s env var is not supported and has been removed to prevent data fragmentation.\n"+
|
||||
"The storage backend is set in .beads/metadata.json. To change it, use: bd migrate dolt", name)
|
||||
"Unset %s; storage selection comes from .beads/metadata.json. To choose a different supported backend, follow 'bd help init-safety'; do not edit metadata.json by hand", name, name)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
|
@ -1747,15 +1729,9 @@ func envTruthyValue(v string) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
// dsnFlags are the flags whose value is a connection string that may embed a
|
||||
// password (bd init --backend=postgres/mysql). Their values get the full
|
||||
// parser-backed DSN scrub; other args only get the unambiguous userinfo scrub.
|
||||
var dsnFlags = map[string]bool{"--pg-url": true, "--mysql-url": true}
|
||||
|
||||
// secretFlagNames are long flag names whose entire value is an opaque credential
|
||||
// that must never reach the bd.args telemetry span. Unlike dsnFlags (a DSN that is
|
||||
// parsed so its structure survives), a secret flag's value is redacted wholesale.
|
||||
// Only federation add-peer's --password currently qualifies. Its shorthand (-p) is
|
||||
// that must never reach the bd.args telemetry span. The flag's value is redacted
|
||||
// wholesale. Only federation add-peer's --password currently qualifies. Its shorthand (-p) is
|
||||
// resolved per command via secretFlagTokens so the same letter bound to
|
||||
// --priority/--prefix/--parallel on other commands is never redacted.
|
||||
var secretFlagNames = map[string]bool{"password": true}
|
||||
|
|
@ -1784,40 +1760,38 @@ func secretFlagTokens(cmd *cobra.Command) map[string]bool {
|
|||
}
|
||||
|
||||
// scrubArgsForTelemetry joins argv for the bd.args span attribute with any
|
||||
// credential-bearing values redacted. --pg-url/--mysql-url may carry a password
|
||||
// for init, and federation add-peer --password/-p carries a SQL password;
|
||||
// RedactPassword protects metadata.json, but the raw argv would otherwise leak the
|
||||
// secret into the telemetry root span and trace logs.
|
||||
//
|
||||
// A DSN flag's value is scrubbed through the parser-backed pgdialect logic so every
|
||||
// password form pgx accepts is caught — URL userinfo, URL `?password=`/`?sslpassword=`
|
||||
// query params, and libpq `password=`/`sslpassword=` keyword/value tokens — in both
|
||||
// the `--pg-url=<dsn>` and `--pg-url <dsn>` spellings. A secretFlags token's value is
|
||||
// an opaque credential and is redacted wholesale across the `--password <v>`,
|
||||
// credential-bearing values redacted. A secretFlags token's value is redacted
|
||||
// wholesale across the `--password <v>`,
|
||||
// `--password=<v>`, `-p <v>`, `-p=<v>`, and `-p<v>` spellings pflag accepts. Every
|
||||
// other arg still gets the narrow user:PASS@host userinfo scrub as defense in depth,
|
||||
// without the broad keyword scan that would over-redact ordinary text.
|
||||
// other arg gets a conservative DSN/userinfo scrub as defense in depth so a
|
||||
// positional connection string cannot leak a password.
|
||||
func scrubArgsForTelemetry(argv []string, secretFlags map[string]bool) string {
|
||||
parts := make([]string, len(argv))
|
||||
redactNext := false
|
||||
for i, a := range argv {
|
||||
if name, val, ok := strings.Cut(a, "="); ok {
|
||||
if dsnFlags[name] {
|
||||
// --pg-url=<dsn> — scrub only the value, keep the flag name.
|
||||
parts[i] = name + "=" + scrubDSNValue(val)
|
||||
continue
|
||||
}
|
||||
if redactNext {
|
||||
parts[i] = "xxxxx"
|
||||
redactNext = false
|
||||
continue
|
||||
}
|
||||
if name, value, ok := strings.Cut(a, "="); ok {
|
||||
if secretFlags[name] {
|
||||
// --password=<secret> / -p=<secret> — redact the whole value.
|
||||
parts[i] = name + "=xxxxx"
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(name, "-") {
|
||||
scrubbed := scrubUserinfoPassword(scrubPotentialDSNPasswords(value))
|
||||
if scrubbed != value {
|
||||
// Preserve an arbitrary flag name while parsing its equals-value as
|
||||
// a possible DSN. Passing the whole token to url.Parse would treat
|
||||
// the flag prefix as the URL scheme and miss query credentials.
|
||||
parts[i] = name + "=" + scrubbed
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
if i > 0 {
|
||||
if dsnFlags[argv[i-1]] {
|
||||
// <dsn> following a bare --pg-url token.
|
||||
parts[i] = scrubDSNValue(a)
|
||||
continue
|
||||
}
|
||||
if secretFlags[argv[i-1]] {
|
||||
// <secret> following a bare --password / -p token.
|
||||
parts[i] = "xxxxx"
|
||||
|
|
@ -1829,7 +1803,14 @@ func scrubArgsForTelemetry(argv []string, secretFlags map[string]bool) string {
|
|||
parts[i] = short + "xxxxx"
|
||||
continue
|
||||
}
|
||||
parts[i] = scrubUserinfoPassword(a)
|
||||
if secretShorthandTakesSeparateValue(a, secretFlags) {
|
||||
// -qp <secret> — a boolean shorthand cluster ending in the
|
||||
// value-taking secret shorthand, with its value in the next token.
|
||||
parts[i] = a
|
||||
redactNext = true
|
||||
continue
|
||||
}
|
||||
parts[i] = scrubUserinfoPassword(scrubPotentialDSNPasswords(a))
|
||||
}
|
||||
return strings.Join(parts, " ")
|
||||
}
|
||||
|
|
@ -1868,12 +1849,23 @@ func secretShorthandPrefix(a string, secretFlags map[string]bool) (string, bool)
|
|||
return "", false
|
||||
}
|
||||
|
||||
// scrubDSNValue redacts every password form from a connection-string value. The
|
||||
// pgdialect pass covers pgx's URL (userinfo + query) and libpq keyword/value shapes;
|
||||
// the trailing userinfo pass covers the MySQL user:PASS@tcp(...) DSN, which is not a
|
||||
// scheme URL and so is invisible to the pgdialect extractor.
|
||||
func scrubDSNValue(val string) string {
|
||||
return scrubUserinfoPassword(pgdialect.ScrubDSNString(val, val))
|
||||
// secretShorthandTakesSeparateValue recognizes a boolean-shorthand cluster that
|
||||
// ends in a registered secret shorthand with no attached value. For example,
|
||||
// pflag parses "-qp secret" as -q followed by -p=secret.
|
||||
func secretShorthandTakesSeparateValue(a string, secretFlags map[string]bool) bool {
|
||||
if len(a) < 3 || a[0] != '-' || a[1] == '-' {
|
||||
return false
|
||||
}
|
||||
for i := 1; i < len(a); i++ {
|
||||
c := a[i]
|
||||
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
|
||||
return false
|
||||
}
|
||||
if secretFlags["-"+string(c)] {
|
||||
return i == len(a)-1
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// scrubUserinfoPassword redacts the password in a URL/DSN userinfo section
|
||||
|
|
|
|||
|
|
@ -142,6 +142,12 @@ func TestBlockedEnvVars(t *testing.T) {
|
|||
if err != nil && !strings.Contains(err.Error(), tt.envVar) {
|
||||
t.Errorf("expected error to mention %s, got: %v", tt.envVar, err)
|
||||
}
|
||||
if err != nil && !strings.Contains(err.Error(), "bd help init-safety") {
|
||||
t.Errorf("expected error to point to safe reinitialization guidance, got: %v", err)
|
||||
}
|
||||
if err != nil && strings.Contains(err.Error(), "bd migrate dolt") {
|
||||
t.Errorf("error must not recommend the removed backend-conversion command: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ var sqlCmd = &cobra.Command{
|
|||
Use: "sql <query>",
|
||||
GroupID: "maint",
|
||||
Short: "Execute raw SQL against the beads database",
|
||||
Long: `Execute a raw SQL query against the underlying database (SQLite or Dolt).
|
||||
Long: `Execute a raw SQL query against the underlying database (Dolt).
|
||||
|
||||
Useful for debugging, maintenance, and working around bugs in higher-level commands.
|
||||
|
||||
|
|
|
|||
|
|
@ -15,9 +15,6 @@ import (
|
|||
"github.com/steveyegge/beads/internal/storage/dbproxy/util"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
"github.com/steveyegge/beads/internal/storage/embeddeddolt"
|
||||
beadsmysql "github.com/steveyegge/beads/internal/storage/mysql"
|
||||
"github.com/steveyegge/beads/internal/storage/postgres"
|
||||
beadssqlite "github.com/steveyegge/beads/internal/storage/sqlite"
|
||||
)
|
||||
|
||||
func usesSQLServer() bool {
|
||||
|
|
@ -110,14 +107,8 @@ func newDoltStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltS
|
|||
// metadata.json (cfg == nil, err == nil) keeps the embedded default.
|
||||
return nil, fmt.Errorf("load %s: %w (refusing to fall back to the embedded store)", configfile.ConfigPath(beadsDir), err)
|
||||
}
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendPostgres {
|
||||
return postgres.NewFromConfig(ctx, beadsDir)
|
||||
}
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendMySQL {
|
||||
return beadsmysql.NewFromConfig(ctx, beadsDir)
|
||||
}
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendSQLite {
|
||||
return beadssqlite.NewFromConfig(ctx, beadsDir)
|
||||
if err := validateConfiguredBackend(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cfg != nil && cfg.IsDoltProxiedServerMode() {
|
||||
// TODO: this needs to be uow provider
|
||||
|
|
@ -199,17 +190,8 @@ func newReadOnlyStoreFromConfig(ctx context.Context, beadsDir string) (storage.D
|
|||
// "database not found" the embedded open would produce.
|
||||
return nil, fmt.Errorf("load %s: %w (refusing to fall back to the embedded store)", configfile.ConfigPath(beadsDir), err)
|
||||
}
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendPostgres {
|
||||
// Postgres has no read-only open mode in the wedge; a normal open is fine
|
||||
// (reads don't mutate, and search_path is per-workspace).
|
||||
return postgres.NewFromConfig(ctx, beadsDir)
|
||||
}
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendMySQL {
|
||||
// MySQL likewise has no separate read-only open in the wedge; reads don't mutate.
|
||||
return beadsmysql.NewFromConfig(ctx, beadsDir)
|
||||
}
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendSQLite {
|
||||
return beadssqlite.NewFromConfig(ctx, beadsDir)
|
||||
if err := validateConfiguredBackend(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cfg != nil && cfg.IsDoltProxiedServerMode() {
|
||||
// TODO: this needs to be uow provider
|
||||
|
|
|
|||
|
|
@ -10,9 +10,6 @@ import (
|
|||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/dbproxy/util"
|
||||
"github.com/steveyegge/beads/internal/storage/dolt"
|
||||
beadsmysql "github.com/steveyegge/beads/internal/storage/mysql"
|
||||
"github.com/steveyegge/beads/internal/storage/postgres"
|
||||
beadssqlite "github.com/steveyegge/beads/internal/storage/sqlite"
|
||||
)
|
||||
|
||||
func usesSQLServer() bool {
|
||||
|
|
@ -57,17 +54,8 @@ func newDoltStoreFromConfig(ctx context.Context, beadsDir string) (storage.DoltS
|
|||
// message below.
|
||||
return nil, fmt.Errorf("load %s: %w", configfile.ConfigPath(beadsDir), err)
|
||||
}
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendPostgres {
|
||||
// Postgres needs no CGO (pure-Go pgx), so it works in the nocgo build too.
|
||||
return postgres.NewFromConfig(ctx, beadsDir)
|
||||
}
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendMySQL {
|
||||
// MySQL (go-sql-driver) needs no CGO either.
|
||||
return beadsmysql.NewFromConfig(ctx, beadsDir)
|
||||
}
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendSQLite {
|
||||
// SQLite (modernc.org/sqlite) is pure-Go; no CGO.
|
||||
return beadssqlite.NewFromConfig(ctx, beadsDir)
|
||||
if err := validateConfiguredBackend(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cfg != nil && cfg.IsDoltProxiedServerMode() {
|
||||
// TODO: this needs to be uow provider
|
||||
|
|
@ -90,14 +78,8 @@ func newReadOnlyStoreFromConfig(ctx context.Context, beadsDir string) (storage.D
|
|||
if err != nil {
|
||||
return nil, fmt.Errorf("load %s: %w", configfile.ConfigPath(beadsDir), err)
|
||||
}
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendPostgres {
|
||||
return postgres.NewFromConfig(ctx, beadsDir)
|
||||
}
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendMySQL {
|
||||
return beadsmysql.NewFromConfig(ctx, beadsDir)
|
||||
}
|
||||
if cfg != nil && cfg.GetBackend() == configfile.BackendSQLite {
|
||||
return beadssqlite.NewFromConfig(ctx, beadsDir)
|
||||
if err := validateConfiguredBackend(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cfg != nil && cfg.IsDoltProxiedServerMode() {
|
||||
// TODO: this needs to be uow provider
|
||||
|
|
|
|||
420
cmd/bd/store_selection_removed_backend_test.go
Normal file
420
cmd/bd/store_selection_removed_backend_test.go
Normal file
|
|
@ -0,0 +1,420 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
)
|
||||
|
||||
func TestCommandStartupRejectsRemovedBackendsBeforeLocalWrites(t *testing.T) {
|
||||
bd := buildBDForInitTests(t)
|
||||
for _, backend := range []string{configfile.BackendPostgres, configfile.BackendMySQL, configfile.BackendSQLite} {
|
||||
t.Run(backend, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
beadsDir := filepath.Join(root, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
|
||||
t.Fatalf("create beads dir: %v", err)
|
||||
}
|
||||
if err := (&configfile.Config{Backend: backend}).Save(beadsDir); err != nil {
|
||||
t.Fatalf("save config: %v", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(bd, "list", "--json")
|
||||
cmd.Dir = root
|
||||
cmd.Env = append(removedBackendTestEnv(beadsDir), "BD_DISABLE_METRICS=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatalf("removed backend %s unexpectedly opened", backend)
|
||||
}
|
||||
message := strings.ToLower(string(out))
|
||||
for _, want := range removedBackendWantSubstrings(backend) {
|
||||
if !strings.Contains(message, want) {
|
||||
t.Errorf("startup error for %s missing %q: %s", backend, want, message)
|
||||
}
|
||||
}
|
||||
|
||||
for _, name := range []string{"embeddeddolt", "dolt", "beads.db", ".local_version"} {
|
||||
path := filepath.Join(beadsDir, name)
|
||||
if _, statErr := os.Stat(path); !os.IsNotExist(statErr) {
|
||||
t.Fatalf("removed backend %s created local state at %s (stat error: %v)", backend, path, statErr)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCommandStartupRejectsUnknownBackendBeforeLocalWrites(t *testing.T) {
|
||||
bd := buildBDForInitTests(t)
|
||||
root := t.TempDir()
|
||||
beadsDir := filepath.Join(root, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
||||
t.Fatalf("create .beads: %v", err)
|
||||
}
|
||||
if err := (&configfile.Config{Backend: "mystery"}).Save(beadsDir); err != nil {
|
||||
t.Fatalf("save unknown-backend metadata: %v", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(bd, "list", "--json")
|
||||
cmd.Dir = root
|
||||
cmd.Env = append(removedBackendTestEnv(beadsDir), "BD_DISABLE_METRICS=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatalf("unknown backend unexpectedly opened:\n%s", out)
|
||||
}
|
||||
message := string(out)
|
||||
if strings.Contains(message, "no beads database found") {
|
||||
t.Fatalf("unknown backend stopped at generic discovery instead of metadata guidance:\n%s", out)
|
||||
}
|
||||
for _, want := range []string{"not recognized", "opened or modified", "dolt"} {
|
||||
if !strings.Contains(message, want) {
|
||||
t.Errorf("unknown-backend error missing %q:\n%s", want, out)
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"embeddeddolt", "dolt", "beads.db", ".local_version"} {
|
||||
if _, statErr := os.Stat(filepath.Join(beadsDir, name)); !os.IsNotExist(statErr) {
|
||||
t.Fatalf("unknown backend created %s (stat error: %v)", name, statErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitGuardRejectsCorruptMetadataBeforeWorkspaceWrites(t *testing.T) {
|
||||
beadsDir := filepath.Join(t.TempDir(), ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
||||
t.Fatalf("create .beads: %v", err)
|
||||
}
|
||||
metadataPath := filepath.Join(beadsDir, configfile.ConfigFileName)
|
||||
metadata := []byte("{\n")
|
||||
if err := os.WriteFile(metadataPath, metadata, 0o600); err != nil {
|
||||
t.Fatalf("write corrupt metadata.json: %v", err)
|
||||
}
|
||||
|
||||
err := checkExistingBeadsDataAt(beadsDir, "guard")
|
||||
if err == nil || !strings.Contains(err.Error(), "metadata.json") {
|
||||
t.Fatalf("init guard error = %v, want corrupt metadata refusal", err)
|
||||
}
|
||||
after, readErr := os.ReadFile(metadataPath)
|
||||
if readErr != nil {
|
||||
t.Fatalf("read metadata after refusal: %v", readErr)
|
||||
}
|
||||
if string(after) != string(metadata) {
|
||||
t.Fatalf("init guard rewrote corrupt metadata: before %q, after %q", metadata, after)
|
||||
}
|
||||
for _, name := range []string{"embeddeddolt", "dolt", "beads.db", ".local_version"} {
|
||||
if _, statErr := os.Stat(filepath.Join(beadsDir, name)); !os.IsNotExist(statErr) {
|
||||
t.Fatalf("init guard created %s (stat error: %v)", name, statErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoctorRejectsUnknownBackendBeforeDoltChecks(t *testing.T) {
|
||||
bd := buildBDForInitTests(t)
|
||||
root := t.TempDir()
|
||||
beadsDir := filepath.Join(root, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
||||
t.Fatalf("create .beads: %v", err)
|
||||
}
|
||||
cfg := &configfile.Config{
|
||||
Backend: "mystery",
|
||||
DoltMode: configfile.DoltModeServer,
|
||||
DoltServerHost: "127.0.0.1",
|
||||
DoltServerPort: 1,
|
||||
}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("save unknown-backend metadata: %v", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(bd, "doctor", "--server", "--json")
|
||||
cmd.Dir = root
|
||||
cmd.Env = append(removedBackendTestEnv(beadsDir), "BEADS_DOLT_AUTO_START=0", "BD_DISABLE_METRICS=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatalf("doctor unexpectedly ran Dolt checks for unknown backend:\n%s", out)
|
||||
}
|
||||
for _, want := range []string{"not recognized", "no storage database was opened or modified", "dolt"} {
|
||||
if !strings.Contains(string(out), want) {
|
||||
t.Errorf("doctor unknown-backend error missing %q:\n%s", want, out)
|
||||
}
|
||||
}
|
||||
for _, name := range []string{"embeddeddolt", "dolt", "beads.db", "dolt-server.pid", "dolt-server.port"} {
|
||||
if _, statErr := os.Stat(filepath.Join(beadsDir, name)); !os.IsNotExist(statErr) {
|
||||
t.Fatalf("doctor created %s for unknown backend (stat error: %v)", name, statErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLegacyInitFlagsReachRemovedBackendGuidance(t *testing.T) {
|
||||
bd := buildBDForInitTests(t)
|
||||
tests := []struct {
|
||||
backend string
|
||||
args []string
|
||||
wants []string
|
||||
}{
|
||||
{
|
||||
backend: configfile.BackendPostgres,
|
||||
args: []string{
|
||||
"init", "--backend=postgres",
|
||||
"--pg-url=postgres://beads@127.0.0.1:5432/beads",
|
||||
"--pg-schema=legacy_workspace",
|
||||
},
|
||||
wants: []string{"no longer supported", "general-purpose server databases", "simple and resource-light", "dolt"},
|
||||
},
|
||||
{
|
||||
backend: configfile.BackendMySQL,
|
||||
args: []string{
|
||||
"init", "--backend=mysql",
|
||||
"--mysql-url=beads@tcp(127.0.0.1:3306)/",
|
||||
"--mysql-database=legacy_workspace",
|
||||
},
|
||||
wants: []string{"no longer supported", "general-purpose server databases", "simple and resource-light", "dolt"},
|
||||
},
|
||||
{
|
||||
backend: configfile.BackendSQLite,
|
||||
args: []string{
|
||||
"init", "--backend=sqlite",
|
||||
"--sqlite-path=beads.db",
|
||||
},
|
||||
wants: []string{"no longer supported", "single engine", "dolt"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.backend, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
beadsDir := filepath.Join(root, ".beads")
|
||||
cmd := exec.Command(bd, tt.args...)
|
||||
cmd.Dir = root
|
||||
cmd.Env = append(removedBackendTestEnv(beadsDir), "BD_DISABLE_METRICS=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatalf("legacy %s init unexpectedly succeeded: %s", tt.backend, out)
|
||||
}
|
||||
|
||||
message := string(out)
|
||||
if strings.Contains(message, "unknown flag") {
|
||||
t.Errorf("legacy %s init stopped at argument parsing instead of rollback guidance: %s", tt.backend, message)
|
||||
}
|
||||
for _, want := range tt.wants {
|
||||
if !strings.Contains(message, want) {
|
||||
t.Errorf("legacy %s init error missing %q: %s", tt.backend, want, message)
|
||||
}
|
||||
}
|
||||
if _, statErr := os.Stat(beadsDir); !os.IsNotExist(statErr) {
|
||||
t.Fatalf("rejected legacy %s init created workspace state (stat error: %v)", tt.backend, statErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoltAdministrativeCommandsRejectRemovedBackends(t *testing.T) {
|
||||
bd := buildBDForInitTests(t)
|
||||
commands := []struct {
|
||||
name string
|
||||
args []string
|
||||
}{
|
||||
{name: "show", args: []string{"dolt", "show"}},
|
||||
{name: "status", args: []string{"dolt", "status"}},
|
||||
{name: "set", args: []string{"dolt", "set", "host", "127.0.0.1"}},
|
||||
{name: "test", args: []string{"dolt", "test"}},
|
||||
{name: "start", args: []string{"dolt", "start"}},
|
||||
{name: "stop", args: []string{"dolt", "stop"}},
|
||||
{name: "killall", args: []string{"dolt", "killall"}},
|
||||
{name: "clean-databases", args: []string{"dolt", "clean-databases", "--dry-run"}},
|
||||
}
|
||||
|
||||
for _, backend := range []string{configfile.BackendPostgres, configfile.BackendMySQL, configfile.BackendSQLite} {
|
||||
t.Run(backend, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
beadsDir := filepath.Join(root, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o755); err != nil {
|
||||
t.Fatalf("create beads dir: %v", err)
|
||||
}
|
||||
if err := (&configfile.Config{Backend: backend}).Save(beadsDir); err != nil {
|
||||
t.Fatalf("save config: %v", err)
|
||||
}
|
||||
|
||||
for _, command := range commands {
|
||||
t.Run(command.name, func(t *testing.T) {
|
||||
cmd := exec.Command(bd, command.args...)
|
||||
cmd.Dir = root
|
||||
cmd.Env = append(removedBackendTestEnv(beadsDir), "BD_DISABLE_METRICS=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatalf("%q unexpectedly succeeded for removed backend %s: %s", strings.Join(command.args, " "), backend, out)
|
||||
}
|
||||
|
||||
message := strings.ToLower(string(out))
|
||||
for _, want := range removedBackendWantSubstrings(backend) {
|
||||
if !strings.Contains(message, want) {
|
||||
t.Errorf("%q error for %s missing %q: %s", strings.Join(command.args, " "), backend, want, message)
|
||||
}
|
||||
}
|
||||
|
||||
for _, name := range []string{"embeddeddolt", "dolt", "beads.db", ".local_version"} {
|
||||
path := filepath.Join(beadsDir, name)
|
||||
if _, statErr := os.Stat(path); !os.IsNotExist(statErr) {
|
||||
t.Fatalf("%q created local state for removed backend %s at %s (stat error: %v)", strings.Join(command.args, " "), backend, path, statErr)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoltReadOnlyCommandsRejectUnknownBackend(t *testing.T) {
|
||||
bd := buildBDForInitTests(t)
|
||||
root := t.TempDir()
|
||||
beadsDir := filepath.Join(root, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
||||
t.Fatalf("create .beads: %v", err)
|
||||
}
|
||||
if err := (&configfile.Config{Backend: "mystery", DoltMode: configfile.DoltModeServer, DoltServerPort: 1}).Save(beadsDir); err != nil {
|
||||
t.Fatalf("save unknown-backend metadata: %v", err)
|
||||
}
|
||||
|
||||
for _, args := range [][]string{{"dolt", "show"}, {"dolt", "status"}, {"dolt", "killall"}} {
|
||||
cmd := exec.Command(bd, args...)
|
||||
cmd.Dir = root
|
||||
cmd.Env = append(removedBackendTestEnv(beadsDir), "BEADS_DOLT_AUTO_START=0", "BD_DISABLE_METRICS=1")
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Errorf("bd %s unexpectedly treated unknown metadata as Dolt:\n%s", strings.Join(args, " "), out)
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(string(out), "not recognized") || !strings.Contains(string(out), "opened or modified") {
|
||||
t.Errorf("bd %s error lacks fail-closed guidance:\n%s", strings.Join(args, " "), out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoltKillallRejectsCorruptMetadataBeforeServerCleanup(t *testing.T) {
|
||||
bd := buildBDForInitTests(t)
|
||||
root := t.TempDir()
|
||||
beadsDir := filepath.Join(root, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o700); err != nil {
|
||||
t.Fatalf("create .beads: %v", err)
|
||||
}
|
||||
metadata := []byte("{\n")
|
||||
metadataPath := filepath.Join(beadsDir, configfile.ConfigFileName)
|
||||
if err := os.WriteFile(metadataPath, metadata, 0o600); err != nil {
|
||||
t.Fatalf("write corrupt metadata: %v", err)
|
||||
}
|
||||
|
||||
cmd := exec.Command(bd, "dolt", "killall")
|
||||
cmd.Dir = root
|
||||
cmd.Env = append(removedBackendTestEnv(beadsDir),
|
||||
"BEADS_DOLT_SHARED_SERVER=1",
|
||||
"BEADS_DOLT_AUTO_START=0",
|
||||
"BEADS_SHARED_SERVER_DIR="+filepath.Join(root, "shared-server"),
|
||||
"BD_DISABLE_METRICS=1",
|
||||
)
|
||||
out, err := cmd.CombinedOutput()
|
||||
if err == nil {
|
||||
t.Fatalf("bd dolt killall unexpectedly ignored corrupt metadata:\n%s", out)
|
||||
}
|
||||
message := strings.ToLower(string(out))
|
||||
for _, want := range []string{"loading config", "parsing config"} {
|
||||
if !strings.Contains(message, want) {
|
||||
t.Errorf("bd dolt killall corrupt-metadata error missing %q:\n%s", want, out)
|
||||
}
|
||||
}
|
||||
after, readErr := os.ReadFile(metadataPath)
|
||||
if readErr != nil {
|
||||
t.Fatalf("read metadata after refusal: %v", readErr)
|
||||
}
|
||||
if string(after) != string(metadata) {
|
||||
t.Fatalf("bd dolt killall rewrote corrupt metadata: before %q, after %q", metadata, after)
|
||||
}
|
||||
for _, name := range []string{"embeddeddolt", "dolt", "beads.db", ".local_version", "dolt-server.pid", "dolt-server.port"} {
|
||||
if _, statErr := os.Stat(filepath.Join(beadsDir, name)); !os.IsNotExist(statErr) {
|
||||
t.Fatalf("bd dolt killall created %s before rejecting corrupt metadata (stat error: %v)", name, statErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func removedBackendTestEnv(beadsDir string) []string {
|
||||
var env []string
|
||||
for _, entry := range os.Environ() {
|
||||
if strings.HasPrefix(entry, "BEADS_") || strings.HasPrefix(entry, "BD_") {
|
||||
continue
|
||||
}
|
||||
env = append(env, entry)
|
||||
}
|
||||
return append(env, "BEADS_DIR="+beadsDir)
|
||||
}
|
||||
|
||||
func TestStoreFactoriesRemovedBackendsFailLoud(t *testing.T) {
|
||||
for _, backend := range []string{configfile.BackendPostgres, configfile.BackendMySQL, configfile.BackendSQLite} {
|
||||
t.Run(backend, func(t *testing.T) {
|
||||
beadsDir := t.TempDir()
|
||||
cfg := &configfile.Config{Backend: backend}
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("save config: %v", err)
|
||||
}
|
||||
|
||||
factories := map[string]func(context.Context, string) (interface{ Close() error }, error){
|
||||
"read-write": func(ctx context.Context, dir string) (interface{ Close() error }, error) {
|
||||
return newDoltStoreFromConfig(ctx, dir)
|
||||
},
|
||||
"read-only": func(ctx context.Context, dir string) (interface{ Close() error }, error) {
|
||||
return newReadOnlyStoreFromConfig(ctx, dir)
|
||||
},
|
||||
}
|
||||
|
||||
for name, open := range factories {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
store, err := open(t.Context(), beadsDir)
|
||||
if err == nil {
|
||||
if store != nil {
|
||||
_ = store.Close()
|
||||
}
|
||||
t.Fatalf("expected removed-backend error for %s", backend)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "no longer supported") {
|
||||
t.Fatalf("error should explain that %s is no longer supported: %v", backend, err)
|
||||
}
|
||||
guidance := strings.ToLower(err.Error())
|
||||
if !strings.Contains(guidance, "export") || !strings.Contains(guidance, "dolt") {
|
||||
t.Fatalf("error should provide safe migration guidance: %v", err)
|
||||
}
|
||||
for _, name := range []string{"embeddeddolt", "dolt", "beads.db"} {
|
||||
path := filepath.Join(beadsDir, name)
|
||||
if _, statErr := os.Stat(path); !os.IsNotExist(statErr) {
|
||||
t.Fatalf("removed backend %s created local storage at %s (stat error: %v)", backend, path, statErr)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequireDoltBackend(t *testing.T) {
|
||||
for _, cfg := range []*configfile.Config{nil, {}, {Backend: configfile.BackendDolt}} {
|
||||
if err := requireDoltBackend(cfg); err != nil {
|
||||
t.Fatalf("Dolt config %#v rejected: %v", cfg, err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, backend := range []string{configfile.BackendPostgres, configfile.BackendMySQL, configfile.BackendSQLite} {
|
||||
err := requireDoltBackend(&configfile.Config{Backend: backend})
|
||||
if err == nil || !strings.Contains(err.Error(), "no longer supported") || !strings.Contains(err.Error(), "export") {
|
||||
t.Fatalf("removed backend %q guard error = %v, want rollback and migration guidance", backend, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// removedBackendWantSubstrings returns the guidance substrings every removed-backend
|
||||
// rejection must carry, including the backend-specific rationale marker.
|
||||
func removedBackendWantSubstrings(backend string) []string {
|
||||
wants := []string{"no longer supported", "was not opened", "export", "dolt"}
|
||||
if backend == configfile.BackendSQLite {
|
||||
return append(wants, "single engine")
|
||||
}
|
||||
return append(wants, "general-purpose server databases", "simple and resource-light")
|
||||
}
|
||||
129
cmd/bd/telemetry_redact.go
Normal file
129
cmd/bd/telemetry_redact.go
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// These expressions recognize password-bearing libpq keyword/value tokens. The
|
||||
// scrubber is intentionally backend-neutral: command-line telemetry may receive a
|
||||
// connection string even when bd does not implement that connection's backend.
|
||||
const dsnWhitespace = `\t\n\f\r \v`
|
||||
|
||||
var dsnPasswordValue = regexp.MustCompile(`(?i)(?:^|[` + dsnWhitespace + `])(?:password|sslpassword)[` + dsnWhitespace + `]*=[` + dsnWhitespace + `]*(?:'((?:[^'\\]|\\.)*)'|((?:[^` + dsnWhitespace + `\\]|\\.)+))`)
|
||||
|
||||
// A libpq keyword/value connection string begins with a recognized connection
|
||||
// parameter. Requiring the first key to be recognized avoids treating ordinary
|
||||
// prose such as "transport=rail password=..." as a DSN while still covering valid
|
||||
// minimal forms such as "password=..." and service-file forms.
|
||||
var dsnLeadingKeyword = regexp.MustCompile(`(?i)^[` + dsnWhitespace + `]*(?:application_name|channel_binding|client_encoding|connect_timeout|dbname|fallback_application_name|gssencmode|gsslib|host|hostaddr|keepalives|keepalives_count|keepalives_idle|keepalives_interval|krbsrvname|load_balance_hosts|options|passfile|password|port|replication|requirepeer|service|servicefile|sslcert|sslcrl|sslcrldir|sslkey|sslmode|sslpassword|sslrootcert|sslsni|ssl_max_protocol_version|ssl_min_protocol_version|target_session_attrs|tcp_user_timeout|user)[` + dsnWhitespace + `]*=`)
|
||||
|
||||
func scrubStructuredDSNPasswords(dsn, text string) string {
|
||||
secrets := structuredDSNPasswords(dsn)
|
||||
sort.Slice(secrets, func(i, j int) bool { return len(secrets[i]) > len(secrets[j]) })
|
||||
for _, secret := range secrets {
|
||||
text = strings.ReplaceAll(text, secret, "xxxxx")
|
||||
}
|
||||
return text
|
||||
}
|
||||
|
||||
func scrubPotentialDSNPasswords(value string) string {
|
||||
if strings.Contains(value, "://") {
|
||||
return scrubStructuredDSNPasswords(value, value)
|
||||
}
|
||||
if dsnLeadingKeyword.MatchString(value) {
|
||||
return scrubStructuredDSNPasswords(value, value)
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func structuredDSNPasswords(dsn string) []string {
|
||||
seen := make(map[string]bool)
|
||||
var values []string
|
||||
add := func(value string) {
|
||||
if value == "" || seen[value] {
|
||||
return
|
||||
}
|
||||
seen[value] = true
|
||||
values = append(values, value)
|
||||
}
|
||||
|
||||
if parsed, err := url.Parse(dsn); err == nil && parsed.Scheme != "" {
|
||||
if parsed.User != nil {
|
||||
if password, ok := parsed.User.Password(); ok {
|
||||
add(password)
|
||||
add(rawURLUserinfoPassword(dsn))
|
||||
}
|
||||
}
|
||||
}
|
||||
// Extract query credentials independently of url.Parse. A malformed escape in
|
||||
// the path, userinfo, or fragment makes url.Parse reject the entire URL even
|
||||
// when the password query pair itself is valid. Telemetry redaction must fail
|
||||
// closed in that case.
|
||||
addRawURLQueryPasswords(dsn, add)
|
||||
|
||||
for _, match := range dsnPasswordValue.FindAllStringSubmatch(dsn, -1) {
|
||||
add(match[1])
|
||||
add(match[2])
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func addRawURLQueryPasswords(dsn string, add func(string)) {
|
||||
queryStart := strings.IndexByte(dsn, '?')
|
||||
if queryStart < 0 {
|
||||
return
|
||||
}
|
||||
query := dsn[queryStart+1:]
|
||||
if fragment := strings.IndexByte(query, '#'); fragment >= 0 {
|
||||
query = query[:fragment]
|
||||
}
|
||||
for _, pair := range strings.Split(query, "&") {
|
||||
key, value, ok := strings.Cut(pair, "=")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
decodedKey, err := url.QueryUnescape(key)
|
||||
if err != nil {
|
||||
decodedKey = key
|
||||
}
|
||||
if !isDSNPasswordKey(key) && !isDSNPasswordKey(decodedKey) {
|
||||
continue
|
||||
}
|
||||
add(value)
|
||||
if decoded, err := url.QueryUnescape(value); err == nil {
|
||||
add(decoded)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isDSNPasswordKey(key string) bool {
|
||||
switch strings.ToLower(key) {
|
||||
case "password", "sslpassword":
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func rawURLUserinfoPassword(dsn string) string {
|
||||
scheme := strings.Index(dsn, "://")
|
||||
if scheme < 0 {
|
||||
return ""
|
||||
}
|
||||
authority := dsn[scheme+len("://"):]
|
||||
if end := strings.IndexAny(authority, "/?#"); end >= 0 {
|
||||
authority = authority[:end]
|
||||
}
|
||||
at := strings.LastIndex(authority, "@")
|
||||
if at < 0 {
|
||||
return ""
|
||||
}
|
||||
_, password, ok := strings.Cut(authority[:at], ":")
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
return password
|
||||
}
|
||||
|
|
@ -7,12 +7,10 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// TestScrubArgsForTelemetry locks the invariant that no password embedded in a
|
||||
// --pg-url/--mysql-url value survives into the bd.args telemetry span, across every
|
||||
// DSN password form pgx/go-sql-driver accept and both the --flag=value and
|
||||
// --flag value spellings. The pre-fix scrubber handled only URL userinfo, so
|
||||
// query-param and libpq keyword/value passwords (the confirmed red-team vectors)
|
||||
// leaked verbatim.
|
||||
// TestScrubArgsForTelemetry locks the invariant that a positional connection
|
||||
// string cannot leak an embedded password into the bd.args telemetry span. This
|
||||
// defense is backend-neutral and remains useful even when bd does not implement
|
||||
// the connection string's database backend.
|
||||
func TestScrubArgsForTelemetry(t *testing.T) {
|
||||
const secret = "s3cr3t-pw"
|
||||
cases := []struct {
|
||||
|
|
@ -21,34 +19,44 @@ func TestScrubArgsForTelemetry(t *testing.T) {
|
|||
keep []string // non-secret structure that must survive redaction
|
||||
}{
|
||||
{
|
||||
name: "pg url userinfo =form",
|
||||
argv: []string{"init", "--backend=postgres", "--pg-url=postgres://bts:" + secret + "@127.0.0.1:5432/db"},
|
||||
keep: []string{"--pg-url=postgres://bts:", "127.0.0.1:5432/db", "--backend=postgres"},
|
||||
name: "url userinfo",
|
||||
argv: []string{"command", "postgres://bts:" + secret + "@127.0.0.1:5432/db"},
|
||||
keep: []string{"postgres://bts:", "127.0.0.1:5432/db"},
|
||||
},
|
||||
{
|
||||
name: "pg url query param space form",
|
||||
argv: []string{"init", "--pg-url", "postgres://bts@127.0.0.1:5432/db?password=" + secret},
|
||||
name: "url query param",
|
||||
argv: []string{"command", "postgres://bts@127.0.0.1:5432/db?password=" + secret},
|
||||
keep: []string{"127.0.0.1:5432/db", "password="},
|
||||
},
|
||||
{
|
||||
name: "pg url sslpassword =form",
|
||||
argv: []string{"init", "--pg-url=postgres://bts@h:5432/db?sslpassword=" + secret + "&sslmode=require"},
|
||||
name: "url sslpassword",
|
||||
argv: []string{"command", "postgres://bts@h:5432/db?sslpassword=" + secret + "&sslmode=require"},
|
||||
keep: []string{"sslmode=require"},
|
||||
},
|
||||
{
|
||||
name: "pg libpq keyword/value =form single token",
|
||||
argv: []string{"init", "--pg-url=host=127.0.0.1 user=bts password=" + secret + " dbname=db"},
|
||||
name: "keyword value connection string",
|
||||
argv: []string{"command", "host=127.0.0.1 user=bts password=" + secret + " dbname=db"},
|
||||
keep: []string{"host=127.0.0.1", "user=bts", "dbname=db"},
|
||||
},
|
||||
{
|
||||
name: "mysql userinfo space form",
|
||||
argv: []string{"init", "--mysql-url", "bts:" + secret + "@tcp(127.0.0.1:3306)/db"},
|
||||
keep: []string{"tcp(127.0.0.1:3306)/db", "bts:"},
|
||||
name: "keyword value connection string with whitespace around equals",
|
||||
argv: []string{"command", "host = 127.0.0.1 password=" + secret},
|
||||
keep: []string{"host = 127.0.0.1", "password="},
|
||||
},
|
||||
{
|
||||
name: "mysql userinfo =form",
|
||||
argv: []string{"init", "--mysql-url=bts:" + secret + "@tcp(127.0.0.1:3306)/db"},
|
||||
keep: []string{"--mysql-url=bts:", "tcp(127.0.0.1:3306)/db"},
|
||||
name: "password-only keyword connection string",
|
||||
argv: []string{"command", "password=" + secret},
|
||||
keep: []string{"password="},
|
||||
},
|
||||
{
|
||||
name: "service keyword connection string",
|
||||
argv: []string{"command", "service=production password=" + secret},
|
||||
keep: []string{"service=production", "password="},
|
||||
},
|
||||
{
|
||||
name: "scheme-less userinfo",
|
||||
argv: []string{"command", "bts:" + secret + "@tcp(127.0.0.1:3306)/db"},
|
||||
keep: []string{"tcp(127.0.0.1:3306)/db", "bts:"},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
|
|
@ -78,11 +86,24 @@ func TestScrubArgsForTelemetryLeavesOrdinaryArgs(t *testing.T) {
|
|||
if out := scrubArgsForTelemetry(argv, nil); out != "create --title document the password= knob" {
|
||||
t.Fatalf("over-redacted ordinary args: got %q", out)
|
||||
}
|
||||
argv = []string{"create", "--title", "transport=rail password=foo"}
|
||||
if out := scrubArgsForTelemetry(argv, nil); out != "create --title transport=rail password=foo" {
|
||||
t.Fatalf("over-redacted non-DSN key/value text: got %q", out)
|
||||
}
|
||||
|
||||
argv = []string{"weird", "postgres://u:leak@h:5432/db"}
|
||||
if out := scrubArgsForTelemetry(argv, nil); strings.Contains(out, "leak") {
|
||||
t.Fatalf("userinfo password not scrubbed as defense in depth: %q", out)
|
||||
}
|
||||
|
||||
for _, arg := range []string{
|
||||
"postgres://u@h:5432/db?password=leak",
|
||||
"host=h user=u password=leak dbname=db",
|
||||
} {
|
||||
if out := scrubArgsForTelemetry([]string{"weird", arg}, nil); strings.Contains(out, "leak") {
|
||||
t.Fatalf("positional DSN password not scrubbed as defense in depth: %q", out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestSecretFlagTokensResolvesByCommand proves -p is treated as secret only on the
|
||||
|
|
@ -190,6 +211,14 @@ func TestScrubArgsForTelemetryRedactsShorthandCluster(t *testing.T) {
|
|||
name: "boolean shorthand -v then -p cluster",
|
||||
argv: []string{"federation", "add-peer", "partner", "h:3306/db", "-vp" + secret},
|
||||
},
|
||||
{
|
||||
name: "boolean shorthand -q then separate -p value",
|
||||
argv: []string{"federation", "add-peer", "partner", "h:3306/db", "-qp", secret},
|
||||
},
|
||||
{
|
||||
name: "boolean shorthand -v then separate -p value",
|
||||
argv: []string{"federation", "add-peer", "partner", "h:3306/db", "-vp", secret},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
|
@ -220,3 +249,194 @@ func TestScrubArgsForTelemetryKeepsOverloadedShortFlag(t *testing.T) {
|
|||
t.Fatalf("over-redacted non-secret -p=prefix: got %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTelemetryRedactionURLInFlagEqualsValue covers URL values embedded in a
|
||||
// --flag=value argv element. The flag prefix must not prevent URL query parsing.
|
||||
func TestTelemetryRedactionURLInFlagEqualsValue(t *testing.T) {
|
||||
const secret = "query-secret"
|
||||
arg := "--remote=postgres://bts@db.example/beads?password=" + secret
|
||||
out := scrubArgsForTelemetry([]string{"init", arg}, nil)
|
||||
if strings.Contains(out, secret) {
|
||||
t.Fatalf("PASSWORD LEAK: URL query password in --flag=value survived telemetry scrub: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTelemetryRedactionMalformedURLQuery fails closed when a URL-shaped DSN has
|
||||
// an invalid percent escape. net/url rejects the whole URL in that case, but the
|
||||
// query password must still never reach the bd.args telemetry span.
|
||||
func TestTelemetryRedactionMalformedURLQuery(t *testing.T) {
|
||||
const secret = "query-secret"
|
||||
arg := "postgres://bts@db.example/beads%zz?password=" + secret + "&sslmode=require"
|
||||
out := scrubArgsForTelemetry([]string{"command", arg}, nil)
|
||||
if strings.Contains(out, secret) {
|
||||
t.Fatalf("PASSWORD LEAK: malformed URL query password survived telemetry scrub: %q", out)
|
||||
}
|
||||
if !strings.Contains(out, "password=xxxxx") || !strings.Contains(out, "sslmode=require") {
|
||||
t.Fatalf("malformed URL query structure was not preserved after redaction: %q", out)
|
||||
}
|
||||
}
|
||||
|
||||
// TestScrubArgsForTelemetryAdversarialDSNVectors ports the adversarial regression
|
||||
// vectors from the deleted internal/storage/pgdialect redaction tests
|
||||
// (redact_error_test.go, redact_matrix_test.go). Each vector was a real leak or a
|
||||
// scrubber-bypass near miss in an earlier redactor, so they are pinned here against
|
||||
// the argv scrubber — the telemetry sink is backend-neutral and keeps receiving
|
||||
// these DSN shapes even though the backends that motivated them are gone.
|
||||
//
|
||||
// Historical failure modes locked in, per case:
|
||||
// - percent-encoded userinfo/query passwords must vanish in BOTH the raw
|
||||
// (SUPER%2ASECRET) and decoded (SUPER*SECRET) forms;
|
||||
// - percent-encoded query KEYS (pass%77ord, sslpass%77ord, %70assword) decode to
|
||||
// password keys and bypassed key matching done on the raw key only;
|
||||
// - password=SUPER\ SECRET: pgx accepts the backslash-escaped space as part of a
|
||||
// single value, but a \S+-shaped value regex stopped at the space and shipped
|
||||
// with the tail " SECRET" left in cleartext;
|
||||
// - a single-quoted libpq password containing spaces is one value, not several;
|
||||
// - pgx treats \v as inter-token whitespace, but Go's regexp \s class does not
|
||||
// include \v, so a (^|\s)-anchored password regex never matched after \v and the
|
||||
// password leaked entirely;
|
||||
// - when one collected secret is a prefix of another (userinfo "foo", query
|
||||
// "fooACTUAL"), replacing the short secret first turned "fooACTUAL" into
|
||||
// "xxxxxACTUAL" and the tail survived; replacement must be length-descending.
|
||||
func TestScrubArgsForTelemetryAdversarialDSNVectors(t *testing.T) {
|
||||
const secret = "SUPERSECRET"
|
||||
cases := []struct {
|
||||
name string
|
||||
dsn string
|
||||
leaks []string // every cleartext form that must NOT survive redaction
|
||||
keep []string // non-secret structure that must survive redaction
|
||||
}{
|
||||
{
|
||||
name: "url userinfo percent-encoded password",
|
||||
dsn: "postgres://u:SUPER%2ASECRET@h:5432/db",
|
||||
leaks: []string{"SUPER%2ASECRET", "SUPER*SECRET"},
|
||||
keep: []string{"postgres://u:", "h:5432/db"},
|
||||
},
|
||||
{
|
||||
name: "url userinfo percent-encoded special chars",
|
||||
dsn: "postgres://bts:p%40ss%3Aw%2Frd-" + secret + "@127.0.0.1:5432/db",
|
||||
leaks: []string{"p%40ss%3Aw%2Frd-" + secret, "p@ss:w/rd-" + secret},
|
||||
keep: []string{"postgres://bts:", "127.0.0.1:5432/db"},
|
||||
},
|
||||
{
|
||||
name: "url query param percent-encoded value",
|
||||
dsn: "postgres://u@h:5432/db?password=SUPER%2ASECRET",
|
||||
leaks: []string{"SUPER%2ASECRET", "SUPER*SECRET"},
|
||||
keep: []string{"password=", "h:5432/db"},
|
||||
},
|
||||
{
|
||||
name: "url query param percent-encoded key (pass%77ord)",
|
||||
dsn: "postgres://u@h:5432/db?pass%77ord=" + secret,
|
||||
leaks: []string{secret},
|
||||
keep: []string{"pass%77ord=", "h:5432/db"},
|
||||
},
|
||||
{
|
||||
name: "url query sslpassword percent-encoded key (sslpass%77ord)",
|
||||
dsn: "postgres://u@h:5432/db?sslpass%77ord=" + secret + "&sslmode=require",
|
||||
leaks: []string{secret},
|
||||
keep: []string{"sslpass%77ord=", "sslmode=require"},
|
||||
},
|
||||
{
|
||||
name: "url query param fully percent-encoded key (%70assword)",
|
||||
dsn: "postgres://u@h:5432/db?%70assword=" + secret,
|
||||
leaks: []string{secret},
|
||||
keep: []string{"%70assword=", "h:5432/db"},
|
||||
},
|
||||
{
|
||||
name: "url query password with sslmode preserved",
|
||||
dsn: "postgres://bts@127.0.0.1:5432/db?password=" + secret + "&sslmode=disable",
|
||||
leaks: []string{secret},
|
||||
keep: []string{"sslmode=disable", "127.0.0.1:5432/db"},
|
||||
},
|
||||
{
|
||||
name: "url userinfo password with sslmode and application_name preserved",
|
||||
dsn: "postgres://bts:" + secret + "@127.0.0.1:5432/db?sslmode=require&application_name=beads",
|
||||
leaks: []string{secret},
|
||||
keep: []string{"sslmode=require", "application_name=beads", "127.0.0.1:5432/db"},
|
||||
},
|
||||
{
|
||||
name: "url userinfo password but no user",
|
||||
dsn: "postgres://:" + secret + "@h:5432/db",
|
||||
leaks: []string{secret},
|
||||
keep: []string{"h:5432/db"},
|
||||
},
|
||||
{
|
||||
name: "keyword value password with backslash-escaped whitespace",
|
||||
dsn: `host=h user=u password=SUPER\ SECRET dbname=db`,
|
||||
// "SECRET" pins the historical shipped bug: the tail after the escaped
|
||||
// space must not survive in cleartext.
|
||||
leaks: []string{"SECRET", `SUPER\`},
|
||||
keep: []string{"host=h", "user=u", "dbname=db"},
|
||||
},
|
||||
{
|
||||
name: "keyword value single-quoted password containing spaces",
|
||||
dsn: "host=h user=u password='se cret " + secret + "' dbname=db",
|
||||
leaks: []string{"se cret", secret},
|
||||
keep: []string{"host=h", "user=u", "dbname=db"},
|
||||
},
|
||||
{
|
||||
name: "keyword value single-quoted password",
|
||||
dsn: "host=h user=u password='" + secret + "' dbname=db",
|
||||
leaks: []string{secret},
|
||||
keep: []string{"host=h", "user=u", "dbname=db"},
|
||||
},
|
||||
{
|
||||
name: "keyword value vertical-tab separator",
|
||||
dsn: "host=h user=u\vpassword=" + secret + " dbname=db",
|
||||
leaks: []string{secret},
|
||||
keep: []string{"host=h", "dbname=db"},
|
||||
},
|
||||
{
|
||||
name: "keyword value sslpassword with sslmode preserved",
|
||||
dsn: "host=h user=u sslpassword=" + secret + " sslmode=require dbname=db",
|
||||
leaks: []string{secret},
|
||||
keep: []string{"host=h", "sslmode=require", "dbname=db"},
|
||||
},
|
||||
{
|
||||
name: "keyword value password but no user",
|
||||
dsn: "host=h port=5432 password=" + secret + " dbname=db",
|
||||
leaks: []string{secret},
|
||||
keep: []string{"host=h", "port=5432", "dbname=db"},
|
||||
},
|
||||
{
|
||||
name: "overlapping secrets where one is a prefix of another",
|
||||
dsn: "postgres://u:foo@h:5432/db?password=fooACTUAL",
|
||||
leaks: []string{"ACTUAL", ":foo@"},
|
||||
keep: []string{"postgres://u:", "h:5432/db"},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
out := scrubArgsForTelemetry([]string{"command", tc.dsn}, nil)
|
||||
for _, leak := range tc.leaks {
|
||||
if strings.Contains(out, leak) {
|
||||
t.Errorf("PASSWORD LEAK: %q survived telemetry scrub of %q: %q", leak, tc.dsn, out)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(out, "xxxxx") {
|
||||
t.Errorf("expected redaction marker xxxxx in %q", out)
|
||||
}
|
||||
for _, k := range tc.keep {
|
||||
if !strings.Contains(out, k) {
|
||||
t.Errorf("expected %q to survive redaction in %q", k, out)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestScrubArgsForTelemetryPasswordlessDSNUnchanged ports the deleted matrix
|
||||
// round-trip invariant: a DSN carrying no password must pass through the scrubber
|
||||
// byte-for-byte unchanged, in both URL and libpq keyword/value form. Over-redaction
|
||||
// here would destroy the non-secret operational signal the span exists to carry.
|
||||
func TestScrubArgsForTelemetryPasswordlessDSNUnchanged(t *testing.T) {
|
||||
for _, dsn := range []string{
|
||||
"postgres://bts@127.0.0.1:5432/db?application_name=beads&sslmode=require",
|
||||
"host=127.0.0.1 port=5432 user=bts dbname=db sslmode=require",
|
||||
} {
|
||||
want := "command " + dsn
|
||||
if out := scrubArgsForTelemetry([]string{"command", dsn}, nil); out != want {
|
||||
t.Errorf("passwordless DSN altered by scrubber:\n in = %q\n out = %q", want, out)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -176,10 +176,6 @@ Examples:
|
|||
return HandleErrorRespectJSON("commit message is required (use -m, --message, or --stdin)")
|
||||
}
|
||||
|
||||
if err := errExplicitCommitUnsupported(store, "vc commit"); err != nil {
|
||||
return HandleErrorRespectJSON("%v", err)
|
||||
}
|
||||
|
||||
commandDidExplicitDoltCommit = true
|
||||
if err := store.Commit(ctx, vcCommitMessage); err != nil {
|
||||
if isDoltNothingToCommit(err) {
|
||||
|
|
|
|||
|
|
@ -163,6 +163,10 @@ func autoMigrateOnVersionBump(beadsDir string) {
|
|||
if cfg == nil {
|
||||
cfg = configfile.DefaultConfig()
|
||||
}
|
||||
if cfg.GetBackend() != configfile.BackendDolt {
|
||||
debug.Logf("auto-migrate: skipping Dolt migration for backend %q", cfg.GetBackend())
|
||||
return
|
||||
}
|
||||
|
||||
if cfg.IsDoltProxiedServerMode() {
|
||||
debug.Logf("auto-migrate: skipping embedded migration, proxied-server handled after UOW provider init")
|
||||
|
|
|
|||
|
|
@ -434,5 +434,5 @@ func TestAutoMigrateOnVersionBump_NoDatabase(t *testing.T) {
|
|||
// TestAutoMigrateOnVersionBump_RefusesDowngrade, and TestAutoMigrateOnVersionBump_TracksMaxVersion
|
||||
// were removed because they depended on the SQLite storage backend (sqlite.New) for round-trip
|
||||
// persistence testing through autoMigrateOnVersionBump -> dolt.NewFromConfig.
|
||||
// Since SQLite support has been removed, auto-migration is only exercised with Dolt backends.
|
||||
// Automatic schema migration is exercised here through the Dolt backend.
|
||||
// Dolt-based migration testing is covered by TestInitDoltMetadata in init_test.go.
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ buildGoModule {
|
|||
# proxyVendor avoids vendor/modules.txt consistency checks when the vendored
|
||||
# tree lags go.mod/go.sum.
|
||||
proxyVendor = true;
|
||||
vendorHash = "sha256-Ij4xnWQLre/uE48TGMJRSGrunLqfXQ8MVBA+NCKlhLE=";
|
||||
vendorHash = "sha256-j+KnR2/0AxVkSfwO/C6/Zo2FjIC7iQs8RlZlrBt/8wc=";
|
||||
|
||||
# Match go.mod to the selected Nix Go toolchain. buildGoModule also builds
|
||||
# vendored dependencies in the Nix sandbox, where toolchain downloads are not
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ Benchmarks automatically generate CPU profiles:
|
|||
make bench
|
||||
|
||||
# View profile in browser (flamegraph)
|
||||
cd internal/storage/sqlite
|
||||
cd internal/storage/dolt
|
||||
go tool pprof -http=:8080 bench-cpu-*.prof
|
||||
```
|
||||
|
||||
|
|
|
|||
|
|
@ -198,7 +198,7 @@ Most tests are <0.01s and very fast.
|
|||
```
|
||||
cmd/bd/ - Main CLI tests (82 test files, most of the suite)
|
||||
internal/beads/ - Core beads library tests
|
||||
internal/storage/ - Storage backend tests (SQLite, memory)
|
||||
internal/storage/ - Storage backend tests (Dolt embedded/server, conformance)
|
||||
internal/rpc/ - RPC protocol tests
|
||||
internal/*/ - Various internal package tests
|
||||
```
|
||||
|
|
|
|||
|
|
@ -35,9 +35,9 @@ import (
|
|||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
// Find and open database
|
||||
dbPath := beads.FindDatabasePath()
|
||||
store, err := beads.Open(ctx, dbPath)
|
||||
// Find the workspace and open its configured Dolt implementation
|
||||
beadsDir := beads.FindBeadsDir()
|
||||
store, err := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
|
@ -145,8 +145,8 @@ type VCStorage struct {
|
|||
beads beads.Storage
|
||||
}
|
||||
|
||||
func NewVCStorage(ctx context.Context, dbPath string) (*VCStorage, error) {
|
||||
store, err := beads.Open(ctx, dbPath)
|
||||
func NewVCStorage(ctx context.Context, beadsDir string) (*VCStorage, error) {
|
||||
store, err := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,13 +5,174 @@ go 1.26.2
|
|||
require github.com/steveyegge/beads v0.0.0
|
||||
|
||||
require (
|
||||
cel.dev/expr v0.25.1 // indirect
|
||||
cloud.google.com/go v0.120.0 // indirect
|
||||
cloud.google.com/go/auth v0.16.2 // indirect
|
||||
cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect
|
||||
cloud.google.com/go/compute/metadata v0.9.0 // indirect
|
||||
cloud.google.com/go/iam v1.5.2 // indirect
|
||||
cloud.google.com/go/monitoring v1.24.2 // indirect
|
||||
cloud.google.com/go/storage v1.50.0 // indirect
|
||||
filippo.io/edwards25519 v1.2.0 // indirect
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.21.0 // indirect
|
||||
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.13.1 // indirect
|
||||
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.2 // indirect
|
||||
github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.6.4 // indirect
|
||||
github.com/AzureAD/microsoft-authentication-library-for-go v1.6.0 // indirect
|
||||
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.31.0 // indirect
|
||||
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.50.0 // indirect
|
||||
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.50.0 // indirect
|
||||
github.com/HdrHistogram/hdrhistogram-go v1.1.2 // indirect
|
||||
github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db // indirect
|
||||
github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible // indirect
|
||||
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 // indirect
|
||||
github.com/apache/arrow/go/arrow v0.0.0-20211112161151-bc219186db40 // indirect
|
||||
github.com/apache/thrift v0.23.0 // indirect
|
||||
github.com/aws/aws-sdk-go-v2 v1.41.5 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.8 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/config v1.29.8 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.17.61 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.64 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.21 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.21 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.4.22 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.41.0 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.7 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.9.13 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.10.15 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.13.21 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.19.21 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.97.3 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.25.0 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.29.0 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.33.16 // indirect
|
||||
github.com/aws/smithy-go v1.24.2 // indirect
|
||||
github.com/bcicen/jstream v1.0.1 // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
|
||||
github.com/cncf/xds/go v0.0.0-20260202195803-dba9d589def2 // indirect
|
||||
github.com/cockroachdb/apd/v3 v3.2.3 // indirect
|
||||
github.com/denisbrodbeck/machineid v1.0.1 // indirect
|
||||
github.com/dolthub/aws-sdk-go-ini-parser v0.0.0-20250305001723-2821c37f6c12 // indirect
|
||||
github.com/dolthub/dolt/go v0.40.5-0.20260715172757-a6690826d767 // indirect
|
||||
github.com/dolthub/driver/v2 v2.2.0 // indirect
|
||||
github.com/dolthub/eventsapi_schema v0.0.0-20260310172945-37a9265ade69 // indirect
|
||||
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 // indirect
|
||||
github.com/dolthub/fslock v0.0.5 // indirect
|
||||
github.com/dolthub/go-icu-regex v0.0.0-20260610153742-72563bc7ca83 // indirect
|
||||
github.com/dolthub/go-mysql-server v0.20.1-0.20260713210757-6d01d00bbbf3 // indirect
|
||||
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63 // indirect
|
||||
github.com/dolthub/ishell v0.0.0-20260414231531-5f031e3e9037 // indirect
|
||||
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 // indirect
|
||||
github.com/dolthub/vitess v0.0.0-20260624214226-81d034e0fde8 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/edsrzf/mmap-go v1.2.0 // indirect
|
||||
github.com/envoyproxy/go-control-plane/envoy v1.37.0 // indirect
|
||||
github.com/envoyproxy/protoc-gen-validate v1.3.3 // indirect
|
||||
github.com/esote/minmaxheap v1.0.0 // indirect
|
||||
github.com/fatih/color v1.16.0 // indirect
|
||||
github.com/felixge/httpsnoop v1.0.4 // indirect
|
||||
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568 // indirect
|
||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||
github.com/go-jose/go-jose/v4 v4.1.4 // indirect
|
||||
github.com/go-logr/logr v1.4.3 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/go-sql-driver/mysql v1.10.0 // indirect
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/gocraft/dbr/v2 v2.7.6 // indirect
|
||||
github.com/gofrs/flock v0.8.1 // indirect
|
||||
github.com/golang-jwt/jwt/v5 v5.3.0 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/google/btree v1.1.2 // indirect
|
||||
github.com/google/go-github/v57 v57.0.0 // indirect
|
||||
github.com/google/go-querystring v1.1.0 // indirect
|
||||
github.com/google/s2a-go v0.1.9 // indirect
|
||||
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect
|
||||
github.com/googleapis/gax-go/v2 v2.14.2 // indirect
|
||||
github.com/hashicorp/golang-lru v1.0.2 // indirect
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
|
||||
github.com/juju/gnuflag v1.0.0 // indirect
|
||||
github.com/kch42/buzhash v0.0.0-20160816060738-9bdec3dec7c6 // indirect
|
||||
github.com/klauspost/compress v1.18.5 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
|
||||
github.com/kylelemons/godebug v1.1.0 // indirect
|
||||
github.com/lestrrat-go/strftime v1.2.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.22 // indirect
|
||||
github.com/mattn/go-runewidth v0.0.23 // indirect
|
||||
github.com/mohae/uvarint v0.0.0-20160208145430-c3f9e62bf2b0 // indirect
|
||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
github.com/oracle/oci-go-sdk/v65 v65.55.0 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
github.com/pierrec/lz4/v4 v4.1.21 // indirect
|
||||
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pkg/profile v1.5.0 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
|
||||
github.com/prometheus/procfs v0.16.1 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||
github.com/sergi/go-diff v1.4.0 // indirect
|
||||
github.com/sirupsen/logrus v1.9.4 // indirect
|
||||
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect
|
||||
github.com/sony/gobreaker v0.5.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
||||
github.com/spf13/afero v1.15.0 // indirect
|
||||
github.com/spf13/cast v1.10.0 // indirect
|
||||
github.com/spf13/pflag v1.0.10 // indirect
|
||||
github.com/spf13/viper v1.21.0 // indirect
|
||||
github.com/spiffe/go-spiffe/v2 v2.6.0 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
github.com/tealeg/xlsx v1.0.5 // indirect
|
||||
github.com/tidwall/gjson v1.18.0 // indirect
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.1 // indirect
|
||||
github.com/tidwall/sjson v1.2.5 // indirect
|
||||
github.com/vbauerster/mpb/v8 v8.7.2 // indirect
|
||||
github.com/xitongsys/parquet-go v1.6.2 // indirect
|
||||
github.com/xitongsys/parquet-go-source v0.0.0-20240122235623-d6294584ab18 // indirect
|
||||
github.com/xtaci/smux v1.5.56 // indirect
|
||||
github.com/zeebo/xxh3 v1.0.2 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||
go.opentelemetry.io/contrib/detectors/gcp v1.42.0 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.61.0 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.61.0 // indirect
|
||||
go.opentelemetry.io/otel v1.44.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.44.0 // indirect
|
||||
go.opentelemetry.io/otel/sdk v1.44.0 // indirect
|
||||
go.opentelemetry.io/otel/sdk/metric v1.44.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.44.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
go.uber.org/zap v1.27.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.4 // indirect
|
||||
golang.org/x/crypto v0.52.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
|
||||
golang.org/x/sys v0.41.0 // indirect
|
||||
golang.org/x/net v0.55.0 // indirect
|
||||
golang.org/x/oauth2 v0.36.0 // indirect
|
||||
golang.org/x/sync v0.20.0 // indirect
|
||||
golang.org/x/sys v0.45.0 // indirect
|
||||
golang.org/x/term v0.43.0 // indirect
|
||||
golang.org/x/text v0.37.0 // indirect
|
||||
golang.org/x/time v0.12.0 // indirect
|
||||
golang.org/x/tools v0.45.0 // indirect
|
||||
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
|
||||
google.golang.org/api v0.241.0 // indirect
|
||||
google.golang.org/genproto v0.0.0-20250505200425-f936aa4a68b2 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260526163538-3dc84a4a5aaa // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260526163538-3dc84a4a5aaa // indirect
|
||||
google.golang.org/grpc v1.81.1 // indirect
|
||||
google.golang.org/protobuf v1.36.11 // indirect
|
||||
gopkg.in/go-jose/go-jose.v2 v2.6.3 // indirect
|
||||
gopkg.in/src-d/go-errors.v1 v1.0.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
modernc.org/libc v1.66.3 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.11.0 // indirect
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -16,16 +16,16 @@ import (
|
|||
func main() {
|
||||
ctx := context.Background()
|
||||
|
||||
// Find the Beads database (looks for .beads/*.db in current/parent dirs)
|
||||
dbPath := beads.FindDatabasePath()
|
||||
if dbPath == "" {
|
||||
log.Fatal("No Beads database found. Run 'bd init' first.")
|
||||
// Find the Beads workspace so backend selection can follow metadata.json.
|
||||
beadsDir := beads.FindBeadsDir()
|
||||
if beadsDir == "" {
|
||||
log.Fatal("No Beads workspace found. Run 'bd init' first.")
|
||||
}
|
||||
|
||||
fmt.Printf("Using database: %s\n\n", dbPath)
|
||||
fmt.Printf("Using workspace: %s\n\n", beadsDir)
|
||||
|
||||
// Open the database
|
||||
store, err := beads.Open(ctx, dbPath)
|
||||
// Open the configured Dolt implementation (embedded or server mode).
|
||||
store, err := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to open storage: %v", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,16 +18,28 @@ func TestExampleCompiles(t *testing.T) {
|
|||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
dbPath := filepath.Join(tmpDir, "dolt")
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
|
||||
t.Fatalf("Failed to create .beads dir: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(`{"backend":"dolt"}`), 0o600); err != nil {
|
||||
t.Fatalf("Failed to create metadata.json: %v", err)
|
||||
}
|
||||
|
||||
// Open storage
|
||||
ctx := context.Background()
|
||||
store, err := beads.Open(ctx, dbPath)
|
||||
store, err := beads.OpenBestAvailable(ctx, beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to open storage: %v", err)
|
||||
}
|
||||
defer store.Close()
|
||||
|
||||
// A fresh workspace needs an issue prefix before issues can be created
|
||||
// (bd init normally seeds this).
|
||||
if err := store.SetConfig(ctx, "issue_prefix", "ex"); err != nil {
|
||||
t.Fatalf("Failed to seed issue prefix: %v", err)
|
||||
}
|
||||
|
||||
// Create an issue (from example code)
|
||||
newIssue := &beads.Issue{
|
||||
ID: "",
|
||||
|
|
|
|||
|
|
@ -59,7 +59,7 @@
|
|||
gopls
|
||||
gotools
|
||||
golangci-lint
|
||||
sqlite
|
||||
sqlite # sqlite3 CLI for the legacy pre-Dolt migration scripts (scripts/migrate-sqlite-to-current.sh, scripts/migration-test/)
|
||||
];
|
||||
shellHook = ''
|
||||
echo "beads development shell"
|
||||
|
|
|
|||
12
go.mod
12
go.mod
|
|
@ -14,7 +14,6 @@ require (
|
|||
github.com/dolthub/driver/v2 v2.2.0
|
||||
github.com/dolthub/eventkit v0.0.0-20260611184414-99f5693e696a
|
||||
github.com/go-sql-driver/mysql v1.10.0
|
||||
github.com/jackc/pgx/v5 v5.10.0
|
||||
github.com/olebedev/when v1.1.0
|
||||
github.com/spf13/cobra v1.10.2
|
||||
github.com/spf13/viper v1.21.0
|
||||
|
|
@ -33,7 +32,6 @@ require (
|
|||
golang.org/x/sys v0.45.0
|
||||
golang.org/x/term v0.43.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
modernc.org/sqlite v1.53.0
|
||||
rsc.io/script v0.0.2
|
||||
)
|
||||
|
||||
|
|
@ -45,20 +43,12 @@ require (
|
|||
github.com/cockroachdb/apd/v3 v3.2.3 // indirect
|
||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||
github.com/invopop/jsonschema v0.13.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/mailru/easyjson v0.7.7 // indirect
|
||||
github.com/moby/moby/api v1.54.1 // indirect
|
||||
github.com/moby/moby/client v0.4.0 // indirect
|
||||
github.com/ncruces/go-strftime v1.0.0 // indirect
|
||||
github.com/prometheus/procfs v0.16.1 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/standard-webhooks/standard-webhooks/libraries v0.0.1 // indirect
|
||||
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
|
||||
modernc.org/libc v1.73.4 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.11.0 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
|
|
@ -228,7 +218,7 @@ require (
|
|||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
||||
github.com/spf13/afero v1.15.0 // indirect
|
||||
github.com/spf13/cast v1.10.0 // indirect
|
||||
github.com/spf13/pflag v1.0.10
|
||||
github.com/spf13/pflag v1.0.10 // indirect
|
||||
github.com/spiffe/go-spiffe/v2 v2.6.0 // indirect
|
||||
github.com/subosito/gotenv v1.6.0
|
||||
github.com/tealeg/xlsx v1.0.5 // indirect
|
||||
|
|
|
|||
41
go.sum
41
go.sum
|
|
@ -661,8 +661,6 @@ github.com/google/pprof v0.0.0-20210506205249-923b5ab0fc1a/go.mod h1:kpwsk12EmLe
|
|||
github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
|
||||
github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
|
||||
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
||||
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
|
||||
github.com/google/s2a-go v0.1.9 h1:LGD7gtMgezd8a/Xak7mEWL0PjoTQFvpRudN895yqKW0=
|
||||
github.com/google/s2a-go v0.1.9/go.mod h1:YA0Ei2ZQL3acow2O62kdp9UlnvMmU7kA6Eutn0dXayM=
|
||||
|
|
@ -724,7 +722,6 @@ github.com/jackc/pgio v1.0.0/go.mod h1:oP+2QK2wFfUWgr+gxjoBH9KGBb31Eio69xUb0w5bY
|
|||
github.com/jackc/pgmock v0.0.0-20190831213851-13a1b77aafa2/go.mod h1:fGZlG77KXmcq05nJLRkk0+p82V8B8Dw8KN2/V9c/OAE=
|
||||
github.com/jackc/pgmock v0.0.0-20201204152224-4fe30f7445fd/go.mod h1:hrBW0Enj2AZTNpt/7Y5rr2xe/9Mn757Wtb2xeBzPv2c=
|
||||
github.com/jackc/pgmock v0.0.0-20210724152146-4ad1a8207f65/go.mod h1:5R2h2EEX+qri8jOWMbJCtaPWkrrNc7OHwsp2TCqp7ak=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgproto3 v1.1.0/go.mod h1:eR5FA3leWg7p9aeAqi37XOTgTIbkABlvcPB3E5rlc78=
|
||||
github.com/jackc/pgproto3/v2 v2.0.0-alpha1.0.20190420180111-c116219b62db/go.mod h1:bhq50y+xrl9n5mRYyCBFKkpRVTLYJVWeCc+mEAI3yXA=
|
||||
|
|
@ -735,8 +732,6 @@ github.com/jackc/pgproto3/v2 v2.0.6/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwX
|
|||
github.com/jackc/pgproto3/v2 v2.1.1/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
|
||||
github.com/jackc/pgproto3/v2 v2.2.0/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA=
|
||||
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg=
|
||||
github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc=
|
||||
github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw=
|
||||
|
|
@ -747,14 +742,10 @@ github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9
|
|||
github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc=
|
||||
github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs=
|
||||
github.com/jackc/pgx/v4 v4.15.0/go.mod h1:D/zyOyXiaM1TmVWnOM18p0xdDtdakRBa0RsVGI3U3bw=
|
||||
github.com/jackc/pgx/v5 v5.10.0 h1:VhSvgU2jSli8o3AqIEOTJr7rZwAEUVo4E4XhR94Zfr0=
|
||||
github.com/jackc/pgx/v5 v5.10.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
|
||||
github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/jackc/puddle v1.2.1/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/jcmturner/gofork v0.0.0-20180107083740-2aebee971930/go.mod h1:MK8+TM0La+2rjBD4jE12Kj1pCCxK7d2LK/UM3ncEo0o=
|
||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
|
||||
|
|
@ -885,8 +876,6 @@ github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELU
|
|||
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
||||
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/ncw/swift v1.0.52/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||
github.com/olebedev/when v1.1.0 h1:dlpoRa7huImhNtEx4yl0WYfTHVEWmJmIWd7fEkTHayc=
|
||||
|
|
@ -931,8 +920,6 @@ github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9Z
|
|||
github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA=
|
||||
github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg=
|
||||
github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
|
||||
|
|
@ -1737,34 +1724,6 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
|
|||
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
|
||||
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
|
||||
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
|
||||
modernc.org/cc/v4 v4.28.4 h1:Hd/4Es+MBj+/7hSdZaisNyu6bv3V0Dp2MdllyfqaH+c=
|
||||
modernc.org/cc/v4 v4.28.4/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI=
|
||||
modernc.org/ccgo/v4 v4.34.4 h1:OVnSOWQjVKOYkFxoHYB+qQmSHK5gqMqARM+K9DpR/Ws=
|
||||
modernc.org/ccgo/v4 v4.34.4/go.mod h1:qdKqE8FNIYyysougB1RX9MxCzp5oJOcQXSobANJ4TuE=
|
||||
modernc.org/fileutil v1.4.0 h1:j6ZzNTftVS054gi281TyLjHPp6CPHr2KCxEXjEbD6SM=
|
||||
modernc.org/fileutil v1.4.0/go.mod h1:EqdKFDxiByqxLk8ozOxObDSfcVOv/54xDs/DUHdvCUU=
|
||||
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
|
||||
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
||||
modernc.org/gc/v3 v3.1.3 h1:6QAplYyVO+KdPW3pGnqmJDUxtkec8ooEWvks/hhU3lc=
|
||||
modernc.org/gc/v3 v3.1.3/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY=
|
||||
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
|
||||
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
|
||||
modernc.org/libc v1.73.4 h1:+ra4Ui8ngyt8HDcO1FTDPWlkAh6yOdaO2yAoh8MddQA=
|
||||
modernc.org/libc v1.73.4/go.mod h1:DXZ3eO8qMCNn2SnmTNCiC71nJ9Rcq3PsnpU6Vc4rWK8=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||
modernc.org/opt v0.2.0 h1:tGyef5ApycA7FSEOMraay9SaTk5zmbx7Tu+cJs4QKZg=
|
||||
modernc.org/opt v0.2.0/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||
modernc.org/sqlite v1.53.0 h1:20WG8N9q4ji/dEqGk4uiI0c6OPjSeLTNYGFCc3+7c1M=
|
||||
modernc.org/sqlite v1.53.0/go.mod h1:xoEpOIpGrgT48H5iiyt/YXPCZPEzlfmfFwtk8Lklw8s=
|
||||
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||
nhooyr.io/websocket v1.8.7/go.mod h1:B70DZP8IakI65RVQ51MsWP/8jndNma26DVA/nFSCgW0=
|
||||
pgregory.net/rapid v1.2.0 h1:keKAYRcjm+e1F0oAuU5F5+YPAWcyxNNRK2wud503Gnk=
|
||||
pgregory.net/rapid v1.2.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=
|
||||
|
|
|
|||
|
|
@ -421,13 +421,27 @@ func findLocalBeadsDir() string {
|
|||
}
|
||||
|
||||
// findDatabaseInBeadsDir searches for a database within a .beads directory.
|
||||
// Checks metadata.json for the Dolt database path. For server mode, no local
|
||||
// directory is required. For embedded mode, checks both the embeddeddolt/
|
||||
// directory (where the embedded engine stores data) and the legacy dolt/ path.
|
||||
// Returns empty string if no database is found.
|
||||
// Checks metadata.json for the selected implementation's database path. For
|
||||
// server mode, no local path needs to exist yet: authoritative
|
||||
// metadata is enough to route the caller without falling through to Dolt.
|
||||
// Embedded Dolt checks both embeddeddolt/ and the legacy dolt/ path. Returns
|
||||
// empty string if no database is configured or found.
|
||||
func findDatabaseInBeadsDir(beadsDir string, _ bool) string {
|
||||
// Check for metadata.json first (single source of truth)
|
||||
if cfg, err := configfile.Load(beadsDir); err == nil && cfg != nil {
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
// A present but unreadable/invalid metadata file is authoritative. Do
|
||||
// not bypass it by discovering a leftover local Dolt directory; callers
|
||||
// that need the detailed error should use OpenBestAvailable.
|
||||
return ""
|
||||
}
|
||||
if cfg != nil {
|
||||
if !configfile.IsSupportedBackend(cfg.Backend) {
|
||||
// Unknown and removed backend markers are not Dolt aliases. Let the
|
||||
// storage-selection layer report the metadata error without routing
|
||||
// through a leftover local Dolt directory.
|
||||
return ""
|
||||
}
|
||||
// For Dolt server mode, database is on the server - no local directory required
|
||||
if cfg.IsDoltServerMode() {
|
||||
return cfg.DatabasePath(beadsDir)
|
||||
|
|
@ -491,6 +505,7 @@ func FindDatabasePath() string {
|
|||
|
||||
// BEADS_DIR is set but no database found - this is OK for --no-db mode
|
||||
// Return empty string and let the caller handle it
|
||||
return ""
|
||||
}
|
||||
|
||||
// 2. Check BEADS_DB environment variable (deprecated but still supported)
|
||||
|
|
|
|||
88
internal/beads/sqlite_database_path_test.go
Normal file
88
internal/beads/sqlite_database_path_test.go
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
package beads
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The sqlite backend is a removed-backend tombstone: discovery must return ""
|
||||
// so store selection reports the metadata error, and it must never create the
|
||||
// database file the removed backend would have provisioned.
|
||||
func TestFindDatabasePathSQLiteMetadataOutsideCWD(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
metadata string
|
||||
relativePath string
|
||||
}{
|
||||
{
|
||||
name: "default path",
|
||||
metadata: `{"backend":"sqlite","sqlite_path":"beads.db"}`,
|
||||
relativePath: "beads.db",
|
||||
},
|
||||
{
|
||||
name: "custom relative path",
|
||||
metadata: `{"backend":"sqlite","sqlite_path":"data/custom.db"}`,
|
||||
relativePath: filepath.Join("data", "custom.db"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cwd := filepath.Join(root, "working-directory")
|
||||
beadsDir := filepath.Join(root, "external-workspace", ".beads")
|
||||
if err := os.MkdirAll(cwd, 0o750); err != nil {
|
||||
t.Fatalf("create working directory: %v", err)
|
||||
}
|
||||
if err := os.MkdirAll(beadsDir, 0o750); err != nil {
|
||||
t.Fatalf("create beads directory: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte(tt.metadata), 0o600); err != nil {
|
||||
t.Fatalf("write metadata.json: %v", err)
|
||||
}
|
||||
|
||||
t.Chdir(cwd)
|
||||
t.Setenv("BEADS_DIR", beadsDir)
|
||||
t.Setenv("BEADS_DB", "")
|
||||
t.Setenv("BD_DB", "")
|
||||
|
||||
dbPath := filepath.Join(beadsDir, tt.relativePath)
|
||||
if _, err := os.Stat(dbPath); !os.IsNotExist(err) {
|
||||
t.Fatalf("SQLite database unexpectedly exists before discovery: %v", err)
|
||||
}
|
||||
|
||||
if got := FindDatabasePath(); got != "" {
|
||||
t.Errorf("FindDatabasePath() = %q, want \"\" for the removed sqlite backend", got)
|
||||
}
|
||||
if _, err := os.Stat(dbPath); !os.IsNotExist(err) {
|
||||
t.Fatalf("FindDatabasePath created SQLite database path %q: %v", dbPath, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFindDatabasePathDoesNotBypassCorruptMetadata(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
beadsDir := filepath.Join(root, "selected", ".beads")
|
||||
leftoverDolt := filepath.Join(beadsDir, "embeddeddolt")
|
||||
if err := os.MkdirAll(leftoverDolt, 0o750); err != nil {
|
||||
t.Fatalf("create leftover Dolt directory: %v", err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(beadsDir, "metadata.json"), []byte("{"), 0o600); err != nil {
|
||||
t.Fatalf("write corrupt metadata.json: %v", err)
|
||||
}
|
||||
ambiguousCWD := filepath.Join(root, "ambient")
|
||||
if err := os.MkdirAll(filepath.Join(ambiguousCWD, ".beads", "embeddeddolt"), 0o750); err != nil {
|
||||
t.Fatalf("create ambient Dolt workspace: %v", err)
|
||||
}
|
||||
t.Chdir(ambiguousCWD)
|
||||
|
||||
t.Setenv("BEADS_DIR", beadsDir)
|
||||
t.Setenv("BEADS_DB", "")
|
||||
t.Setenv("BD_DB", "")
|
||||
|
||||
if got := FindDatabasePath(); got != "" {
|
||||
t.Fatalf("FindDatabasePath() = %q with corrupt metadata, want empty path instead of leftover Dolt", got)
|
||||
}
|
||||
}
|
||||
51
internal/configfile/backend_messages.go
Normal file
51
internal/configfile/backend_messages.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package configfile
|
||||
|
||||
import "fmt"
|
||||
|
||||
// This file is the single source of truth for the user-facing removed-backend
|
||||
// and unknown-backend messages. cmd/bd, the public beads package, and the Dolt
|
||||
// store must all fail closed with the same text, so the wording is deliberately
|
||||
// centralized here. The framing of these messages is pending a separate
|
||||
// maintainer decision — reword them here, in one place, once that lands.
|
||||
|
||||
// RemovedBackendRationale explains why direct PostgreSQL/MySQL support was
|
||||
// removed. Shortened site-specific messages (e.g. bd init flag guidance)
|
||||
// compose with this clause directly.
|
||||
const RemovedBackendRationale = "direct support for general-purpose server databases was rolled back to keep Beads simple and resource-light"
|
||||
|
||||
// RemovedSQLiteRationale explains why the SQLite backend was removed. SQLite is
|
||||
// not a server database, so it carries its own rationale instead of reusing
|
||||
// RemovedBackendRationale.
|
||||
const RemovedSQLiteRationale = "the SQLite backend was rolled back to consolidate storage on a single engine and dialect and keep Beads simple and robust"
|
||||
|
||||
// BackendNotOpenedGuarantee is the fail-closed data-safety guarantee included
|
||||
// in backend rejection errors: refusing a workspace never opens, creates, or
|
||||
// modifies its storage.
|
||||
const BackendNotOpenedGuarantee = "no storage database was opened or modified"
|
||||
|
||||
// removedBackendRationale picks the rationale clause for a removed backend.
|
||||
func removedBackendRationale(backend string) string {
|
||||
if backend == BackendSQLite {
|
||||
return RemovedSQLiteRationale
|
||||
}
|
||||
return RemovedBackendRationale
|
||||
}
|
||||
|
||||
// RemovedBackendDetail returns the shared body of every removed-backend error:
|
||||
// the rationale, the untouched-data guarantee, and the migration path. Callers
|
||||
// prepend a site-specific lead-in; RemovedBackendError carries the standard one.
|
||||
func RemovedBackendDetail(backend string) string {
|
||||
return fmt.Sprintf("%s; the configured %s database was not opened or modified; export it with a bd version that supports %s, then follow bd help init-safety to reinitialize with Dolt and import the exported data", removedBackendRationale(backend), backend, backend)
|
||||
}
|
||||
|
||||
// RemovedBackendError is the standard fail-closed error for metadata that
|
||||
// selects a backend whose direct support was removed.
|
||||
func RemovedBackendError(backend string) error {
|
||||
return fmt.Errorf("storage backend %q is no longer supported: %s", backend, RemovedBackendDetail(backend))
|
||||
}
|
||||
|
||||
// UnknownBackendError is the standard fail-closed error for metadata that
|
||||
// names a backend this build does not recognize.
|
||||
func UnknownBackendError(backend string) error {
|
||||
return fmt.Errorf("storage backend %q in metadata.json is not recognized or supported; %s; the supported backend is %q; fix or restore metadata.json and retry", backend, BackendNotOpenedGuarantee, BackendDolt)
|
||||
}
|
||||
|
|
@ -17,7 +17,7 @@ const ConfigFileName = "metadata.json"
|
|||
|
||||
type Config struct {
|
||||
Database string `json:"database"`
|
||||
Backend string `json:"backend,omitempty"` // Storage backend: "dolt" (default), "postgres", "mysql", or "sqlite". Read via GetBackend().
|
||||
Backend string `json:"backend,omitempty"` // Storage backend: "dolt" (default); legacy "postgres"/"mysql"/"sqlite" values are rejection tombstones. Read via GetBackend().
|
||||
|
||||
// Deletions configuration
|
||||
DeletionsRetentionDays int `json:"deletions_retention_days,omitempty"` // 0 means use default (3 days)
|
||||
|
|
@ -36,17 +36,16 @@ type Config struct {
|
|||
DoltRemotesAPIPort int `json:"dolt_remotesapi_port,omitempty"` // Dolt remotesapi port for federation (default: 8080)
|
||||
// Note: Password should be set via BEADS_DOLT_PASSWORD env var for security
|
||||
|
||||
// Postgres backend (backend="postgres"). Password is NEVER persisted; it
|
||||
// comes from BEADS_PG_PASSWORD or BEADS_POSTGRES_URL.
|
||||
PostgresDSN string `json:"postgres_dsn,omitempty"` // e.g. postgres://user@host:5432/db (no password)
|
||||
PostgresSchema string `json:"postgres_schema,omitempty"` // per-workspace schema (search_path)
|
||||
// Deprecated backend fields are retained only to round-trip metadata written by
|
||||
// the short-lived PostgreSQL/MySQL implementations. They are not connection
|
||||
// configuration for current builds; store selection rejects those backends.
|
||||
PostgresDSN string `json:"postgres_dsn,omitempty"`
|
||||
PostgresSchema string `json:"postgres_schema,omitempty"`
|
||||
MySQLDSN string `json:"mysql_dsn,omitempty"`
|
||||
MySQLDatabase string `json:"mysql_database,omitempty"`
|
||||
|
||||
// MySQL backend (backend="mysql"). Password is NEVER persisted; it comes from
|
||||
// BEADS_MYSQL_PASSWORD or BEADS_MYSQL_URL.
|
||||
MySQLDSN string `json:"mysql_dsn,omitempty"` // e.g. user@tcp(host:3306)/ (no password)
|
||||
MySQLDatabase string `json:"mysql_database,omitempty"` // per-workspace database (MySQL's isolation unit)
|
||||
|
||||
// SQLite backend (backend="sqlite"). File-based, embedded; no credentials.
|
||||
// Deprecated: retained only to round-trip metadata written by the removed
|
||||
// SQLite backend (backend="sqlite"); store selection rejects that backend.
|
||||
SQLitePath string `json:"sqlite_path,omitempty"` // database file, relative to the beads dir (default beads.db)
|
||||
|
||||
// Project identity — unique ID generated at bd init time.
|
||||
|
|
@ -231,9 +230,9 @@ type BackendCapabilities struct {
|
|||
SingleProcessOnly bool
|
||||
}
|
||||
|
||||
// CapabilitiesForBackend returns capabilities for a backend string.
|
||||
// Dolt is the only supported backend. Returns SingleProcessOnly=true by default;
|
||||
// use Config.GetCapabilities() to properly handle server mode.
|
||||
// CapabilitiesForBackend returns capabilities for a backend string. Embedded Dolt
|
||||
// is single-process-only; use Config.GetCapabilities() to account for
|
||||
// Dolt server and proxied-server modes.
|
||||
func CapabilitiesForBackend(_ string) BackendCapabilities {
|
||||
return BackendCapabilities{SingleProcessOnly: true}
|
||||
}
|
||||
|
|
@ -249,10 +248,19 @@ func (c *Config) GetCapabilities() BackendCapabilities {
|
|||
return CapabilitiesForBackend(backend)
|
||||
}
|
||||
|
||||
// GetBackend returns the configured storage backend. Only the explicitly-allowlisted
|
||||
// non-default backends ("postgres", "mysql") are honored; "", "dolt", and any legacy
|
||||
// or unknown value resolve to dolt so the default path stays byte-identical and a
|
||||
// typo fails safe to Dolt.
|
||||
// IsSupportedBackend reports whether backend selects an implementation shipped by
|
||||
// beads. The empty value is the legacy/default spelling of Dolt.
|
||||
func IsSupportedBackend(backend string) bool {
|
||||
return backend == "" || backend == BackendDolt
|
||||
}
|
||||
|
||||
// GetBackend returns the configured storage backend. PostgreSQL, MySQL, and
|
||||
// SQLite remain recognizable here so workspaces created by earlier builds can
|
||||
// fail loudly at store selection instead of silently falling back to an empty
|
||||
// Dolt database.
|
||||
// Empty and explicit Dolt retain the established Dolt behavior. GetBackend keeps
|
||||
// the historical Dolt fallback for unknown values, so storage-selection callers
|
||||
// must check IsSupportedBackend(c.Backend) before opening or creating storage.
|
||||
func (c *Config) GetBackend() string {
|
||||
if c != nil {
|
||||
switch c.Backend {
|
||||
|
|
@ -276,82 +284,6 @@ func (c *Config) GetSQLitePath() string {
|
|||
return c.SQLitePath
|
||||
}
|
||||
|
||||
// GetPostgresDSN returns the base Postgres connection string: BEADS_POSTGRES_URL (a
|
||||
// full override that may carry a password) if set, else the persisted,
|
||||
// password-free metadata postgres_dsn. It never merges a password — password
|
||||
// resolution (BEADS_PG_PASSWORD_COMMAND, BEADS_PG_PASSWORD, the credentials file)
|
||||
// and placement happen at open time in the postgres backend, which owns the pgx
|
||||
// parser this low-level package must not import (see the note at RedactPassword).
|
||||
func (c *Config) GetPostgresDSN() string {
|
||||
if u := os.Getenv("BEADS_POSTGRES_URL"); u != "" {
|
||||
return u
|
||||
}
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
return c.PostgresDSN
|
||||
}
|
||||
|
||||
// GetPostgresPasswordCommand returns the credential command that resolves the
|
||||
// Postgres password: BEADS_PG_PASSWORD_COMMAND. Empty means no command — the static
|
||||
// BEADS_PG_PASSWORD / credentials-file path applies. The command's stdout is a
|
||||
// password (a bare token or a {token,expires_in} envelope); it is run at open time,
|
||||
// out-ranking the static password so a rotating secret is never shadowed by a stale
|
||||
// env value. It is deliberately read from the environment only, NOT metadata.json:
|
||||
// a metadata-sourced command is arbitrary code run on open, so persisting it waits
|
||||
// on a workspace-trust gate.
|
||||
func (c *Config) GetPostgresPasswordCommand() string {
|
||||
return os.Getenv("BEADS_PG_PASSWORD_COMMAND")
|
||||
}
|
||||
|
||||
// GetPostgresSchema returns the per-workspace Postgres schema (search_path).
|
||||
func (c *Config) GetPostgresSchema() string {
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
return c.PostgresSchema
|
||||
}
|
||||
|
||||
// GetMySQLDSN returns the base MySQL server DSN: BEADS_MYSQL_URL (a full override
|
||||
// that may carry a password) if set, else the persisted, password-free metadata
|
||||
// mysql_dsn. It never merges a password — password resolution
|
||||
// (BEADS_MYSQL_PASSWORD_COMMAND, BEADS_MYSQL_PASSWORD, the credentials file) and
|
||||
// placement happen at open time in the mysql backend, which owns the go-sql-driver
|
||||
// parser this low-level package must not import (see the note at RedactPassword).
|
||||
func (c *Config) GetMySQLDSN() string {
|
||||
if u := os.Getenv("BEADS_MYSQL_URL"); u != "" {
|
||||
return u
|
||||
}
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
return c.MySQLDSN
|
||||
}
|
||||
|
||||
// GetMySQLPasswordCommand returns the credential command that resolves the MySQL
|
||||
// password: BEADS_MYSQL_PASSWORD_COMMAND. Empty means no command — the static
|
||||
// BEADS_MYSQL_PASSWORD / credentials-file path applies. The command's stdout is a
|
||||
// password (a bare token or a {token,expires_in} envelope); it is run at open time,
|
||||
// out-ranking the static password so a rotating secret is never shadowed by a stale
|
||||
// env value. It is deliberately read from the environment only, NOT metadata.json:
|
||||
// a metadata-sourced command is arbitrary code run on open, so persisting it waits
|
||||
// on a workspace-trust gate.
|
||||
func (c *Config) GetMySQLPasswordCommand() string {
|
||||
return os.Getenv("BEADS_MYSQL_PASSWORD_COMMAND")
|
||||
}
|
||||
|
||||
// GetMySQLDatabase returns the per-workspace MySQL database (isolation unit).
|
||||
func (c *Config) GetMySQLDatabase() string {
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
return c.MySQLDatabase
|
||||
}
|
||||
|
||||
// Password redaction for persistence lives in pgdialect.RedactPassword, not here:
|
||||
// it must fail closed (verify with pgx that no password survives) and therefore
|
||||
// depends on the pgx parser, which this low-level config package must not import.
|
||||
|
||||
// Dolt mode constants
|
||||
const (
|
||||
DoltModeEmbedded = "embedded"
|
||||
|
|
|
|||
|
|
@ -629,10 +629,10 @@ func TestProxiedServerClientInfo_ResolvedPaths(t *testing.T) {
|
|||
})
|
||||
}
|
||||
|
||||
// TestGetBackendAllowlist verifies the allowlist semantics: the SQL backends
|
||||
// (postgres, mysql, sqlite) are honored; every other value (empty, legacy,
|
||||
// genuinely unknown) falls back to Dolt. This is the guard behind backend selection
|
||||
// — a typo in metadata.json must fail safe to Dolt, never to an unintended backend.
|
||||
// TestGetBackendAllowlist verifies the metadata-routing semantics: current backends
|
||||
// and removed-backend tombstones remain recognizable, while empty or unknown values
|
||||
// retain the historical Dolt fallback. Store selection rejects the tombstones before
|
||||
// it can open an empty Dolt database.
|
||||
func TestGetBackendAllowlist(t *testing.T) {
|
||||
fallsBackToDolt := []struct {
|
||||
name string
|
||||
|
|
@ -662,6 +662,29 @@ func TestGetBackendAllowlist(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSupportedBackendAllowlist(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
backend string
|
||||
supported bool
|
||||
}{
|
||||
{name: "implicit dolt", backend: "", supported: true},
|
||||
{name: "dolt", backend: BackendDolt, supported: true},
|
||||
{name: "sqlite", backend: BackendSQLite, supported: false},
|
||||
{name: "postgres", backend: BackendPostgres, supported: false},
|
||||
{name: "mysql", backend: BackendMySQL, supported: false},
|
||||
{name: "unknown", backend: "mystery", supported: false},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := IsSupportedBackend(tt.backend); got != tt.supported {
|
||||
t.Fatalf("IsSupportedBackend(%q) = %v, want %v", tt.backend, got, tt.supported)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestDatabasePathAlwaysDolt tests that DatabasePath always returns the dolt path.
|
||||
func TestDatabasePathAlwaysDolt(t *testing.T) {
|
||||
beadsDir := "/home/user/project/.beads"
|
||||
|
|
|
|||
|
|
@ -1,37 +0,0 @@
|
|||
package configfile
|
||||
|
||||
import "testing"
|
||||
|
||||
// GetMySQLDSN returns the BASE DSN and never merges a password: BEADS_MYSQL_PASSWORD
|
||||
// is resolved and placed at open time in the mysql backend (which owns the
|
||||
// go-sql-driver parser), so the persisted, password-free metadata DSN comes back
|
||||
// unchanged here.
|
||||
func TestGetMySQLDSNReturnsBaseUnmerged(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "secret")
|
||||
c := &Config{MySQLDSN: "bts@tcp(127.0.0.1:3306)/"}
|
||||
if got, want := c.GetMySQLDSN(), "bts@tcp(127.0.0.1:3306)/"; got != want {
|
||||
t.Fatalf("GetMySQLDSN() = %q, want %q (must not merge the password)", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// BEADS_MYSQL_URL (full override) takes precedence over the metadata DSN.
|
||||
func TestGetMySQLDSNEnvURLPrecedence(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_URL", "root:pw@tcp(host:3306)/db")
|
||||
c := &Config{MySQLDSN: "other@tcp(127.0.0.1:3306)/"}
|
||||
if got, want := c.GetMySQLDSN(), "root:pw@tcp(host:3306)/db"; got != want {
|
||||
t.Fatalf("GetMySQLDSN() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// GetMySQLPasswordCommand reads BEADS_MYSQL_PASSWORD_COMMAND from the environment
|
||||
// only (metadata is deliberately excluded until a workspace-trust gate exists).
|
||||
func TestGetMySQLPasswordCommand(t *testing.T) {
|
||||
c := &Config{}
|
||||
if got := c.GetMySQLPasswordCommand(); got != "" {
|
||||
t.Fatalf("GetMySQLPasswordCommand() = %q, want empty when unset", got)
|
||||
}
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", "get-credential")
|
||||
if got, want := c.GetMySQLPasswordCommand(), "get-credential"; got != want {
|
||||
t.Fatalf("GetMySQLPasswordCommand() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,36 +0,0 @@
|
|||
package configfile
|
||||
|
||||
import "testing"
|
||||
|
||||
// GetPostgresDSN returns the BASE DSN and never merges a password: BEADS_PG_PASSWORD
|
||||
// is resolved and placed at open time in the postgres backend (which owns the pgx
|
||||
// parser), so the persisted, password-free metadata DSN comes back unchanged here.
|
||||
func TestGetPostgresDSNReturnsBaseUnmerged(t *testing.T) {
|
||||
t.Setenv("BEADS_PG_PASSWORD", "secret")
|
||||
c := &Config{PostgresDSN: "postgres://bts@127.0.0.1:5432/db"}
|
||||
if got, want := c.GetPostgresDSN(), "postgres://bts@127.0.0.1:5432/db"; got != want {
|
||||
t.Fatalf("GetPostgresDSN() = %q, want %q (must not merge the password)", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// BEADS_POSTGRES_URL (full override) takes precedence over the metadata DSN.
|
||||
func TestGetPostgresDSNEnvURLPrecedence(t *testing.T) {
|
||||
t.Setenv("BEADS_POSTGRES_URL", "postgres://a:b@host:5432/db")
|
||||
c := &Config{PostgresDSN: "postgres://other@127.0.0.1:5432/db"}
|
||||
if got, want := c.GetPostgresDSN(), "postgres://a:b@host:5432/db"; got != want {
|
||||
t.Fatalf("GetPostgresDSN() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// GetPostgresPasswordCommand reads BEADS_PG_PASSWORD_COMMAND from the environment
|
||||
// only (metadata is deliberately excluded until a workspace-trust gate exists).
|
||||
func TestGetPostgresPasswordCommand(t *testing.T) {
|
||||
c := &Config{}
|
||||
if got := c.GetPostgresPasswordCommand(); got != "" {
|
||||
t.Fatalf("GetPostgresPasswordCommand() = %q, want empty when unset", got)
|
||||
}
|
||||
t.Setenv("BEADS_PG_PASSWORD_COMMAND", "get-credential")
|
||||
if got, want := c.GetPostgresPasswordCommand(), "get-credential"; got != want {
|
||||
t.Fatalf("GetPostgresPasswordCommand() = %q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
package storage
|
||||
|
||||
// NonCommitGraphBackend is an affirmative *negative* marker: it is implemented
|
||||
// only by backends whose Commit/GetCurrentCommit/Push do NOT participate in a
|
||||
// real version-control commit graph (e.g. Postgres, SQLite). cmd/bd's PostRun
|
||||
// skips its Dolt-only maintenance tail (auto-commit, auto-export, auto-backup,
|
||||
// auto-push) when the active store implements this and returns true.
|
||||
//
|
||||
// The marker is negative and lives only on non-Dolt backends by design: a store
|
||||
// that does NOT implement it is assumed to be Dolt, so the default path runs the
|
||||
// full maintenance tail unchanged and stays byte-identical. New SQL backends opt
|
||||
// OUT of Dolt maintenance by implementing this; they never have to touch the
|
||||
// Dolt constructors.
|
||||
type NonCommitGraphBackend interface {
|
||||
CommitGraphUnsupported() bool
|
||||
}
|
||||
|
|
@ -43,9 +43,8 @@ func auditFoldCount(all map[string]string, want string) int {
|
|||
// testAuditConfigKeyCaseSensitive pins the reference's config key-column collation.
|
||||
// The finding predicted Dolt's VARCHAR primary key would be case-insensitive, but
|
||||
// the embedded-Dolt oracle treats "MyKey" and "mykey" as DISTINCT keys: each holds
|
||||
// its own value and GetAllConfig carries both rows. Postgres text and SQLite BINARY
|
||||
// are case-sensitive too, so they match; the divergence suspect is real MySQL, whose
|
||||
// server-default utf8mb4 collation is case-insensitive and would collapse the two.
|
||||
// its own value and GetAllConfig carries both rows. SQLite's BINARY collation matches
|
||||
// that behavior.
|
||||
func testAuditConfigKeyCaseSensitive(t *testing.T, f Factory) {
|
||||
s := f(t)
|
||||
c := ctx()
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ import (
|
|||
// Audit cases for the "labels-comments-events" slice. Each case pins a strange
|
||||
// but real behavior of the issueops label/comment/event helpers, validated
|
||||
// against the embedded-Dolt oracle. See the audit findings for provenance
|
||||
// (issueops/events.go, issueops/labels.go, issueops/comments.go, sqlkit/annotations.go).
|
||||
// (issueops/events.go, issueops/labels.go, issueops/comments.go; formerly sqlkit/annotations.go).
|
||||
|
||||
// auditEventsOfType returns the events of exactly the given type, in the order
|
||||
// GetEvents returned them.
|
||||
|
|
@ -245,7 +245,7 @@ func testAuditImportCommentSubSecond(t *testing.T, f Factory) {
|
|||
c := ctx()
|
||||
must(t, s.CreateIssue(c, withDefaults(&types.Issue{ID: "test-i", Title: "I"}), "a"))
|
||||
// Sub-second precision is intentionally backend-specific (Dolt datetime(0) rounds
|
||||
// half-up; SQLite keeps the fraction; Postgres keeps microseconds), so we test the
|
||||
// half-up; SQLite keeps the fraction), so we test the
|
||||
// portable contract every backend honors: a whole-second UTC timestamp round-trips
|
||||
// exactly. Higher fidelity on some backends is accepted, not asserted.
|
||||
ts := time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC)
|
||||
|
|
|
|||
|
|
@ -665,9 +665,8 @@ func testAuditGetNextChildID(t *testing.T, f Factory) {
|
|||
}
|
||||
}
|
||||
|
||||
// The self-heal scan (id LIKE parent.'%') is case-sensitive on every backend
|
||||
// (Dolt/MySQL bin collation, Postgres "C", SQLite case_sensitive_like — see
|
||||
// bd-oyvc2.10), so a different-cased sibling parent's children never advance
|
||||
// The self-heal scan (id LIKE parent.'%') is case-sensitive on Dolt and SQLite
|
||||
// (see bd-oyvc2.10), so a different-cased sibling parent's children never advance
|
||||
// this parent's counter.
|
||||
func testAuditGetNextChildIDCaseSensitive(t *testing.T, f Factory) {
|
||||
s := f(t)
|
||||
|
|
|
|||
|
|
@ -182,7 +182,7 @@ func testAuditReadyWorkDepCreatedAtParity(t *testing.T, f Factory) {
|
|||
}
|
||||
|
||||
// countByColumnInTx emits COALESCE(priority, ”) GROUP BY priority; priority is
|
||||
// integer NOT NULL. MySQL/Dolt coerce the int to a string key, then
|
||||
// integer NOT NULL. Both maintained implementations return a string key, then
|
||||
// countGroupForTablesInTx prepends 'P'. Reference keys: P0/P1/P2.
|
||||
func testAuditCountByPriority(t *testing.T, f Factory) {
|
||||
s := f(t)
|
||||
|
|
@ -366,7 +366,7 @@ func testAuditSearchIdenticalTimestampIDOrder(t *testing.T, f Factory) {
|
|||
}
|
||||
|
||||
// SortBy="closed" emits ORDER BY closed_at DESC, id ASC. closed_at is nullable;
|
||||
// MySQL/Dolt place NULLs LAST on DESC. The open (NULL-closed) rows follow the
|
||||
// the storage contract places NULLs last on DESC. The open (NULL-closed) rows follow the
|
||||
// closed ones, ordered by id ASC.
|
||||
func testAuditSearchSortByClosedNullsLast(t *testing.T, f Factory) {
|
||||
s := f(t)
|
||||
|
|
@ -409,9 +409,9 @@ func testAuditSearchTextIDBranchExternalRef(t *testing.T, f Factory) {
|
|||
}
|
||||
}
|
||||
|
||||
// LIKE over raw-cased operands is case-sensitive on every backend: Dolt/MySQL use
|
||||
// the utf8mb4_0900_bin table collation, Postgres uses collation "C", and SQLite
|
||||
// sets PRAGMA case_sensitive_like (sqlite/dsn.go) — its default LIKE is
|
||||
// LIKE over raw-cased operands is case-sensitive on both implementations: Dolt uses
|
||||
// a binary table collation, and SQLite set PRAGMA case_sensitive_like (formerly sqlite/dsn.go).
|
||||
// SQLite's default LIKE is
|
||||
// ASCII-case-insensitive and silently diverged (bd-oyvc2.10). IDPrefix filtering
|
||||
// (sqlbuild/filter.go `id LIKE ?`) must therefore distinguish test-AB from test-ab.
|
||||
func testAuditSearchIDPrefixCaseSensitive(t *testing.T, f Factory) {
|
||||
|
|
|
|||
|
|
@ -2,8 +2,6 @@ package conformance
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -12,10 +10,10 @@ import (
|
|||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// Claim / lease behavior (Gas Station v1.1 dead-worker recovery). Every backend
|
||||
// Claim / lease behavior (Gas Station v1.1 dead-worker recovery). Each backend
|
||||
// routes ClaimIssue/ClaimReadyIssue/HeartbeatIssue/ReclaimExpiredLeases through the
|
||||
// shared issueops implementations, so these assertions hold identically on the Dolt
|
||||
// reference and every SQL backend. issueops.WithLeaseTTL pins the lease deterministically
|
||||
// shared issueops implementations, so these assertions hold identically on Dolt and
|
||||
// SQLite. issueops.WithLeaseTTL pins the lease deterministically
|
||||
// so the reclaim path is testable without wall-clock waits.
|
||||
|
||||
// testClaim: claiming an open issue stamps assignee, in_progress, started_at, and a
|
||||
|
|
@ -156,102 +154,6 @@ func issueID(i *types.Issue) string {
|
|||
return i.ID
|
||||
}
|
||||
|
||||
// requireMultiWriter skips the caller unless the backend backs concurrent writers on
|
||||
// separate connections. Postgres and MySQL use a multi-connection pool; SQLite pins its
|
||||
// pool to a single connection (sqliteDialect.Open sets MaxOpenConns(1)) and embedded-Dolt
|
||||
// serializes writers, so a "concurrent" claim race there only ever exercises the trivial
|
||||
// serialized path. The dialect name is the profile signal: sqlkit-backed SQL stores
|
||||
// expose DialectName(); the Dolt reference does not, so it skips via the type assertion.
|
||||
func requireMultiWriter(t *testing.T, s storage.DoltStorage) {
|
||||
t.Helper()
|
||||
dn, ok := s.(interface{ DialectName() string })
|
||||
if !ok {
|
||||
t.Skip("backend does not expose a SQL dialect; concurrent multi-writer claim race not applicable")
|
||||
}
|
||||
switch dn.DialectName() {
|
||||
case "postgres", "mysql":
|
||||
// multi-connection pool: a genuine cross-connection race.
|
||||
default:
|
||||
t.Skipf("dialect %q is single-writer (pool pinned to one connection); concurrent claim race not applicable", dn.DialectName())
|
||||
}
|
||||
}
|
||||
|
||||
// testClaimReadyIssueConcurrentExclusivity races many workers, each on its own pooled
|
||||
// connection, to claim a small pool of ready issues and asserts every issue is claimed by
|
||||
// exactly one worker. This is the multi-writer property the SQL backends exist for — the
|
||||
// whole reason to run bd on Postgres/MySQL instead of single-writer Dolt — and the shared
|
||||
// claim path's cross-process exclusivity (a conditional-UPDATE CAS: only the writer whose
|
||||
// UPDATE finds the row still unassigned wins; the losers' ClaimReadyIssue retries the next
|
||||
// ready candidate) is otherwise only asserted, never raced. Gated to the multi-connection
|
||||
// backends; see requireMultiWriter.
|
||||
func testClaimReadyIssueConcurrentExclusivity(t *testing.T, f Factory) {
|
||||
s := f(t)
|
||||
requireMultiWriter(t, s)
|
||||
|
||||
const readyCount = 8
|
||||
for i := 0; i < readyCount; i++ {
|
||||
must(t, s.CreateIssue(ctx(), withDefaults(&types.Issue{
|
||||
ID: fmt.Sprintf("ccx-%d", i),
|
||||
Title: "ready",
|
||||
Priority: 1,
|
||||
}), "a"))
|
||||
}
|
||||
|
||||
const workers = 24 // > readyCount, so workers must contend and most must lose a race
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
claims = make(map[string]string) // issueID -> claiming worker
|
||||
dupes []string
|
||||
errs []error
|
||||
)
|
||||
start := make(chan struct{})
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < workers; w++ {
|
||||
wg.Add(1)
|
||||
go func(worker string) {
|
||||
defer wg.Done()
|
||||
<-start // barrier: release all workers together to maximize contention
|
||||
// Drain: keep claiming until no ready work remains. Bounded so a bug that
|
||||
// kept returning issues can never hang the suite.
|
||||
for attempt := 0; attempt < readyCount+workers; attempt++ {
|
||||
claimed, err := s.ClaimReadyIssue(ctx(), types.WorkFilter{}, worker)
|
||||
if err != nil {
|
||||
mu.Lock()
|
||||
errs = append(errs, err)
|
||||
mu.Unlock()
|
||||
return
|
||||
}
|
||||
if claimed == nil {
|
||||
return // no ready work left to claim
|
||||
}
|
||||
if claimed.Assignee != worker || claimed.Status != types.StatusInProgress {
|
||||
t.Errorf("claimed %s not owned by claimer: assignee=%q status=%q", claimed.ID, claimed.Assignee, claimed.Status)
|
||||
}
|
||||
mu.Lock()
|
||||
if prev, ok := claims[claimed.ID]; ok {
|
||||
dupes = append(dupes, fmt.Sprintf("%s claimed by both %s and %s", claimed.ID, prev, worker))
|
||||
} else {
|
||||
claims[claimed.ID] = worker
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
}(fmt.Sprintf("worker-%d", w))
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
|
||||
for _, e := range errs {
|
||||
t.Errorf("ClaimReadyIssue errored under concurrency: %v", e)
|
||||
}
|
||||
for _, d := range dupes {
|
||||
t.Errorf("claim exclusivity violated — %s", d)
|
||||
}
|
||||
if len(claims) != readyCount {
|
||||
t.Errorf("distinct claimed issues = %d, want %d (every ready issue claimed exactly once, none lost)", len(claims), readyCount)
|
||||
}
|
||||
}
|
||||
|
||||
// testHeartbeatRenewsLease: a heartbeat extends the lease (and keeps the claim).
|
||||
// Heartbeating with a one-hour TTL must push lease_expires_at far into the future.
|
||||
func testHeartbeatRenewsLease(t *testing.T, f Factory) {
|
||||
|
|
|
|||
|
|
@ -149,7 +149,6 @@ func RunAll(t *testing.T, factory Factory) {
|
|||
t.Run("ClaimNotClaimable", func(t *testing.T) { testClaimNotClaimable(t, factory) })
|
||||
t.Run("ClaimReadyIssue", func(t *testing.T) { testClaimReadyIssue(t, factory) })
|
||||
t.Run("ClaimReadyIssueLabelFilters", func(t *testing.T) { testClaimReadyIssueLabelFilters(t, factory) })
|
||||
t.Run("ClaimReadyIssueConcurrentExclusivity", func(t *testing.T) { testClaimReadyIssueConcurrentExclusivity(t, factory) })
|
||||
t.Run("HeartbeatRenewsLease", func(t *testing.T) { testHeartbeatRenewsLease(t, factory) })
|
||||
t.Run("HeartbeatWisp", func(t *testing.T) { testHeartbeatWisp(t, factory) })
|
||||
t.Run("ReclaimExpiredLease", func(t *testing.T) { testReclaimExpiredLease(t, factory) })
|
||||
|
|
@ -191,12 +190,10 @@ func RunAll(t *testing.T, factory Factory) {
|
|||
t.Run("Transaction", func(t *testing.T) { testTransaction(t, factory) })
|
||||
}
|
||||
|
||||
// RunDeferredReads runs the subset of the suite covering the shared "deferred"
|
||||
// non-version-control reads — statistics, external-ref lookup, and staleness — that
|
||||
// the SQL-family backends (postgres/mysql/sqlite) implement through issueops. RunAll
|
||||
// is the full fail-loud measurement (and stays red on genuinely Dolt-only methods
|
||||
// like slots), so these backends run this focused GREEN gate instead. The Dolt
|
||||
// reference covers the same cases via RunAll.
|
||||
// RunDeferredReads runs the subset of the suite covering SQLite's shared
|
||||
// non-version-control reads: statistics, external-ref lookup, and staleness. RunAll
|
||||
// remains the full Dolt reference; SQLite runs this focused gate for methods supplied
|
||||
// by issueops while its Dolt-only methods fail loudly as unsupported.
|
||||
func RunDeferredReads(t *testing.T, factory Factory) {
|
||||
t.Helper()
|
||||
t.Run("Statistics", func(t *testing.T) { testStatistics(t, factory) })
|
||||
|
|
|
|||
|
|
@ -12,9 +12,9 @@ import (
|
|||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// This file holds the behavior contract for the "portable" non-version-control methods
|
||||
// that the SQL-family backends (postgres/mysql/sqlite) implement through shared issueops
|
||||
// helpers — molecule rollups, repo-mtime cache, event/dependency streams, dependent
|
||||
// This file holds the behavior contract for the portable, non-version-control methods
|
||||
// that SQLite implements through shared issueops helpers: molecule rollups, repo-mtime
|
||||
// cache, event/dependency streams, dependent
|
||||
// counts, wisp cascade discovery, comment/audit writes, id rekey, source-repo purge, and
|
||||
// batch create. Each case is validated against the embedded-Dolt reference (the oracle)
|
||||
// and, once a method is wired into a backend, must match it there too.
|
||||
|
|
@ -23,7 +23,7 @@ import (
|
|||
// ties, tie-broken "current step") is asserted as a set, never positionally.
|
||||
|
||||
// RunPortableMethods runs the portable-method behavior contract. The Dolt reference runs
|
||||
// it via RunAll; each SQL backend runs it once the methods are implemented.
|
||||
// it via RunAll; SQLite runs it for the methods supplied by the shared layer.
|
||||
func RunPortableMethods(t *testing.T, factory Factory) {
|
||||
t.Helper()
|
||||
t.Run("MoleculeProgress", func(t *testing.T) { testGetMoleculeProgress(t, factory) })
|
||||
|
|
|
|||
|
|
@ -48,6 +48,25 @@ func ApplyCLIAutoStart(beadsDir string, cfg *Config) {
|
|||
cfg.AutoStart = resolveAutoStart(true, autoStartCfg, mode)
|
||||
}
|
||||
|
||||
// requireDoltBackend keeps metadata-driven callers from bypassing the storage
|
||||
// factory and interpreting another backend's workspace as Dolt. Removed backend
|
||||
// identifiers deliberately remain recognizable in metadata so this check can fail
|
||||
// closed instead of opening a new, empty Dolt database.
|
||||
func requireDoltBackend(fileCfg *configfile.Config) error {
|
||||
switch fileCfg.Backend {
|
||||
case configfile.BackendPostgres, configfile.BackendMySQL, configfile.BackendSQLite:
|
||||
return fmt.Errorf("configured storage backend %q is no longer supported and cannot be opened as Dolt: %s", fileCfg.Backend, configfile.RemovedBackendDetail(fileCfg.Backend))
|
||||
}
|
||||
if !configfile.IsSupportedBackend(fileCfg.Backend) {
|
||||
return fmt.Errorf("configured storage backend %q in metadata.json is not recognized and cannot be opened as Dolt; %s", fileCfg.Backend, configfile.BackendNotOpenedGuarantee)
|
||||
}
|
||||
backend := fileCfg.GetBackend()
|
||||
if backend != configfile.BackendDolt {
|
||||
return fmt.Errorf("configured storage backend %q cannot be opened as Dolt", backend)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// NewFromConfig creates a DoltStore based on the metadata.json configuration.
|
||||
// beadsDir is the path to the .beads directory.
|
||||
func NewFromConfig(ctx context.Context, beadsDir string) (*DoltStore, error) {
|
||||
|
|
@ -66,6 +85,9 @@ func NewFromConfigWithCLIOptions(ctx context.Context, beadsDir string, cfg *Conf
|
|||
if fileCfg == nil {
|
||||
fileCfg = configfile.DefaultConfig()
|
||||
}
|
||||
if err := requireDoltBackend(fileCfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Apply central server config as defaults for any server fields not
|
||||
// set in the per-project metadata.json. This eliminates the need to
|
||||
|
|
@ -93,6 +115,9 @@ func NewFromConfigWithOptions(ctx context.Context, beadsDir string, cfg *Config)
|
|||
if fileCfg == nil {
|
||||
fileCfg = configfile.DefaultConfig()
|
||||
}
|
||||
if err := requireDoltBackend(fileCfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Apply central server config as defaults for any server fields not
|
||||
// set in the per-project metadata.json.
|
||||
|
|
|
|||
|
|
@ -162,6 +162,50 @@ func TestApplyCLIAutoStart_DisableAutoStartWins(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestConfigConstructorsRejectNonDoltBackends(t *testing.T) {
|
||||
for _, backend := range []string{
|
||||
configfile.BackendSQLite,
|
||||
configfile.BackendPostgres,
|
||||
configfile.BackendMySQL,
|
||||
"mystery",
|
||||
} {
|
||||
t.Run(backend, func(t *testing.T) {
|
||||
beadsDir := t.TempDir()
|
||||
if err := (&configfile.Config{Backend: backend}).Save(beadsDir); err != nil {
|
||||
t.Fatalf("save metadata: %v", err)
|
||||
}
|
||||
|
||||
constructors := map[string]func() (*DoltStore, error){
|
||||
"standard": func() (*DoltStore, error) {
|
||||
return NewFromConfigWithOptions(t.Context(), beadsDir, &Config{DisableAutoStart: true})
|
||||
},
|
||||
"cli": func() (*DoltStore, error) {
|
||||
return NewFromConfigWithCLIOptions(t.Context(), beadsDir, &Config{DisableAutoStart: true})
|
||||
},
|
||||
}
|
||||
for name, construct := range constructors {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
store, err := construct()
|
||||
if store != nil {
|
||||
_ = store.Close()
|
||||
t.Fatalf("non-Dolt backend %q unexpectedly opened as Dolt", backend)
|
||||
}
|
||||
if err == nil || !strings.Contains(err.Error(), "cannot be opened as Dolt") {
|
||||
t.Fatalf("constructor error = %v, want backend mismatch", err)
|
||||
}
|
||||
if backend == configfile.BackendPostgres || backend == configfile.BackendMySQL {
|
||||
for _, want := range []string{"no longer supported", "was not opened or modified", "bd help init-safety"} {
|
||||
if !strings.Contains(err.Error(), want) {
|
||||
t.Errorf("removed-backend constructor error missing %q: %v", want, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCLIDirUsesSharedDoltRootInSharedServerMode(t *testing.T) {
|
||||
sharedRoot := t.TempDir()
|
||||
t.Setenv("BEADS_DOLT_SHARED_SERVER", "1")
|
||||
|
|
|
|||
|
|
@ -1,288 +0,0 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/domain"
|
||||
storagesqlite "github.com/steveyegge/beads/internal/storage/sqlite"
|
||||
"github.com/steveyegge/beads/internal/storage/sqlitedialect"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
func TestDependencyUseCaseSQLiteRejectsHierarchyBlockingButAllowsSiblings(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
dbPath := filepath.Join(t.TempDir(), "beads.db")
|
||||
|
||||
raw, err := sql.Open("sqlite", dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("open raw sqlite: %v", err)
|
||||
}
|
||||
if err := storagesqlite.InitSchema(ctx, raw); err != nil {
|
||||
_ = raw.Close()
|
||||
t.Fatalf("init sqlite schema: %v", err)
|
||||
}
|
||||
if err := raw.Close(); err != nil {
|
||||
t.Fatalf("close raw sqlite: %v", err)
|
||||
}
|
||||
|
||||
runner, err := sqlitedialect.Open(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("open translated sqlite: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = runner.Close() })
|
||||
|
||||
issueRepo := NewIssueSQLRepository(runner)
|
||||
for _, id := range []string{"parent", "child", "sibling"} {
|
||||
if err := issueRepo.Insert(ctx, newTestIssue(id, id), "tester", domain.InsertIssueOpts{}); err != nil {
|
||||
t.Fatalf("insert issue %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
depRepo := NewDependencySQLRepository(runner)
|
||||
for _, childID := range []string{"child", "sibling"} {
|
||||
if err := depRepo.Insert(ctx,
|
||||
newDep(childID, "parent", types.DepParentChild),
|
||||
"tester", domain.DepInsertOpts{}); err != nil {
|
||||
t.Fatalf("insert parent-child %s -> parent: %v", childID, err)
|
||||
}
|
||||
}
|
||||
|
||||
uc := domain.NewDependencyUseCase(depRepo)
|
||||
if _, err := uc.AddDependencies(ctx, []*types.Dependency{
|
||||
newDep("sibling", "child", types.DepBlocks),
|
||||
}, "tester", domain.BulkAddDepsOpts{}); err != nil {
|
||||
t.Fatalf("siblings may carry ordering edges: %v", err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
dep *types.Dependency
|
||||
wantError string
|
||||
}{
|
||||
{
|
||||
name: "child blocked by ancestor",
|
||||
dep: newDep("child", "parent", types.DepBlocks),
|
||||
wantError: "cannot be blocked by its ancestor",
|
||||
},
|
||||
{
|
||||
name: "conditional child blocked by ancestor",
|
||||
dep: newDep("child", "parent", types.DepConditionalBlocks),
|
||||
wantError: "cannot be blocked by its ancestor",
|
||||
},
|
||||
{
|
||||
name: "parent blocked by descendant",
|
||||
dep: newDep("parent", "child", types.DepBlocks),
|
||||
wantError: "cannot be blocked by its descendant",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := uc.AddDependencies(ctx, []*types.Dependency{tt.dep}, "tester", domain.BulkAddDepsOpts{})
|
||||
if err == nil {
|
||||
t.Fatalf("AddDependencies(%s -> %s) succeeded, want hierarchy rejection", tt.dep.IssueID, tt.dep.DependsOnID)
|
||||
}
|
||||
if got := err.Error(); !strings.Contains(got, tt.wantError) {
|
||||
t.Fatalf("error = %q, want substring %q", got, tt.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDependencyUseCaseSQLiteRejectsCombinedSchedulingCycles(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
dbPath := filepath.Join(t.TempDir(), "beads.db")
|
||||
raw, err := sql.Open("sqlite", dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("open raw sqlite: %v", err)
|
||||
}
|
||||
if err := storagesqlite.InitSchema(ctx, raw); err != nil {
|
||||
_ = raw.Close()
|
||||
t.Fatalf("init sqlite schema: %v", err)
|
||||
}
|
||||
if err := raw.Close(); err != nil {
|
||||
t.Fatalf("close raw sqlite: %v", err)
|
||||
}
|
||||
runner, err := sqlitedialect.Open(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("open translated sqlite: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = runner.Close() })
|
||||
|
||||
issueRepo := NewIssueSQLRepository(runner)
|
||||
for _, id := range []string{
|
||||
"block-a", "block-b", "block-c",
|
||||
"parent-a", "parent-b", "parent-c",
|
||||
"bulk-a", "bulk-b", "bulk-c",
|
||||
"deferred-a", "deferred-b", "deferred-c",
|
||||
} {
|
||||
if err := issueRepo.Insert(ctx, newTestIssue(id, id), "tester", domain.InsertIssueOpts{}); err != nil {
|
||||
t.Fatalf("insert issue %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
uc := domain.NewDependencyUseCase(NewDependencySQLRepository(runner))
|
||||
|
||||
for _, dep := range []*types.Dependency{
|
||||
newDep("block-a", "block-b", types.DepBlocks),
|
||||
newDep("block-b", "block-c", types.DepParentChild),
|
||||
} {
|
||||
if err := uc.AddDependency(ctx, dep, "tester"); err != nil {
|
||||
t.Fatalf("seed block-closing path: %v", err)
|
||||
}
|
||||
}
|
||||
if err := uc.AddDependency(ctx, newDep("block-c", "block-a", types.DepConditionalBlocks), "tester"); err == nil || !strings.Contains(err.Error(), "cycle") {
|
||||
t.Fatalf("blocking closer error = %v, want combined-cycle rejection", err)
|
||||
}
|
||||
|
||||
for _, dep := range []*types.Dependency{
|
||||
newDep("parent-a", "parent-b", types.DepBlocks),
|
||||
newDep("parent-b", "parent-c", types.DepConditionalBlocks),
|
||||
} {
|
||||
if err := uc.AddDependency(ctx, dep, "tester"); err != nil {
|
||||
t.Fatalf("seed parent-child-closing path: %v", err)
|
||||
}
|
||||
}
|
||||
if err := uc.AddDependency(ctx, newDep("parent-c", "parent-a", types.DepParentChild), "tester"); err == nil || !strings.Contains(err.Error(), "cycle") {
|
||||
t.Fatalf("parent-child closer error = %v, want combined-cycle rejection", err)
|
||||
}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
ids [3]string
|
||||
opts domain.BulkAddDepsOpts
|
||||
}{
|
||||
{name: "per edge", ids: [3]string{"bulk-a", "bulk-b", "bulk-c"}},
|
||||
{name: "deferred whole graph", ids: [3]string{"deferred-a", "deferred-b", "deferred-c"}, opts: domain.BulkAddDepsOpts{SkipPerEdgeCycleCheck: true}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
_, err := uc.AddDependencies(ctx, []*types.Dependency{
|
||||
newDep(tc.ids[0], tc.ids[1], types.DepBlocks),
|
||||
newDep(tc.ids[1], tc.ids[2], types.DepParentChild),
|
||||
newDep(tc.ids[2], tc.ids[0], types.DepBlocks),
|
||||
}, "tester", tc.opts)
|
||||
if err == nil || !strings.Contains(err.Error(), "cycle") {
|
||||
t.Fatalf("bulk mixed cycle error = %v, want rejection", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDependencyUseCaseSQLiteValidatesPlannedHierarchyBeforeBlocking(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
dbPath := filepath.Join(t.TempDir(), "beads.db")
|
||||
raw, err := sql.Open("sqlite", dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("open raw sqlite: %v", err)
|
||||
}
|
||||
if err := storagesqlite.InitSchema(ctx, raw); err != nil {
|
||||
_ = raw.Close()
|
||||
t.Fatalf("init sqlite schema: %v", err)
|
||||
}
|
||||
if err := raw.Close(); err != nil {
|
||||
t.Fatalf("close raw sqlite: %v", err)
|
||||
}
|
||||
runner, err := sqlitedialect.Open(dbPath)
|
||||
if err != nil {
|
||||
t.Fatalf("open translated sqlite: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = runner.Close() })
|
||||
|
||||
issueRepo := NewIssueSQLRepository(runner)
|
||||
for _, id := range []string{
|
||||
"anc-grand", "anc-parent", "anc-child",
|
||||
"desc-grand", "desc-parent", "desc-child",
|
||||
"pair-parent", "pair-child",
|
||||
"sibs-parent", "sibs-a", "sibs-b",
|
||||
"prec-parent", "prec-middle", "prec-child",
|
||||
} {
|
||||
if err := issueRepo.Insert(ctx, newTestIssue(id, id), "tester", domain.InsertIssueOpts{}); err != nil {
|
||||
t.Fatalf("insert issue %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
depRepo := NewDependencySQLRepository(runner)
|
||||
uc := domain.NewDependencyUseCase(depRepo)
|
||||
tests := []struct {
|
||||
name string
|
||||
deps []*types.Dependency
|
||||
wantError string
|
||||
}{
|
||||
{
|
||||
name: "block-first child to planned ancestor",
|
||||
deps: []*types.Dependency{
|
||||
newDep("anc-child", "anc-grand", types.DepConditionalBlocks),
|
||||
newDep("anc-child", "anc-parent", types.DepParentChild),
|
||||
newDep("anc-parent", "anc-grand", types.DepParentChild),
|
||||
},
|
||||
wantError: "cannot be blocked by its ancestor",
|
||||
},
|
||||
{
|
||||
name: "block-first ancestor to planned descendant",
|
||||
deps: []*types.Dependency{
|
||||
newDep("desc-grand", "desc-child", types.DepBlocks),
|
||||
newDep("desc-child", "desc-parent", types.DepParentChild),
|
||||
newDep("desc-parent", "desc-grand", types.DepParentChild),
|
||||
},
|
||||
wantError: "cannot be blocked by its descendant",
|
||||
},
|
||||
{
|
||||
name: "block-first same pair conflict reports hierarchy",
|
||||
deps: []*types.Dependency{
|
||||
newDep("pair-child", "pair-parent", types.DepBlocks),
|
||||
newDep("pair-child", "pair-parent", types.DepParentChild),
|
||||
},
|
||||
wantError: "cannot be blocked by its ancestor",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
_, err := uc.AddDependencies(ctx, tt.deps, "tester", domain.BulkAddDepsOpts{})
|
||||
if err == nil || !strings.Contains(err.Error(), tt.wantError) {
|
||||
t.Fatalf("error = %v, want %q", err, tt.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
_, err = uc.AddDependencies(ctx, []*types.Dependency{
|
||||
newDep("sibs-a", "sibs-b", types.DepBlocks), // Deliberately first.
|
||||
newDep("sibs-a", "sibs-parent", types.DepParentChild),
|
||||
newDep("sibs-b", "sibs-parent", types.DepParentChild),
|
||||
}, "tester", domain.BulkAddDepsOpts{})
|
||||
if err != nil {
|
||||
t.Fatalf("planned sibling ordering edge: %v", err)
|
||||
}
|
||||
|
||||
for _, id := range []string{"wplan-grand", "wplan-parent", "wplan-child"} {
|
||||
issue := newTestIssue(id, id)
|
||||
issue.Ephemeral = true
|
||||
if err := issueRepo.Insert(ctx, issue, "tester", domain.InsertIssueOpts{UseWispsTable: true}); err != nil {
|
||||
t.Fatalf("insert wisp %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
_, err = uc.AddWispDependencies(ctx, []*types.Dependency{
|
||||
newDep("wplan-child", "wplan-grand", types.DepConditionalBlocks), // Deliberately first.
|
||||
newDep("wplan-child", "wplan-parent", types.DepParentChild),
|
||||
newDep("wplan-parent", "wplan-grand", types.DepParentChild),
|
||||
}, "tester", domain.BulkAddDepsOpts{})
|
||||
if err == nil || !strings.Contains(err.Error(), "cannot be blocked by its ancestor") {
|
||||
t.Fatalf("planned wisp hierarchy error = %v, want ancestor rejection", err)
|
||||
}
|
||||
|
||||
// If a legacy blocking path also forms a cycle, hierarchy remains the
|
||||
// canonical error, matching the classic issueops path.
|
||||
for _, dep := range []*types.Dependency{
|
||||
newDep("prec-child", "prec-parent", types.DepParentChild),
|
||||
newDep("prec-parent", "prec-middle", types.DepBlocks),
|
||||
newDep("prec-middle", "prec-child", types.DepBlocks),
|
||||
} {
|
||||
if err := depRepo.Insert(ctx, dep, "tester", domain.DepInsertOpts{CycleValidated: true}); err != nil {
|
||||
t.Fatalf("seed dependency %s -> %s: %v", dep.IssueID, dep.DependsOnID, err)
|
||||
}
|
||||
}
|
||||
err = uc.AddDependency(ctx, newDep("prec-child", "prec-parent", types.DepBlocks), "tester")
|
||||
if err == nil || !strings.Contains(err.Error(), "cannot be blocked by its ancestor") {
|
||||
t.Fatalf("combined hierarchy/cycle error = %v, want ancestor rejection", err)
|
||||
}
|
||||
}
|
||||
|
|
@ -4,8 +4,8 @@
|
|||
//
|
||||
// Slice-wrapping stubs for the Iter* methods. The embedded Dolt backend
|
||||
// uses a per-method short-lived connection model (`withConn`) which is
|
||||
// incompatible with the dedicated-conn cursor pattern used by the
|
||||
// streaming iterators in internal/storage/dolt and internal/storage/postgres.
|
||||
// incompatible with the dedicated-connection cursor pattern used by the
|
||||
// streaming iterators in internal/storage/dolt.
|
||||
// The interface ships complete now (be-jaavsb / be-yinl4d); a follow-up
|
||||
// child of be-yinl4d may add a streaming variant if EmbeddedDoltStore
|
||||
// gains a cursor-conn API. For now every Iter* method materializes the
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ import "fmt"
|
|||
// taxonomy; this is the embryo of the seam's ErrUnsupported{Op, Backend}.
|
||||
type ErrUnsupported struct {
|
||||
Op string // method name, e.g. "AddLabel" or "Transaction.CreateIssues"
|
||||
Backend string // e.g. "postgres"
|
||||
Backend string // e.g. "sqlite"
|
||||
}
|
||||
|
||||
func (e *ErrUnsupported) Error() string {
|
||||
|
|
|
|||
|
|
@ -334,29 +334,6 @@ func unmarkBlockedTemplateForWisps() string {
|
|||
`, waitsForGateBlockedSQL)
|
||||
}
|
||||
|
||||
// SelfReferentialRecomputeTemplates returns the four is_blocked recompute UPDATE
|
||||
// statements exactly as issueops emits them to the driver (with a representative
|
||||
// IN-clause batch substituted for the placeholder). Each one updates issues/wisps
|
||||
// while its WHERE clause self-references that same target table in EXISTS subqueries,
|
||||
// which MySQL 8 rejects with error 1093 unless the mysqldialect driver rewrites the
|
||||
// statement into a materialized derived-table form.
|
||||
//
|
||||
// It is exported solely so that rewrite can be regression-tested against these real
|
||||
// templates instead of a hand-copied fixture: if a template ever drifts out of the
|
||||
// shape the rewrite recognizes (a predicate before `id IN`, a backticked table, a
|
||||
// renamed alias, changed whitespace), the mysqldialect rewrite test fails in a normal
|
||||
// non-gated `go test` run rather than only under the env-gated MySQL conformance suite.
|
||||
func SelfReferentialRecomputeTemplates() []string {
|
||||
placeholders, _ := buildSQLInClause([]string{"a", "b", "c"})
|
||||
render := func(tmpl string) string { return fmt.Sprintf(tmpl, placeholders) }
|
||||
return []string{
|
||||
render(markBlockedTemplateForIssues()),
|
||||
render(unmarkBlockedTemplateForIssues()),
|
||||
render(markBlockedTemplateForWisps()),
|
||||
render(unmarkBlockedTemplateForWisps()),
|
||||
}
|
||||
}
|
||||
|
||||
//nolint:gosec // G201: callers pass constant templates; only IN-clause placeholders are formatted in.
|
||||
func runMarkUnmarkBatchedInTx(ctx context.Context, tx DBTX, markTmpl, unmarkTmpl string, ids []string) (int64, error) {
|
||||
var changed int64
|
||||
|
|
|
|||
|
|
@ -186,9 +186,7 @@ func countByColumnInTx(ctx context.Context, tx *sql.Tx, filter types.IssueFilter
|
|||
whereSQL = " WHERE " + strings.Join(clauses, " AND ")
|
||||
}
|
||||
// CAST the group column to text before COALESCE so an integer column (priority)
|
||||
// COALESCEs with '' consistently. MySQL/Dolt coerce int->text implicitly, but
|
||||
// Postgres rejects COALESCE(integer, '') (22P02); CAST(... AS CHAR) is portable
|
||||
// (pgdialect -> ::text, native on MySQL/SQLite) and byte-identical on every backend.
|
||||
// COALESCEs with '' consistently across Dolt and SQLite.
|
||||
//nolint:gosec // G201: tables.Main hardcoded; col validated by caller
|
||||
query := fmt.Sprintf("SELECT COALESCE(CAST(%s AS CHAR), ''), COUNT(*) FROM %s%s GROUP BY %s", col, tables.Main, whereSQL, col)
|
||||
rows, err := tx.QueryContext(ctx, query, args...)
|
||||
|
|
|
|||
|
|
@ -905,10 +905,9 @@ func ReconcileChildCounters(ctx context.Context, tx *sql.Tx, issues []*types.Iss
|
|||
if err == nil && current >= b.maxChild {
|
||||
continue
|
||||
}
|
||||
// Qualify the existing-row column with the table name. Bare `last_child`
|
||||
// on the update RHS is a valid MySQL "current row value" reference, but
|
||||
// Postgres sees it as ambiguous between the target row and EXCLUDED
|
||||
// (SQLSTATE 42702); table-qualifying it is unambiguous on every backend.
|
||||
// Qualify the existing-row column with the table name so the canonical
|
||||
// MySQL form and SQLite's translated ON CONFLICT form both unambiguously
|
||||
// refer to the target row rather than the incoming value.
|
||||
//nolint:gosec // G201: table is one of two hardcoded constants.
|
||||
if _, err := tx.ExecContext(ctx, fmt.Sprintf(`
|
||||
INSERT INTO %[1]s (parent_id, last_child) VALUES (?, ?)
|
||||
|
|
|
|||
|
|
@ -89,10 +89,9 @@ func issueUpsertAssignments(table string, rejectStaleUpdate bool) string {
|
|||
assignments := make([]string, 0, len(issueUpsertColumns))
|
||||
for _, col := range issueUpsertColumns {
|
||||
if rejectStaleUpdate {
|
||||
// Qualify the existing-row references with the table name. Postgres's
|
||||
// ON CONFLICT DO UPDATE rejects a bare `updated_at` as ambiguous (it could
|
||||
// be the target row or EXCLUDED); <table>.updated_at is unambiguous and is
|
||||
// also accepted by MySQL/Dolt/SQLite. VALUES(...) is the incoming row.
|
||||
// Qualify existing-row references with the table name so the target value
|
||||
// remains unambiguous after the SQLite upsert translation. VALUES(...) is
|
||||
// the incoming row in the canonical Dolt/MySQL-dialect statement.
|
||||
assignments = append(assignments,
|
||||
fmt.Sprintf("%s = IF(VALUES(updated_at) > %s.updated_at, VALUES(%s), %s.%s)", col, table, col, table, col))
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
//
|
||||
// Iter[T] is a generic streaming-iterator shape that lets storage callers
|
||||
// walk an unbounded result set without materializing the whole slice.
|
||||
// Per-method implementations live in the dolt/ and postgres/ sub-packages.
|
||||
// Per-method implementations live in the concrete backend packages.
|
||||
//
|
||||
// See be-jaavsb (design) and be-yinl4d (architecture) for the motivation.
|
||||
package storage
|
||||
|
|
|
|||
|
|
@ -1,107 +0,0 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"os"
|
||||
"regexp"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/conformance"
|
||||
)
|
||||
|
||||
// legitimatelyUnsupported is the EXPLICIT denominator of every storage.DoltStorage
|
||||
// method the Postgres wedge deliberately does not implement, each with a reason.
|
||||
//
|
||||
// The interface-completeness gate (TestInterfaceCompleteness) asserts the
|
||||
// generated capability shell (unsupported_gen.go) contains EXACTLY these methods
|
||||
// — no more, no less. This is the guard the red-team had to stand in for: a
|
||||
// method that silently resolves to the typed-unsupported shell (the class that
|
||||
// bit us: DeleteIssues, GetCustomStatuses, the batch reads, …) fails this test
|
||||
// at build time instead of failing a user at runtime. Adding a method to the
|
||||
// shell without an entry here is a hard error that forces the triage question:
|
||||
// "implement it in sqlkit, or record here why the wedge legitimately omits it."
|
||||
var legitimatelyUnsupported = map[string]string{
|
||||
// VersionControl — the Dolt commit graph. The wedge's whole premise is
|
||||
// "bd on Postgres, history/version-control simply absent".
|
||||
"Branch": "VC: Dolt branch", "Checkout": "VC: Dolt checkout",
|
||||
"CurrentBranch": "VC", "DeleteBranch": "VC", "ListBranches": "VC",
|
||||
// Commit/CommitWithConfig/CommitMergeResolution are NO-OPS on Postgres (data
|
||||
// is already durable per-tx), not unsupported — implemented on postgres.Store.
|
||||
"CommitExists": "VC", "GetCurrentCommit": "VC: Dolt hash", "Status": "VC",
|
||||
"Log": "VC: dolt_log", "Merge": "VC", "GetConflicts": "VC", "ResolveConflicts": "VC",
|
||||
|
||||
// HistoryViewer — Dolt time-travel.
|
||||
"History": "history: dolt_log", "AsOf": "history: as-of ref", "Diff": "history: dolt_diff",
|
||||
|
||||
// RemoteStore — Dolt remotes.
|
||||
"AddRemote": "remote", "RemoveRemote": "remote", "HasRemote": "remote", "ListRemotes": "remote",
|
||||
"Push": "remote", "Pull": "remote", "ForcePush": "remote", "PushRemote": "remote",
|
||||
"PullRemote": "remote", "Fetch": "remote", "PushTo": "remote", "PullFrom": "remote",
|
||||
|
||||
// SyncStore — Dolt peer sync.
|
||||
"Sync": "sync", "SyncStatus": "sync",
|
||||
|
||||
// FederationStore — Dolt-remote-backed peer table.
|
||||
"AddFederationPeer": "federation", "GetFederationPeer": "federation",
|
||||
"ListFederationPeers": "federation", "RemoveFederationPeer": "federation",
|
||||
|
||||
// CompactionStore — bound to Dolt commit hashes.
|
||||
"CheckEligibility": "compaction", "ApplyCompaction": "compaction",
|
||||
"GetTier1Candidates": "compaction", "GetTier2Candidates": "compaction",
|
||||
"SnapshotIssue": "compaction", "GetCompactionSnapshot": "compaction", "RestoreFromSnapshot": "compaction",
|
||||
|
||||
// MergeSlot — the Dolt merge-serialization primitive.
|
||||
"MergeSlotCreate": "merge-slot", "MergeSlotCheck": "merge-slot",
|
||||
"MergeSlotAcquire": "merge-slot", "MergeSlotRelease": "merge-slot",
|
||||
}
|
||||
|
||||
var shellMethodRe = regexp.MustCompile(`func \(unsupportedDoltStorage\) ([A-Za-z0-9]+)\(`)
|
||||
|
||||
// TestInterfaceCompleteness is the interface-completeness gate: the generated
|
||||
// shell must equal the audited legitimatelyUnsupported set, both directions.
|
||||
func TestInterfaceCompleteness(t *testing.T) {
|
||||
src, err := os.ReadFile("unsupported_gen.go")
|
||||
if err != nil {
|
||||
t.Fatalf("read unsupported_gen.go: %v", err)
|
||||
}
|
||||
shell := map[string]bool{}
|
||||
for _, m := range shellMethodRe.FindAllStringSubmatch(string(src), -1) {
|
||||
shell[m[1]] = true
|
||||
}
|
||||
if len(shell) == 0 {
|
||||
t.Fatal("parsed 0 shell methods — regex or file drift")
|
||||
}
|
||||
|
||||
// (1) No SILENT gap: every shell method must be explicitly justified.
|
||||
var unjustified []string
|
||||
for m := range shell {
|
||||
if _, ok := legitimatelyUnsupported[m]; !ok {
|
||||
unjustified = append(unjustified, m)
|
||||
}
|
||||
}
|
||||
sort.Strings(unjustified)
|
||||
for _, m := range unjustified {
|
||||
t.Errorf("method %q resolves to the typed-unsupported shell but is NOT in legitimatelyUnsupported: implement it on *sqlkit.Store, or add it here with a reason", m)
|
||||
}
|
||||
|
||||
// (2) No STALE allowlist: every justified method must still be in the shell
|
||||
// (else it was implemented and the entry should be removed).
|
||||
var stale []string
|
||||
for m := range legitimatelyUnsupported {
|
||||
if !shell[m] {
|
||||
stale = append(stale, m)
|
||||
}
|
||||
}
|
||||
sort.Strings(stale)
|
||||
for _, m := range stale {
|
||||
t.Errorf("method %q is in legitimatelyUnsupported but no longer in the shell (implemented?): remove the allowlist entry", m)
|
||||
}
|
||||
}
|
||||
|
||||
// TestUnsupportedContract is the behavioral complement to TestInterfaceCompleteness:
|
||||
// every allowlisted method must actually return a typed storage.ErrUnsupported when
|
||||
// called, not panic or return a different error. DB-free — the generated stubs ignore
|
||||
// their receiver, so a zero-value store answers them.
|
||||
func TestUnsupportedContract(t *testing.T) {
|
||||
conformance.RunUnsupportedContract(t, &Store{}, legitimatelyUnsupported)
|
||||
}
|
||||
|
|
@ -1,80 +0,0 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/conformance"
|
||||
)
|
||||
|
||||
// TestConformance runs bd's backend-agnostic storage conformance suite against the
|
||||
// MySQL backend. Every failure is a MySQL gap: an allowlisted-unsupported method
|
||||
// (expected — completeness_test.go) or a latent divergence. Gated on
|
||||
// BEADS_MYSQL_TEST_URL (a server DSN, e.g. user:pass@tcp(host:3306)/).
|
||||
func TestConformance(t *testing.T) {
|
||||
url := os.Getenv("BEADS_MYSQL_TEST_URL")
|
||||
if url == "" {
|
||||
t.Skip("BEADS_MYSQL_TEST_URL not set")
|
||||
}
|
||||
conformance.RunAll(t, mysqlConformanceFactory(url))
|
||||
}
|
||||
|
||||
// TestDeferredReads runs the shared deferred non-version-control reads (statistics,
|
||||
// external-ref, stale) that the backend now implements through issueops, as a GREEN
|
||||
// gate. RunAll stays the fail-loud measurement (red on genuinely Dolt-only methods
|
||||
// like slots), so this focused gate is what conformance.sh runs. Gated on
|
||||
// BEADS_MYSQL_TEST_URL.
|
||||
func TestDeferredReads(t *testing.T) {
|
||||
url := os.Getenv("BEADS_MYSQL_TEST_URL")
|
||||
if url == "" {
|
||||
t.Skip("BEADS_MYSQL_TEST_URL not set")
|
||||
}
|
||||
conformance.RunDeferredReads(t, mysqlConformanceFactory(url))
|
||||
}
|
||||
|
||||
// TestPortableMethods runs the full portable-method behavior contract. Red until the
|
||||
// methods are wired into sqlkit; green after. Gated on BEADS_MYSQL_TEST_URL.
|
||||
func TestPortableMethods(t *testing.T) {
|
||||
url := os.Getenv("BEADS_MYSQL_TEST_URL")
|
||||
if url == "" {
|
||||
t.Skip("BEADS_MYSQL_TEST_URL not set")
|
||||
}
|
||||
conformance.RunPortableMethods(t, mysqlConformanceFactory(url))
|
||||
}
|
||||
|
||||
// mysqlConformanceFactory provisions a fresh, isolated database per sub-test against
|
||||
// the MySQL server at url, seeded with issue_prefix as `bd init` leaves it.
|
||||
func mysqlConformanceFactory(url string) conformance.Factory {
|
||||
base := time.Now().UnixNano()
|
||||
var seq int64
|
||||
return func(t *testing.T) storage.DoltStorage {
|
||||
ctx := context.Background()
|
||||
database := fmt.Sprintf("conf_%d_%d", base, atomic.AddInt64(&seq, 1))
|
||||
|
||||
st, err := Provision(ctx, url, database)
|
||||
if err != nil {
|
||||
t.Fatalf("Provision: %v", err)
|
||||
}
|
||||
// The conformance factory contract wants an init'd store (issue_prefix set),
|
||||
// as `bd init` leaves it. No Commit — MySQL has no commit graph.
|
||||
if err := st.SetConfig(ctx, "issue_prefix", "test"); err != nil {
|
||||
t.Fatalf("SetConfig(issue_prefix): %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if serverDSN, e := withDatabase(url, ""); e == nil {
|
||||
if srv, e2 := sql.Open("mysql", serverDSN); e2 == nil {
|
||||
_, _ = srv.ExecContext(context.Background(), "DROP DATABASE IF EXISTS `"+database+"`")
|
||||
_ = srv.Close()
|
||||
}
|
||||
}
|
||||
_ = st.Close()
|
||||
})
|
||||
return st
|
||||
}
|
||||
}
|
||||
|
|
@ -1,67 +0,0 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/creds"
|
||||
"github.com/steveyegge/beads/internal/storage/mysqldialect"
|
||||
)
|
||||
|
||||
// resolveDSNCredential returns baseDSN with a credential resolved and placed, ready to
|
||||
// connect. The precedence, high to low:
|
||||
//
|
||||
// - BEADS_MYSQL_CREDENTIAL_COMMAND set is a hard error: an identity credential is
|
||||
// presented as a username to a gateway, and this backend connects directly;
|
||||
// - the DSN already carries a password (a full BEADS_MYSQL_URL, or an explicit
|
||||
// --mysql-url) — it wins outright, no ladder is applied;
|
||||
// - BEADS_MYSQL_PASSWORD_COMMAND — a credential command (e.g. Vault / RDS-IAM / GCP-IAM);
|
||||
// run at open time, cached until near expiry;
|
||||
// - BEADS_MYSQL_PASSWORD — a static password;
|
||||
// - the credentials file [host:port] ($BEADS_CREDENTIALS_FILE or
|
||||
// ~/.config/beads/credentials);
|
||||
// - nothing configured — baseDSN is returned untouched and the connection is
|
||||
// attempted with an empty password (valid only for an unsecured dev server;
|
||||
// go-sql-driver reads no env vars or option files, so there is no ~/.my.cnf
|
||||
// fallback).
|
||||
//
|
||||
// It fails closed: if a configured credential command errors, the open aborts — it
|
||||
// never silently downgrades to a lower rung.
|
||||
func resolveDSNCredential(ctx context.Context, cfg *configfile.Config, baseDSN string) (string, error) {
|
||||
if os.Getenv("BEADS_MYSQL_CREDENTIAL_COMMAND") != "" {
|
||||
return "", fmt.Errorf("mysql: BEADS_MYSQL_CREDENTIAL_COMMAND is reserved: an identity credential is presented as a username to a gateway, and the mysql backend connects directly to the database with no gateway to present it to; use BEADS_MYSQL_PASSWORD_COMMAND for a helper that produces a password")
|
||||
}
|
||||
// host:port keys the credentials-file lookup; ok=false (unix socket / unparseable)
|
||||
// leaves the file rung not-configured (zero Host/Port), never failing.
|
||||
host, port, _ := mysqldialect.HostPort(baseDSN)
|
||||
return resolveDSNWithSources(ctx, baseDSN,
|
||||
creds.CommandSource{Command: cfg.GetMySQLPasswordCommand(), Kind: creds.KindSecret, Label: "BEADS_MYSQL_PASSWORD_COMMAND"},
|
||||
creds.EnvSource{Var: "BEADS_MYSQL_PASSWORD"},
|
||||
creds.FileSource{Host: host, Port: port, Lookup: configfile.LookupCredentialsPassword, Label: "credentials-file"},
|
||||
)
|
||||
}
|
||||
|
||||
// resolveDSNWithSources is the testable core of resolveDSNCredential: it applies the
|
||||
// given credential ladder to baseDSN.
|
||||
func resolveDSNWithSources(ctx context.Context, baseDSN string, sources ...creds.Source) (string, error) {
|
||||
if mysqldialect.HasPassword(baseDSN) {
|
||||
return baseDSN, nil
|
||||
}
|
||||
cred, ok, err := creds.ResolveLadder(ctx, sources...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if !ok {
|
||||
return baseDSN, nil
|
||||
}
|
||||
// The SQL backends take a secret in the password slot; an identity credential
|
||||
// (presented as a username to a gateway) has no home on a direct connection, so
|
||||
// refuse it loudly rather than land a token where a password belongs. Defense in
|
||||
// depth behind the BEADS_MYSQL_CREDENTIAL_COMMAND reject above.
|
||||
if cred.Kind != creds.KindSecret {
|
||||
return "", fmt.Errorf("mysql: credential from %s is an identity, not a password; the mysql backend connects directly and has no gateway to present it to", cred.Source)
|
||||
}
|
||||
return mysqldialect.WithCredential(baseDSN, cred.Username, cred.Value)
|
||||
}
|
||||
|
|
@ -1,288 +0,0 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
gomysql "github.com/go-sql-driver/mysql"
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/creds"
|
||||
)
|
||||
|
||||
const baseDSN = "bts@tcp(127.0.0.1:55441)/"
|
||||
|
||||
// TestMain points the credentials-file rung at a nonexistent path in a private temp
|
||||
// dir so tests never read the developer's real ~/.config/beads/credentials.
|
||||
// go-sql-driver reads no ambient env/option files, so there is nothing else to
|
||||
// neutralize. Tests that exercise the file rung override it per-test with t.Setenv.
|
||||
func TestMain(m *testing.M) {
|
||||
dir, err := os.MkdirTemp("", "beads-mysql-cred-test")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
os.Setenv("BEADS_CREDENTIALS_FILE", filepath.Join(dir, "none"))
|
||||
code := m.Run()
|
||||
_ = os.RemoveAll(dir)
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
// password parses out and returns the password go-sql-driver sees.
|
||||
func password(t *testing.T, dsn string) string {
|
||||
t.Helper()
|
||||
cfg, err := gomysql.ParseDSN(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("gomysql.ParseDSN(%q): %v", dsn, err)
|
||||
}
|
||||
return cfg.Passwd
|
||||
}
|
||||
|
||||
// user parses out and returns the username go-sql-driver sees.
|
||||
func user(t *testing.T, dsn string) string {
|
||||
t.Helper()
|
||||
cfg, err := gomysql.ParseDSN(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("gomysql.ParseDSN(%q): %v", dsn, err)
|
||||
}
|
||||
return cfg.User
|
||||
}
|
||||
|
||||
// writeCredsFile writes an INI credentials file and returns its path.
|
||||
func writeCredsFile(t *testing.T, body string) string {
|
||||
t.Helper()
|
||||
p := filepath.Join(t.TempDir(), "credentials")
|
||||
if err := os.WriteFile(p, []byte(body), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func TestResolveDSNCredentialCommand(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", "printf cmd-pw")
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "")
|
||||
got, err := resolveDSNCredential(context.Background(), &configfile.Config{}, baseDSN)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pw := password(t, got); pw != "cmd-pw" {
|
||||
t.Fatalf("password = %q, want cmd-pw", pw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveDSNCredentialEnv(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", "")
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "env-pw")
|
||||
got, err := resolveDSNCredential(context.Background(), &configfile.Config{}, baseDSN)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pw := password(t, got); pw != "env-pw" {
|
||||
t.Fatalf("password = %q, want env-pw", pw)
|
||||
}
|
||||
}
|
||||
|
||||
// The command out-ranks the static env password.
|
||||
func TestResolveDSNCredentialCommandBeatsEnv(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", "printf cmd-wins")
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "env-loses")
|
||||
got, err := resolveDSNCredential(context.Background(), &configfile.Config{}, baseDSN)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pw := password(t, got); pw != "cmd-wins" {
|
||||
t.Fatalf("password = %q, want cmd-wins", pw)
|
||||
}
|
||||
}
|
||||
|
||||
// A configured-but-erroring command aborts the open and does NOT fall through to the
|
||||
// env password. Here a bare token with whitespace is rejected by parseCredential — a
|
||||
// configured error, not a command exit failure.
|
||||
func TestResolveDSNCredentialFailsClosed(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", `printf 'access denied'`)
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "env-would-be-wrong")
|
||||
_, err := resolveDSNCredential(context.Background(), &configfile.Config{}, baseDSN)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error (fail-closed); a broken command must not fall through to BEADS_MYSQL_PASSWORD")
|
||||
}
|
||||
}
|
||||
|
||||
// A command that exits non-zero also aborts (the other configured-error shape).
|
||||
func TestResolveDSNCredentialFailsClosedOnExit(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", "false")
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "env-would-be-wrong")
|
||||
_, err := resolveDSNCredential(context.Background(), &configfile.Config{}, baseDSN)
|
||||
if err == nil {
|
||||
t.Fatal("expected an error when the command exits non-zero")
|
||||
}
|
||||
}
|
||||
|
||||
// A getToken/ExecCredential JSON envelope resolves the password end-to-end.
|
||||
func TestResolveDSNCredentialJSONEnvelope(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", `printf '{"access_token":"tok-pw","expires_in":90}'`)
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "")
|
||||
got, err := resolveDSNCredential(context.Background(), &configfile.Config{}, baseDSN)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pw := password(t, got); pw != "tok-pw" {
|
||||
t.Fatalf("password = %q, want tok-pw", pw)
|
||||
}
|
||||
}
|
||||
|
||||
// End-to-end: resolving a password into a userless base DSN must fail loudly (the
|
||||
// grammar would silently drop it), not connect passwordless.
|
||||
func TestResolveDSNCredentialUserlessDSNFailsLoudly(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", "")
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "some-pw")
|
||||
_, err := resolveDSNCredential(context.Background(), &configfile.Config{}, "tcp(127.0.0.1:55441)/")
|
||||
if err == nil {
|
||||
t.Fatal("expected an error placing a password into a userless DSN")
|
||||
}
|
||||
}
|
||||
|
||||
// withDatabase refuses a userless password-bearing DSN (the path that skips the
|
||||
// ladder via an inline ":secret@tcp(host)/" URL) rather than silently dropping it.
|
||||
func TestWithDatabaseRefusesUserlessPassword(t *testing.T) {
|
||||
if _, err := withDatabase(":secret@tcp(127.0.0.1:55441)/", "db"); err == nil {
|
||||
t.Fatal("expected withDatabase to refuse a userless password-bearing DSN")
|
||||
}
|
||||
// A normal user:pass DSN still works.
|
||||
if _, err := withDatabase("bts:bts@tcp(127.0.0.1:55441)/", "db"); err != nil {
|
||||
t.Fatalf("withDatabase rejected a valid DSN: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveDSNCredentialNothingConfigured(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", "")
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "")
|
||||
got, err := resolveDSNCredential(context.Background(), &configfile.Config{}, baseDSN)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != baseDSN {
|
||||
t.Fatalf("dsn = %q, want it unchanged (%q)", got, baseDSN)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveDSNCredentialExistingPasswordWins(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", "printf should-not-run")
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "should-not-run")
|
||||
withPw := "bts:already-here@tcp(127.0.0.1:55441)/"
|
||||
got, err := resolveDSNCredential(context.Background(), &configfile.Config{}, withPw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pw := password(t, got); pw != "already-here" {
|
||||
t.Fatalf("password = %q, want already-here (existing password must win)", pw)
|
||||
}
|
||||
}
|
||||
|
||||
// The credentials file resolves the password when no command or env is set.
|
||||
func TestResolveDSNCredentialFile(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", "")
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "")
|
||||
t.Setenv("BEADS_CREDENTIALS_FILE", writeCredsFile(t, "[127.0.0.1:55441]\npassword=file-pw\n"))
|
||||
got, err := resolveDSNCredential(context.Background(), &configfile.Config{}, baseDSN)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pw := password(t, got); pw != "file-pw" {
|
||||
t.Fatalf("password = %q, want file-pw", pw)
|
||||
}
|
||||
}
|
||||
|
||||
// The env password out-ranks the credentials file.
|
||||
func TestResolveDSNCredentialEnvBeatsFile(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", "")
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "env-pw")
|
||||
t.Setenv("BEADS_CREDENTIALS_FILE", writeCredsFile(t, "[127.0.0.1:55441]\npassword=file-pw\n"))
|
||||
got, err := resolveDSNCredential(context.Background(), &configfile.Config{}, baseDSN)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pw := password(t, got); pw != "env-pw" {
|
||||
t.Fatalf("password = %q, want env-pw (env out-ranks file)", pw)
|
||||
}
|
||||
}
|
||||
|
||||
// A broken command fails closed even when the file could supply a valid password.
|
||||
func TestResolveDSNCredentialBrokenCommandNotRescuedByFile(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", "false")
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "")
|
||||
t.Setenv("BEADS_CREDENTIALS_FILE", writeCredsFile(t, "[127.0.0.1:55441]\npassword=file-pw\n"))
|
||||
if _, err := resolveDSNCredential(context.Background(), &configfile.Config{}, baseDSN); err == nil {
|
||||
t.Fatal("expected fail-closed; the credentials file must not rescue a broken command")
|
||||
}
|
||||
}
|
||||
|
||||
// A unix-socket DSN has no [host:port] endpoint, so the file rung degrades to
|
||||
// not-configured (no crash), leaving the DSN untouched.
|
||||
func TestResolveDSNCredentialFileUnixSocketDegrades(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD_COMMAND", "")
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "")
|
||||
t.Setenv("BEADS_CREDENTIALS_FILE", writeCredsFile(t, "[127.0.0.1:55441]\npassword=file-pw\n"))
|
||||
unixDSN := "bts@unix(/tmp/mysql.sock)/db"
|
||||
got, err := resolveDSNCredential(context.Background(), &configfile.Config{}, unixDSN)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != unixDSN {
|
||||
t.Fatalf("dsn = %q, want it unchanged (unix socket has no file-rung endpoint)", got)
|
||||
}
|
||||
}
|
||||
|
||||
// A set BEADS_MYSQL_CREDENTIAL_COMMAND is a hard error, pre-empting even a
|
||||
// password-bearing DSN and any lower rung.
|
||||
func TestResolveDSNCredentialRejectsReservedIdentityVar(t *testing.T) {
|
||||
t.Setenv("BEADS_MYSQL_CREDENTIAL_COMMAND", "get-credential")
|
||||
t.Setenv("BEADS_MYSQL_PASSWORD", "would-otherwise-work")
|
||||
_, err := resolveDSNCredential(context.Background(), &configfile.Config{}, "bts:has-pw@tcp(127.0.0.1:55441)/")
|
||||
if err == nil || !strings.Contains(err.Error(), "BEADS_MYSQL_CREDENTIAL_COMMAND") {
|
||||
t.Fatalf("expected a reserved-var rejection naming BEADS_MYSQL_CREDENTIAL_COMMAND, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// staticSource is a test Source with a fixed credential, used to exercise placement
|
||||
// and the identity-refusal guard directly.
|
||||
type staticSource struct{ cred creds.Credential }
|
||||
|
||||
func (s staticSource) Name() string { return "test" }
|
||||
func (s staticSource) Resolve(context.Context) (creds.Credential, bool, error) {
|
||||
return s.cred, true, nil
|
||||
}
|
||||
|
||||
func TestResolveDSNCredentialRefusesIdentity(t *testing.T) {
|
||||
src := staticSource{cred: creds.Credential{Value: "an-identity-token", Kind: creds.KindIdentity, Source: "test"}}
|
||||
_, err := resolveDSNWithSources(context.Background(), baseDSN, src)
|
||||
if err == nil || !strings.Contains(err.Error(), "identity") {
|
||||
t.Fatalf("expected an identity-refusal error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// A credential carrying a username (a Vault dynamic user/password pair) places both,
|
||||
// overriding the base DSN's user.
|
||||
func TestResolveDSNCredentialPlacesUsername(t *testing.T) {
|
||||
src := staticSource{cred: creds.Credential{Value: "pw", Username: "dyn", Kind: creds.KindSecret}}
|
||||
got, err := resolveDSNWithSources(context.Background(), baseDSN, src)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u, pw := user(t, got), password(t, got); u != "dyn" || pw != "pw" {
|
||||
t.Fatalf("(user,password) = (%q,%q), want (dyn,pw)", u, pw)
|
||||
}
|
||||
}
|
||||
|
||||
// A dynamic username satisfies the userless-DSN requirement: a bare tcp(host)/ base
|
||||
// plus a user/password pair is a valid configuration.
|
||||
func TestResolveDSNWithSourcesUsernameSatisfiesUserlessBase(t *testing.T) {
|
||||
src := staticSource{cred: creds.Credential{Value: "pw", Username: "dyn", Kind: creds.KindSecret}}
|
||||
got, err := resolveDSNWithSources(context.Background(), "tcp(127.0.0.1:55441)/", src)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u, pw := user(t, got), password(t, got); u != "dyn" || pw != "pw" {
|
||||
t.Fatalf("(user,password) = (%q,%q), want (dyn,pw)", u, pw)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,23 +0,0 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/mysqldialect"
|
||||
)
|
||||
|
||||
// mysqlDialect is the MySQL SQL flavor. bd's canonical SQL is already MySQL, so this
|
||||
// is nearly the identity dialect: mysqldialect.Open wraps go-sql-driver with a single
|
||||
// rewrite (the self-referential is_blocked recompute UPDATE that MySQL 8 rejects) and
|
||||
// passes everything else through verbatim. The dsn already targets the workspace
|
||||
// database.
|
||||
type mysqlDialect struct {
|
||||
dsn string
|
||||
}
|
||||
|
||||
func (d mysqlDialect) Name() string { return "mysql" }
|
||||
|
||||
func (d mysqlDialect) Open(_ context.Context) (*sql.DB, error) {
|
||||
return mysqldialect.Open(d.dsn)
|
||||
}
|
||||
|
|
@ -1,53 +0,0 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
gomysql "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// RedactPassword returns dsn with the password removed, or an error if it cannot be
|
||||
// stripped. It uses go-sql-driver's own parser (never hand-rolled string surgery) and
|
||||
// verifies with a re-parse that no password survives, failing closed. bd init persists
|
||||
// the returned string to metadata.json; the password is re-supplied at open time by
|
||||
// the credential ladder (see resolveDSNCredential in credential.go).
|
||||
func RedactPassword(dsn string) (string, error) {
|
||||
cfg, err := gomysql.ParseDSN(dsn)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("parse DSN: %w", err)
|
||||
}
|
||||
cfg.Passwd = ""
|
||||
stripped := cfg.FormatDSN()
|
||||
if again, err := gomysql.ParseDSN(stripped); err != nil || again.Passwd != "" {
|
||||
return "", fmt.Errorf("refusing to persist a connection string that still carries a password; supply it via BEADS_MYSQL_PASSWORD instead of embedding it in --mysql-url")
|
||||
}
|
||||
return stripped, nil
|
||||
}
|
||||
|
||||
// withDatabase returns dsn with its default database set to database (or cleared when
|
||||
// database is ""). Used to derive a server connection (for CREATE DATABASE) and the
|
||||
// per-workspace connection from one base DSN. It also forces parseTime and UTC so
|
||||
// datetime columns round-trip identically to the Dolt reference.
|
||||
func withDatabase(dsn, database string) (string, error) {
|
||||
cfg, err := gomysql.ParseDSN(dsn)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// The go-sql-driver grammar cannot carry a password without a username —
|
||||
// FormatDSN would silently drop it. Refuse loudly rather than reformat this DSN
|
||||
// into a passwordless one and connect (the same invariant mysqldialect.WithPassword
|
||||
// enforces; this covers the path where a userless password-bearing DSN reaches us
|
||||
// directly, e.g. an inline ":secret@tcp(host)/" URL that skips the credential ladder).
|
||||
if cfg.User == "" && cfg.Passwd != "" {
|
||||
return "", fmt.Errorf("connection string carries a password but no username; the MySQL DSN grammar cannot carry a password without a user (it would be silently dropped) — add a user to the DSN")
|
||||
}
|
||||
cfg.DBName = database
|
||||
cfg.ParseTime = true
|
||||
if cfg.Params == nil {
|
||||
cfg.Params = map[string]string{}
|
||||
}
|
||||
// Pin the session time zone to UTC so CURRENT_TIMESTAMP defaults and reads match
|
||||
// the shared layer's UTC assumption (and the Dolt reference).
|
||||
cfg.Params["time_zone"] = "'+00:00'"
|
||||
return cfg.FormatDSN(), nil
|
||||
}
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/steveyegge/beads/internal/configfile"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
)
|
||||
|
||||
// NewFromConfig opens the MySQL backend for a workspace. It reads the base server DSN
|
||||
// and per-workspace database from .beads/metadata.json, resolves the password through
|
||||
// the credential ladder (command > env > credentials file, fail-closed) and places it into the DSN —
|
||||
// never persisting it — then provisions and returns the store. It is the factory arm
|
||||
// cmd/bd dispatches to when metadata.json has backend="mysql".
|
||||
func NewFromConfig(ctx context.Context, beadsDir string) (storage.DoltStorage, error) {
|
||||
cfg, err := configfile.Load(beadsDir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mysql: load config: %w", err)
|
||||
}
|
||||
dsn := cfg.GetMySQLDSN()
|
||||
if dsn == "" {
|
||||
return nil, fmt.Errorf("mysql: no DSN (set mysql_dsn in metadata.json, or BEADS_MYSQL_URL)")
|
||||
}
|
||||
dsn, err = resolveDSNCredential(ctx, cfg, dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mysql: resolve credential: %w", err)
|
||||
}
|
||||
database := cfg.GetMySQLDatabase()
|
||||
if database == "" {
|
||||
return nil, fmt.Errorf("mysql: no database (set mysql_database in metadata.json)")
|
||||
}
|
||||
return Provision(ctx, dsn, database)
|
||||
}
|
||||
|
||||
// Provision opens the MySQL backend from an explicit server DSN and workspace
|
||||
// database: it creates the database if absent and applies the schema (InitSchema —
|
||||
// idempotent, config seeds on first provision only), then returns the store over the
|
||||
// per-workspace connection. baseDSN may carry a password; it is used only to connect
|
||||
// and is never persisted. bd init calls this directly; NewFromConfig calls it after
|
||||
// resolving DSN/database from metadata.
|
||||
func Provision(ctx context.Context, baseDSN, database string) (storage.DoltStorage, error) {
|
||||
if baseDSN == "" {
|
||||
return nil, fmt.Errorf("mysql: empty DSN")
|
||||
}
|
||||
if database == "" {
|
||||
return nil, fmt.Errorf("mysql: empty database")
|
||||
}
|
||||
if err := InitSchema(ctx, baseDSN, database); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
wsDSN, err := withDatabase(baseDSN, database)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mysql: parse DSN: %w", err)
|
||||
}
|
||||
return New(ctx, Config{DSN: wsDSN})
|
||||
}
|
||||
|
|
@ -1,117 +0,0 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
gomysql "github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// placeholderPW is an obvious non-secret stand-in used wherever a password appears in
|
||||
// these tests, so nothing here can be mistaken for a leaked real credential.
|
||||
const placeholderPW = "s3cr3t-PLACEHOLDER"
|
||||
|
||||
// TestRedactPassword locks the security invariant for mysql.RedactPassword across every
|
||||
// secret-bearing DSN shape the go-sql-driver grammar accepts. For each case it asserts
|
||||
// BOTH:
|
||||
//
|
||||
// (a) the persisted string, re-parsed by go-sql-driver — the SAME parser that opens
|
||||
// the connection — carries NO password (prove the secret is gone); and
|
||||
// (b) every non-secret component (user, host:port, network, database, params, and the
|
||||
// parseTime flag) survives redaction.
|
||||
//
|
||||
// A no-password DSN must additionally round-trip byte-for-byte unchanged.
|
||||
func TestRedactPassword(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
dsn string
|
||||
wantUser string
|
||||
wantAddr string
|
||||
wantNet string
|
||||
wantDB string
|
||||
wantParseTime bool
|
||||
wantParams map[string]string
|
||||
roundTripSame bool
|
||||
}{
|
||||
{
|
||||
name: "password present",
|
||||
dsn: "bts:" + placeholderPW + "@tcp(127.0.0.1:3306)/beadsdb",
|
||||
wantUser: "bts", wantAddr: "127.0.0.1:3306", wantNet: "tcp", wantDB: "beadsdb",
|
||||
},
|
||||
{
|
||||
// go-sql-driver splits the userinfo password on the LAST '@', so a password
|
||||
// full of ':' '/' '#' '@' must still be stripped cleanly.
|
||||
name: "password with special chars",
|
||||
dsn: "bts:p@ss:w/rd#-" + placeholderPW + "@tcp(127.0.0.1:3306)/beadsdb",
|
||||
wantUser: "bts", wantAddr: "127.0.0.1:3306", wantNet: "tcp", wantDB: "beadsdb",
|
||||
},
|
||||
{
|
||||
name: "password present, params + parseTime preserved",
|
||||
dsn: "bts:" + placeholderPW + "@tcp(db.corp:3307)/beadsdb?parseTime=true&myapp=beads",
|
||||
wantUser: "bts", wantAddr: "db.corp:3307", wantNet: "tcp", wantDB: "beadsdb",
|
||||
wantParseTime: true, wantParams: map[string]string{"myapp": "beads"},
|
||||
},
|
||||
{
|
||||
// The go-sql-driver grammar cannot carry a password without a username;
|
||||
// FormatDSN drops the whole user:pass@ block, so redaction still removes the
|
||||
// secret. (The loud refusal for this shape lives in withDatabase/WithPassword,
|
||||
// not RedactPassword — here the invariant is only that the password is gone.)
|
||||
name: "password but no username",
|
||||
dsn: ":" + placeholderPW + "@tcp(127.0.0.1:3306)/beadsdb",
|
||||
wantUser: "", wantAddr: "127.0.0.1:3306", wantNet: "tcp", wantDB: "beadsdb",
|
||||
},
|
||||
{
|
||||
// A canonical, param-free DSN must survive redaction byte-for-byte
|
||||
// (go-sql-driver reorders query params on reformat, so the round-trip
|
||||
// invariant is asserted only against a DSN already in canonical form).
|
||||
name: "no password round-trips unchanged",
|
||||
dsn: "bts@tcp(127.0.0.1:3306)/beadsdb",
|
||||
wantUser: "bts", wantAddr: "127.0.0.1:3306", wantNet: "tcp", wantDB: "beadsdb",
|
||||
roundTripSame: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := RedactPassword(tc.dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("RedactPassword(%q) errored: %v", tc.dsn, err)
|
||||
}
|
||||
|
||||
// (a) The authoritative security check: the same parser go-sql-driver uses to
|
||||
// open the connection must find no password in the persisted string.
|
||||
if pw := password(t, got); pw != "" {
|
||||
t.Fatalf("PASSWORD LEAK: RedactPassword(%q) = %q still carries password %q", tc.dsn, got, pw)
|
||||
}
|
||||
|
||||
// (b) Non-secret components must survive.
|
||||
cfg, err := gomysql.ParseDSN(got)
|
||||
if err != nil {
|
||||
t.Fatalf("redacted DSN no longer parses: %q: %v", got, err)
|
||||
}
|
||||
if cfg.User != tc.wantUser {
|
||||
t.Errorf("user = %q, want %q", cfg.User, tc.wantUser)
|
||||
}
|
||||
if cfg.Addr != tc.wantAddr {
|
||||
t.Errorf("addr = %q, want %q", cfg.Addr, tc.wantAddr)
|
||||
}
|
||||
if cfg.Net != tc.wantNet {
|
||||
t.Errorf("net = %q, want %q", cfg.Net, tc.wantNet)
|
||||
}
|
||||
if cfg.DBName != tc.wantDB {
|
||||
t.Errorf("dbname = %q, want %q", cfg.DBName, tc.wantDB)
|
||||
}
|
||||
if cfg.ParseTime != tc.wantParseTime {
|
||||
t.Errorf("parseTime = %v, want %v (must be preserved)", cfg.ParseTime, tc.wantParseTime)
|
||||
}
|
||||
for k, want := range tc.wantParams {
|
||||
if got := cfg.Params[k]; got != want {
|
||||
t.Errorf("param %q = %q, want %q (must be preserved)", k, got, want)
|
||||
}
|
||||
}
|
||||
|
||||
if tc.roundTripSame && got != tc.dsn {
|
||||
t.Errorf("no-password DSN must round-trip unchanged:\n in = %q\n out = %q", tc.dsn, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -1,162 +0,0 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ddl is the MySQL backend schema, embedded from schema.sql. It is the Dolt
|
||||
// `SHOW CREATE TABLE` output at HEAD (all migrations applied) with the handful of
|
||||
// Dolt-vs-MySQL-8 fixes required by stock MySQL: TEXT/JSON literal defaults →
|
||||
// expression form `DEFAULT (”)`, the overlong `spec_id` index → a 191-char prefix
|
||||
// (InnoDB's 3072-byte key cap), and the one-target CHECK constraints dropped (MySQL 8
|
||||
// forbids a column in both a CHECK and an FK cascade; issueops enforces the invariant
|
||||
// in code). No data-plane translation is needed — bd's canonical SQL is MySQL.
|
||||
//
|
||||
//go:embed schema.sql
|
||||
var ddl string
|
||||
|
||||
const (
|
||||
schemaVersion = "1"
|
||||
schemaVersionKey = "mysql_schema_version"
|
||||
)
|
||||
|
||||
// identRe restricts a database name to a plain identifier so it can be safely
|
||||
// interpolated into CREATE DATABASE (which cannot take a placeholder).
|
||||
var identRe = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`)
|
||||
|
||||
// InitSchema creates the workspace database if absent (via a server connection with
|
||||
// no default database), applies the DDL to it, seeds the default config rows on first
|
||||
// provision, and stamps the schema version. baseDSN is the server DSN; database is the
|
||||
// per-workspace database (MySQL's unit of isolation, analogous to a Postgres schema).
|
||||
func InitSchema(ctx context.Context, baseDSN, database string) error {
|
||||
if !identRe.MatchString(database) {
|
||||
return fmt.Errorf("mysql: invalid database name %q", database)
|
||||
}
|
||||
serverDSN, err := withDatabase(baseDSN, "")
|
||||
if err != nil {
|
||||
return fmt.Errorf("mysql: parse DSN: %w", err)
|
||||
}
|
||||
srv, err := sql.Open("mysql", serverDSN)
|
||||
if err != nil {
|
||||
return fmt.Errorf("mysql: open server: %w", err)
|
||||
}
|
||||
if _, err := srv.ExecContext(ctx, "CREATE DATABASE IF NOT EXISTS `"+database+"` CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_bin"); err != nil {
|
||||
_ = srv.Close()
|
||||
return fmt.Errorf("mysql: create database %q: %w", database, err)
|
||||
}
|
||||
_ = srv.Close()
|
||||
|
||||
wsDSN, err := withDatabase(baseDSN, database)
|
||||
if err != nil {
|
||||
return fmt.Errorf("mysql: parse DSN: %w", err)
|
||||
}
|
||||
db, err := sql.Open("mysql", wsDSN)
|
||||
if err != nil {
|
||||
return fmt.Errorf("mysql: open workspace db: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
for _, stmt := range splitStatements(ddl) {
|
||||
if _, err := db.ExecContext(ctx, stmt); err != nil {
|
||||
return fmt.Errorf("mysql: exec DDL statement: %w\n-- statement:\n%s", err, stmt)
|
||||
}
|
||||
}
|
||||
fresh, err := stampSchemaVersion(ctx, db)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if fresh {
|
||||
if err := seedDefaultConfig(ctx, db); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// defaultConfigSeeds mirrors migration 0016 exactly — the config rows a fresh Dolt
|
||||
// workspace materializes on init. Seeded only on first provision (like the one-shot
|
||||
// migration), so a `config unset` is never resurrected on the next open.
|
||||
var defaultConfigSeeds = [][2]string{
|
||||
{"compaction_enabled", "false"},
|
||||
{"compact_tier1_days", "30"},
|
||||
{"compact_tier1_dep_levels", "2"},
|
||||
{"compact_tier2_days", "90"},
|
||||
{"compact_tier2_dep_levels", "5"},
|
||||
{"compact_tier2_commits", "100"},
|
||||
{"compact_batch_size", "50"},
|
||||
{"compact_parallel_workers", "5"},
|
||||
{"auto_compact_enabled", "false"},
|
||||
}
|
||||
|
||||
func seedDefaultConfig(ctx context.Context, db *sql.DB) error {
|
||||
for _, kv := range defaultConfigSeeds {
|
||||
if _, err := db.ExecContext(ctx,
|
||||
"INSERT IGNORE INTO config (`key`, `value`) VALUES (?, ?)", kv[0], kv[1]); err != nil {
|
||||
return fmt.Errorf("mysql: seed default config %q: %w", kv[0], err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// stampSchemaVersion records schemaVersion in the metadata table on first provision
|
||||
// and refuses to open a workspace written by a binary with a different schema version
|
||||
// (the proof-wedge has no migrator). Returns fresh=true only on the first stamp.
|
||||
func stampSchemaVersion(ctx context.Context, db *sql.DB) (fresh bool, err error) {
|
||||
var stored string
|
||||
err = db.QueryRowContext(ctx, "SELECT `value` FROM metadata WHERE `key` = ?", schemaVersionKey).Scan(&stored)
|
||||
switch {
|
||||
case errors.Is(err, sql.ErrNoRows):
|
||||
// Provision runs on every open; concurrent first opens of the same
|
||||
// workspace database can both reach here. INSERT IGNORE lets both
|
||||
// succeed; only the opener that inserted the row (RowsAffected==1) is
|
||||
// fresh and seeds defaults (seedDefaultConfig is already INSERT IGNORE).
|
||||
res, ierr := db.ExecContext(ctx, "INSERT IGNORE INTO metadata (`key`, `value`) VALUES (?, ?)", schemaVersionKey, schemaVersion)
|
||||
if ierr != nil {
|
||||
return false, fmt.Errorf("mysql: stamp schema version: %w", ierr)
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 1 {
|
||||
return true, nil
|
||||
}
|
||||
// Lost the race: re-read what the winner stored and version-check it.
|
||||
if rerr := db.QueryRowContext(ctx, "SELECT `value` FROM metadata WHERE `key` = ?", schemaVersionKey).Scan(&stored); rerr != nil {
|
||||
return false, fmt.Errorf("mysql: read schema version after conflict: %w", rerr)
|
||||
}
|
||||
if stored != schemaVersion {
|
||||
return false, fmt.Errorf("mysql: workspace schema version %s, this binary requires %s — no migrator in the proof-wedge, recreate the workspace or use a matching binary", stored, schemaVersion)
|
||||
}
|
||||
return false, nil
|
||||
case err != nil:
|
||||
return false, fmt.Errorf("mysql: read schema version: %w", err)
|
||||
case stored != schemaVersion:
|
||||
return false, fmt.Errorf("mysql: workspace schema version %s, this binary requires %s — no migrator in the proof-wedge, recreate the workspace or use a matching binary", stored, schemaVersion)
|
||||
default:
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
||||
// splitStatements splits the DDL on ';' at statement boundaries. The MySQL schema has
|
||||
// no procedural bodies (triggers/procedures) or embedded ';' — the CHECK constraints
|
||||
// that would complicate this were removed — so a plain split suffices. Blank fragments
|
||||
// (and SQL line comments) are dropped.
|
||||
func splitStatements(ddl string) []string {
|
||||
var stmts []string
|
||||
for _, raw := range strings.Split(ddl, ";") {
|
||||
var b strings.Builder
|
||||
for _, line := range strings.Split(raw, "\n") {
|
||||
if strings.HasPrefix(strings.TrimSpace(line), "--") {
|
||||
continue
|
||||
}
|
||||
b.WriteString(line)
|
||||
b.WriteString("\n")
|
||||
}
|
||||
if s := strings.TrimSpace(b.String()); s != "" {
|
||||
stmts = append(stmts, s)
|
||||
}
|
||||
}
|
||||
return stmts
|
||||
}
|
||||
|
|
@ -1,344 +0,0 @@
|
|||
CREATE TABLE IF NOT EXISTS `issues` (
|
||||
`id` varchar(255) NOT NULL,
|
||||
`content_hash` varchar(64),
|
||||
`title` varchar(500) NOT NULL,
|
||||
`description` longtext NOT NULL,
|
||||
`design` longtext NOT NULL,
|
||||
`acceptance_criteria` longtext NOT NULL,
|
||||
`notes` longtext NOT NULL,
|
||||
`status` varchar(32) NOT NULL DEFAULT 'open',
|
||||
`priority` int NOT NULL DEFAULT '2',
|
||||
`issue_type` varchar(32) NOT NULL DEFAULT 'task',
|
||||
`assignee` varchar(255),
|
||||
`estimated_minutes` int,
|
||||
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`created_by` varchar(255) DEFAULT '',
|
||||
`owner` varchar(255) DEFAULT '',
|
||||
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`closed_at` datetime,
|
||||
`closed_by_session` varchar(255) DEFAULT '',
|
||||
`external_ref` varchar(255),
|
||||
`spec_id` varchar(1024),
|
||||
`compaction_level` int DEFAULT '0',
|
||||
`compacted_at` datetime,
|
||||
`compacted_at_commit` varchar(64),
|
||||
`original_size` int,
|
||||
`sender` varchar(255) DEFAULT '',
|
||||
`ephemeral` tinyint(1) DEFAULT '0',
|
||||
`wisp_type` varchar(32) DEFAULT '',
|
||||
`pinned` tinyint(1) DEFAULT '0',
|
||||
`is_template` tinyint(1) DEFAULT '0',
|
||||
`mol_type` varchar(32) DEFAULT '',
|
||||
`work_type` varchar(32) DEFAULT 'mutex',
|
||||
`source_system` varchar(255) DEFAULT '',
|
||||
`metadata` json DEFAULT (json_object()),
|
||||
`source_repo` varchar(512) DEFAULT '',
|
||||
`close_reason` longtext DEFAULT (''),
|
||||
`event_kind` varchar(32) DEFAULT '',
|
||||
`actor` varchar(255) DEFAULT '',
|
||||
`target` varchar(255) DEFAULT '',
|
||||
`payload` text DEFAULT (''),
|
||||
`await_type` varchar(32) DEFAULT '',
|
||||
`await_id` varchar(255) DEFAULT '',
|
||||
`timeout_ns` bigint DEFAULT '0',
|
||||
`waiters` text DEFAULT (''),
|
||||
`hook_bead` varchar(255) DEFAULT '',
|
||||
`role_bead` varchar(255) DEFAULT '',
|
||||
`agent_state` varchar(32) DEFAULT '',
|
||||
`last_activity` datetime,
|
||||
`role_type` varchar(32) DEFAULT '',
|
||||
`rig` varchar(255) DEFAULT '',
|
||||
`due_at` datetime,
|
||||
`defer_until` datetime,
|
||||
`no_history` tinyint(1) DEFAULT '0',
|
||||
`started_at` datetime,
|
||||
`is_blocked` tinyint(1) NOT NULL DEFAULT '0',
|
||||
`row_lock` bigint NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_issues_assignee` (`assignee`),
|
||||
KEY `idx_issues_created_at` (`created_at`),
|
||||
KEY `idx_issues_defer_until` (`defer_until`),
|
||||
KEY `idx_issues_external_ref` (`external_ref`),
|
||||
KEY `idx_issues_is_blocked` (`is_blocked`,`status`),
|
||||
KEY `idx_issues_issue_type` (`issue_type`),
|
||||
KEY `idx_issues_priority` (`priority`),
|
||||
KEY `idx_issues_spec_id` (`spec_id`(191)),
|
||||
KEY `idx_issues_status_updated_at` (`status`,`updated_at`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
-- Ephemeral claim leases (bd-lrgn1): node-local, never exported by backup.
|
||||
-- One row per live claim. See issueops.UpsertLeaseInTx for the invariant.
|
||||
CREATE TABLE IF NOT EXISTS `leases` (
|
||||
`issue_id` varchar(255) NOT NULL,
|
||||
`holder` varchar(255) NOT NULL,
|
||||
`granted_at` datetime NOT NULL,
|
||||
`lease_expires_at` datetime NOT NULL,
|
||||
`heartbeat_at` datetime NOT NULL,
|
||||
PRIMARY KEY (`issue_id`),
|
||||
KEY `idx_leases_expires` (`lease_expires_at`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `wisps` (
|
||||
`id` varchar(255) NOT NULL,
|
||||
`content_hash` varchar(64),
|
||||
`title` varchar(500) NOT NULL,
|
||||
`description` longtext NOT NULL DEFAULT (''),
|
||||
`design` longtext NOT NULL DEFAULT (''),
|
||||
`acceptance_criteria` longtext NOT NULL DEFAULT (''),
|
||||
`notes` longtext NOT NULL DEFAULT (''),
|
||||
`status` varchar(32) NOT NULL DEFAULT 'open',
|
||||
`priority` int NOT NULL DEFAULT '2',
|
||||
`issue_type` varchar(32) NOT NULL DEFAULT 'task',
|
||||
`assignee` varchar(255),
|
||||
`estimated_minutes` int,
|
||||
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`created_by` varchar(255) DEFAULT '',
|
||||
`owner` varchar(255) DEFAULT '',
|
||||
`updated_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`closed_at` datetime,
|
||||
`closed_by_session` varchar(255) DEFAULT '',
|
||||
`external_ref` varchar(255),
|
||||
`spec_id` varchar(1024),
|
||||
`compaction_level` int DEFAULT '0',
|
||||
`compacted_at` datetime,
|
||||
`compacted_at_commit` varchar(64),
|
||||
`original_size` int,
|
||||
`sender` varchar(255) DEFAULT '',
|
||||
`ephemeral` tinyint(1) DEFAULT '0',
|
||||
`wisp_type` varchar(32) DEFAULT '',
|
||||
`pinned` tinyint(1) DEFAULT '0',
|
||||
`is_template` tinyint(1) DEFAULT '0',
|
||||
`mol_type` varchar(32) DEFAULT '',
|
||||
`work_type` varchar(32) DEFAULT 'mutex',
|
||||
`source_system` varchar(255) DEFAULT '',
|
||||
`metadata` json DEFAULT (json_object()),
|
||||
`source_repo` varchar(512) DEFAULT '',
|
||||
`close_reason` longtext DEFAULT (''),
|
||||
`event_kind` varchar(32) DEFAULT '',
|
||||
`actor` varchar(255) DEFAULT '',
|
||||
`target` varchar(255) DEFAULT '',
|
||||
`payload` text DEFAULT (''),
|
||||
`await_type` varchar(32) DEFAULT '',
|
||||
`await_id` varchar(255) DEFAULT '',
|
||||
`timeout_ns` bigint DEFAULT '0',
|
||||
`waiters` text DEFAULT (''),
|
||||
`hook_bead` varchar(255) DEFAULT '',
|
||||
`role_bead` varchar(255) DEFAULT '',
|
||||
`agent_state` varchar(32) DEFAULT '',
|
||||
`last_activity` datetime,
|
||||
`role_type` varchar(32) DEFAULT '',
|
||||
`rig` varchar(255) DEFAULT '',
|
||||
`due_at` datetime,
|
||||
`defer_until` datetime,
|
||||
`no_history` tinyint(1) DEFAULT '0',
|
||||
`started_at` datetime,
|
||||
`is_blocked` tinyint(1) NOT NULL DEFAULT '0',
|
||||
`row_lock` bigint NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_wisps_assignee` (`assignee`),
|
||||
KEY `idx_wisps_created_at` (`created_at`),
|
||||
KEY `idx_wisps_external_ref` (`external_ref`),
|
||||
KEY `idx_wisps_is_blocked` (`is_blocked`,`status`),
|
||||
KEY `idx_wisps_issue_type` (`issue_type`),
|
||||
KEY `idx_wisps_priority` (`priority`),
|
||||
KEY `idx_wisps_spec_id` (`spec_id`(191)),
|
||||
KEY `idx_wisps_status` (`status`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `labels` (
|
||||
`issue_id` varchar(255) NOT NULL,
|
||||
`label` varchar(255) NOT NULL,
|
||||
PRIMARY KEY (`issue_id`,`label`),
|
||||
KEY `idx_labels_label` (`label`),
|
||||
CONSTRAINT `fk_labels_issue` FOREIGN KEY (`issue_id`) REFERENCES `issues` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `wisp_labels` (
|
||||
`issue_id` varchar(255) NOT NULL,
|
||||
`label` varchar(255) NOT NULL,
|
||||
PRIMARY KEY (`issue_id`,`label`),
|
||||
KEY `idx_wisp_labels_label` (`label`),
|
||||
CONSTRAINT `fk_wisp_labels_issue` FOREIGN KEY (`issue_id`) REFERENCES `wisps` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `dependencies` (
|
||||
`id` char(36) NOT NULL,
|
||||
`issue_id` varchar(255) NOT NULL,
|
||||
`type` varchar(32) NOT NULL DEFAULT 'blocks',
|
||||
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`created_by` varchar(255) NOT NULL,
|
||||
`metadata` json DEFAULT (json_object()),
|
||||
`thread_id` varchar(255) DEFAULT '',
|
||||
`depends_on_issue_id` varchar(255),
|
||||
`depends_on_wisp_id` varchar(255),
|
||||
`depends_on_external` varchar(255),
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_dep_external_target` (`depends_on_external`),
|
||||
KEY `idx_dep_issue_target` (`depends_on_issue_id`),
|
||||
KEY `idx_dep_type_external` (`type`,`depends_on_external`),
|
||||
KEY `idx_dep_type_issue` (`type`,`depends_on_issue_id`),
|
||||
KEY `idx_dep_type_wisp` (`type`,`depends_on_wisp_id`),
|
||||
KEY `idx_dep_wisp_target` (`depends_on_wisp_id`),
|
||||
KEY `idx_dependencies_issue` (`issue_id`),
|
||||
KEY `idx_dependencies_thread` (`thread_id`),
|
||||
UNIQUE KEY `uk_dep_external_target` (`issue_id`,`depends_on_external`),
|
||||
UNIQUE KEY `uk_dep_issue_target` (`issue_id`,`depends_on_issue_id`),
|
||||
UNIQUE KEY `uk_dep_wisp_target` (`issue_id`,`depends_on_wisp_id`),
|
||||
CONSTRAINT `fk_dep_issue` FOREIGN KEY (`issue_id`) REFERENCES `issues` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
CONSTRAINT `fk_dep_issue_target` FOREIGN KEY (`depends_on_issue_id`) REFERENCES `issues` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `wisp_dependencies` (
|
||||
`id` char(36) NOT NULL,
|
||||
`issue_id` varchar(255) NOT NULL,
|
||||
`depends_on_issue_id` varchar(255),
|
||||
`depends_on_wisp_id` varchar(255),
|
||||
`depends_on_external` varchar(255),
|
||||
`type` varchar(32) NOT NULL DEFAULT 'blocks',
|
||||
`created_at` datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
`created_by` varchar(255) DEFAULT '',
|
||||
`metadata` json DEFAULT (json_object()),
|
||||
`thread_id` varchar(255) DEFAULT '',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `fk_wisp_dep_issue_target` (`depends_on_issue_id`),
|
||||
KEY `fk_wisp_dep_wisp_target` (`depends_on_wisp_id`),
|
||||
KEY `idx_wisp_dep_type` (`type`),
|
||||
KEY `idx_wisp_dep_type_external` (`type`,`depends_on_external`),
|
||||
KEY `idx_wisp_dep_type_issue` (`type`,`depends_on_issue_id`),
|
||||
KEY `idx_wisp_dep_type_wisp` (`type`,`depends_on_wisp_id`),
|
||||
UNIQUE KEY `uk_wisp_dep_external_target` (`issue_id`,`depends_on_external`),
|
||||
UNIQUE KEY `uk_wisp_dep_issue_target` (`issue_id`,`depends_on_issue_id`),
|
||||
UNIQUE KEY `uk_wisp_dep_wisp_target` (`issue_id`,`depends_on_wisp_id`),
|
||||
CONSTRAINT `fk_wisp_dep_issue` FOREIGN KEY (`issue_id`) REFERENCES `wisps` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
CONSTRAINT `fk_wisp_dep_issue_target` FOREIGN KEY (`depends_on_issue_id`) REFERENCES `issues` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
|
||||
CONSTRAINT `fk_wisp_dep_wisp_target` FOREIGN KEY (`depends_on_wisp_id`) REFERENCES `wisps` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `comments` (
|
||||
`id` char(36) NOT NULL,
|
||||
`issue_id` varchar(255) NOT NULL,
|
||||
`author` varchar(255) NOT NULL,
|
||||
`text` longtext NOT NULL,
|
||||
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_comments_created_at` (`created_at`),
|
||||
KEY `idx_comments_issue` (`issue_id`),
|
||||
CONSTRAINT `fk_comments_issue` FOREIGN KEY (`issue_id`) REFERENCES `issues` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `wisp_comments` (
|
||||
`id` char(36) NOT NULL,
|
||||
`issue_id` varchar(255) NOT NULL,
|
||||
`author` varchar(255) DEFAULT '',
|
||||
`text` text NOT NULL,
|
||||
`created_at` datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_wisp_comments_issue` (`issue_id`),
|
||||
CONSTRAINT `fk_wisp_comments_issue` FOREIGN KEY (`issue_id`) REFERENCES `wisps` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `events` (
|
||||
`id` char(36) NOT NULL,
|
||||
`issue_id` varchar(255) NOT NULL,
|
||||
`event_type` varchar(32) NOT NULL,
|
||||
`actor` varchar(255) NOT NULL,
|
||||
`old_value` longtext,
|
||||
`new_value` longtext,
|
||||
`comment` text,
|
||||
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_events_created_at` (`created_at`),
|
||||
KEY `idx_events_issue` (`issue_id`),
|
||||
CONSTRAINT `fk_events_issue` FOREIGN KEY (`issue_id`) REFERENCES `issues` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `wisp_events` (
|
||||
`id` char(36) NOT NULL,
|
||||
`issue_id` varchar(255) NOT NULL,
|
||||
`event_type` varchar(32) NOT NULL,
|
||||
`actor` varchar(255) DEFAULT '',
|
||||
`old_value` longtext DEFAULT (''),
|
||||
`new_value` longtext DEFAULT (''),
|
||||
`comment` text DEFAULT (''),
|
||||
`created_at` datetime DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_wisp_events_created_at` (`created_at`),
|
||||
KEY `idx_wisp_events_issue` (`issue_id`),
|
||||
CONSTRAINT `fk_wisp_events_issue` FOREIGN KEY (`issue_id`) REFERENCES `wisps` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `config` (
|
||||
`key` varchar(255) NOT NULL,
|
||||
`value` text NOT NULL,
|
||||
PRIMARY KEY (`key`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `metadata` (
|
||||
`key` varchar(255) NOT NULL,
|
||||
`value` text NOT NULL,
|
||||
PRIMARY KEY (`key`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `local_metadata` (
|
||||
`key` varchar(255) NOT NULL,
|
||||
`value` text NOT NULL DEFAULT (''),
|
||||
PRIMARY KEY (`key`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `issue_counter` (
|
||||
`prefix` varchar(255) NOT NULL,
|
||||
`last_id` int NOT NULL DEFAULT '0',
|
||||
PRIMARY KEY (`prefix`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `child_counters` (
|
||||
`parent_id` varchar(255) NOT NULL,
|
||||
`last_child` int NOT NULL DEFAULT '0',
|
||||
PRIMARY KEY (`parent_id`),
|
||||
CONSTRAINT `fk_counter_parent` FOREIGN KEY (`parent_id`) REFERENCES `issues` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `wisp_child_counters` (
|
||||
`parent_id` varchar(255) NOT NULL,
|
||||
`last_child` int NOT NULL DEFAULT '0',
|
||||
PRIMARY KEY (`parent_id`),
|
||||
CONSTRAINT `fk_wisp_child_counters_parent` FOREIGN KEY (`parent_id`) REFERENCES `wisps` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `custom_statuses` (
|
||||
`name` varchar(64) NOT NULL,
|
||||
`category` varchar(32) NOT NULL DEFAULT 'unspecified',
|
||||
PRIMARY KEY (`name`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `custom_types` (
|
||||
`name` varchar(64) NOT NULL,
|
||||
PRIMARY KEY (`name`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `repo_mtimes` (
|
||||
`repo_path` varchar(512) NOT NULL,
|
||||
`jsonl_path` varchar(512) NOT NULL,
|
||||
`mtime_ns` bigint NOT NULL,
|
||||
`last_checked` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`repo_path`),
|
||||
KEY `idx_repo_mtimes_checked` (`last_checked`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_bin;
|
||||
|
|
@ -1,54 +0,0 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// TestSeedOnlyOnFirstProvision guards the same regression the Postgres backend has:
|
||||
// InitSchema runs on every open, so seeding default config unconditionally would
|
||||
// resurrect a key the user `config unset`. Seeding must be gated on first provision.
|
||||
func TestSeedOnlyOnFirstProvision(t *testing.T) {
|
||||
url := os.Getenv("BEADS_MYSQL_TEST_URL")
|
||||
if url == "" {
|
||||
t.Skip("BEADS_MYSQL_TEST_URL not set")
|
||||
}
|
||||
database := fmt.Sprintf("seedonce_%d", time.Now().UnixNano())
|
||||
ctx := context.Background()
|
||||
t.Cleanup(func() {
|
||||
if serverDSN, e := withDatabase(url, ""); e == nil {
|
||||
if srv, e2 := sql.Open("mysql", serverDSN); e2 == nil {
|
||||
_, _ = srv.ExecContext(context.Background(), "DROP DATABASE IF EXISTS `"+database+"`")
|
||||
_ = srv.Close()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
st, err := Provision(ctx, url, database)
|
||||
if err != nil {
|
||||
t.Fatalf("Provision: %v", err)
|
||||
}
|
||||
if v, _ := st.GetConfig(ctx, "auto_compact_enabled"); v != "false" {
|
||||
t.Fatalf("first provision: want seeded auto_compact_enabled=false, got %q", v)
|
||||
}
|
||||
if err := st.DeleteConfig(ctx, "auto_compact_enabled"); err != nil {
|
||||
t.Fatalf("DeleteConfig: %v", err)
|
||||
}
|
||||
if v, _ := st.GetConfig(ctx, "auto_compact_enabled"); v != "" {
|
||||
t.Fatalf("after unset: want empty, got %q", v)
|
||||
}
|
||||
_ = st.Close()
|
||||
|
||||
st2, err := Provision(ctx, url, database)
|
||||
if err != nil {
|
||||
t.Fatalf("re-Provision: %v", err)
|
||||
}
|
||||
defer st2.Close()
|
||||
if v, _ := st2.GetConfig(ctx, "auto_compact_enabled"); v != "" {
|
||||
t.Fatalf("REGRESSION: re-provision resurrected an unset key: auto_compact_enabled=%q (want empty)", v)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,69 +0,0 @@
|
|||
// Package mysql is the MySQL backend for bd: the shared sqlkit.Store bundled with
|
||||
// the identity MySQL dialect and a no-op readiness strategy, plus the non-Dolt
|
||||
// markers. bd's canonical SQL is MySQL-dialect, so unlike Postgres there is no
|
||||
// translating driver — the only backend-specific work is a curated MySQL-8 schema
|
||||
// (Dolt is more permissive than stock MySQL 8 at the DDL level).
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/storage/sqlkit"
|
||||
)
|
||||
|
||||
// Backend is the metadata.json backend identifier for this store.
|
||||
const Backend = "mysql"
|
||||
|
||||
// Store is the MySQL backend. It embeds *sqlkit.Store (inheriting every core method)
|
||||
// and the generated typed-unsupported shell for the non-core surface.
|
||||
type Store struct {
|
||||
*sqlkit.Store
|
||||
unsupportedDoltStorage
|
||||
}
|
||||
|
||||
// Compile-time guard on the generated skip list: the composite backend (real
|
||||
// *sqlkit.Store methods plus the typed-unsupported shell) must fully satisfy
|
||||
// storage.DoltStorage. This fails the build on skip-list drift — a skipped
|
||||
// method sqlkit does not actually implement (missing method), or a method both
|
||||
// implemented and stubbed (ambiguous selector) — so no method can silently
|
||||
// become "unsupported".
|
||||
var _ storage.DoltStorage = (*Store)(nil)
|
||||
|
||||
// Config configures a MySQL backend. DSN targets the per-workspace database; the
|
||||
// password is merged from env at open and never persisted to metadata.json.
|
||||
type Config struct {
|
||||
DSN string
|
||||
}
|
||||
|
||||
// New builds the MySQL backend from its strategy bundle: the identity mysql dialect
|
||||
// and a no-op readiness strategy (issueops already maintains is_blocked in-tx, exactly
|
||||
// like the Dolt reference).
|
||||
func New(ctx context.Context, cfg Config) (*Store, error) {
|
||||
base, err := sqlkit.New(ctx, sqlkit.Config{
|
||||
Dialect: mysqlDialect{dsn: cfg.DSN},
|
||||
Readiness: sqlkit.NoopReadiness{},
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Store{Store: base}, nil
|
||||
}
|
||||
|
||||
// CommitGraphUnsupported tells cmd/bd's PostRun to skip Dolt-only maintenance.
|
||||
func (s *Store) CommitGraphUnsupported() bool { return true }
|
||||
|
||||
// CommitPending is a no-op on a non-version-controlled backend.
|
||||
func (s *Store) CommitPending(_ context.Context, _ string) (bool, error) { return false, nil }
|
||||
|
||||
// Commit and its variants are no-ops on MySQL, not unsupported: every sqlkit write
|
||||
// already committed its own SQL transaction, so the data is durable. Command paths
|
||||
// call store.Commit() opportunistically; returning nil lets those writes stand.
|
||||
func (s *Store) Commit(_ context.Context, _ string) error { return nil }
|
||||
func (s *Store) CommitWithConfig(_ context.Context, _ string) error { return nil }
|
||||
func (s *Store) CommitMergeResolution(_ context.Context, _ string) error { return nil }
|
||||
|
||||
var (
|
||||
_ storage.DoltStorage = (*Store)(nil)
|
||||
_ storage.NonCommitGraphBackend = (*Store)(nil)
|
||||
)
|
||||
|
|
@ -1,17 +0,0 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
)
|
||||
|
||||
// Regeneration mirrors postgres: the mysql shell is the exact complement of
|
||||
// *sqlkit.Store's method set (identical base + identical Commit/CommitGraph overrides,
|
||||
// so the complement is identical to postgres's shell modulo package name).
|
||||
//
|
||||
//go:generate go run ../unsupportedgen -pkg mysql -src .. -out unsupported_gen.go -type DoltStorage -skip AddDependency,AddIssueComment,AddLabel,ClaimIssue,ClaimReadyIssue,Close,CloseIssue,Commit,CommitMergeResolution,CommitPending,CommitWithConfig,CountDependencies,CountDependents,CountEvents,CountIssueComments,CountIssues,CountIssuesByGroup,CreateIssue,CreateIssues,DeleteConfig,DeleteIssue,DeleteIssues,DetectCycles,GetAllConfig,GetBlockedIssues,GetBlockingInfoForIssues,GetCommentCounts,GetCommentsForIssues,GetConfig,GetCustomStatuses,GetCustomStatusesDetailed,GetCustomTypes,GetDependencies,GetDependenciesWithMetadata,GetDependencyCounts,GetDependencyRecords,GetDependencyRecordsForIssues,GetDependencyTree,GetDependents,GetDependentsWithMetadata,GetEpicsEligibleForClosure,GetEvents,GetInfraTypes,GetIssue,GetIssueByExternalRef,GetIssueComments,GetIssuesByIDs,GetIssuesByLabel,GetLabels,GetLabelsForIssues,GetLocalMetadata,GetMetadata,GetNewlyUnblockedByClose,GetNextChildID,GetReadyWork,GetReadyWorkWithCounts,GetStaleIssues,GetStatistics,IsBlocked,IsInfraTypeCtx,IterBlockedIssues,IterDependenciesWithMetadata,IterDependentsWithMetadata,IterEvents,IterIssueComments,IterIssues,IterReadyWork,IterWisps,ListWisps,RemoveDependency,RemoveLabel,ReopenIssue,RunInTransaction,SearchIssues,SearchIssuesWithCounts,SetConfig,SetLocalMetadata,SetMetadata,UpdateIssue,UpdateIssueType,AddComment,ClearRepoMtime,CountDependentsByStatus,DeleteIssuesBySourceRepo,FindWispDependentsRecursive,GetAllDependencyRecords,GetAllEventsSince,GetMoleculeLastActivity,GetMoleculeProgress,GetRepoMtime,ImportIssueComment,IterAllDependencyRecords,IterAllEventsSince,PromoteFromEphemeral,SetRepoMtime,UpdateIssueID,CreateIssuesWithFullOptions,SlotClear,SlotGet,SlotSet,SearchIssueIDs,HeartbeatIssue,ReclaimExpiredLeases,UnclaimIssue
|
||||
|
||||
// errUnsupported is the constructor every generated stub in unsupported_gen.go calls.
|
||||
// It returns the backend-agnostic *storage.ErrUnsupported sentinel; Backend is "mysql".
|
||||
func errUnsupported(op string) error {
|
||||
return &storage.ErrUnsupported{Op: op, Backend: Backend}
|
||||
}
|
||||
|
|
@ -1,236 +0,0 @@
|
|||
// Code generated by gen (mysql typed-unsupported shell); DO NOT EDIT.
|
||||
|
||||
package mysql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/steveyegge/beads/internal/storage"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
// unsupportedDoltStorage is the generated typed-unsupported shell for storage.DoltStorage.
|
||||
// Every method returns *storage.ErrUnsupported; embed it and override the
|
||||
// real slice. DO NOT hand-edit — regenerate with `go generate ./...`.
|
||||
type unsupportedDoltStorage struct{}
|
||||
|
||||
func (unsupportedDoltStorage) MergeSlotCreate(_ context.Context, _ string) (_ *types.Issue, err error) {
|
||||
err = errUnsupported("MergeSlotCreate")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) MergeSlotCheck(_ context.Context) (_ *storage.MergeSlotStatus, err error) {
|
||||
err = errUnsupported("MergeSlotCheck")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) MergeSlotAcquire(_ context.Context, _ string, _ string, _ bool) (_ *storage.MergeSlotResult, err error) {
|
||||
err = errUnsupported("MergeSlotAcquire")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) MergeSlotRelease(_ context.Context, _ string, _ string) (err error) {
|
||||
err = errUnsupported("MergeSlotRelease")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) Branch(_ context.Context, _ string) (err error) {
|
||||
err = errUnsupported("Branch")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) Checkout(_ context.Context, _ string) (err error) {
|
||||
err = errUnsupported("Checkout")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) CurrentBranch(_ context.Context) (_ string, err error) {
|
||||
err = errUnsupported("CurrentBranch")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) DeleteBranch(_ context.Context, _ string) (err error) {
|
||||
err = errUnsupported("DeleteBranch")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) ListBranches(_ context.Context) (_ []string, err error) {
|
||||
err = errUnsupported("ListBranches")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) CommitExists(_ context.Context, _ string) (_ bool, err error) {
|
||||
err = errUnsupported("CommitExists")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) GetCurrentCommit(_ context.Context) (_ string, err error) {
|
||||
err = errUnsupported("GetCurrentCommit")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) Status(_ context.Context) (_ *storage.Status, err error) {
|
||||
err = errUnsupported("Status")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) Log(_ context.Context, _ int) (_ []storage.CommitInfo, err error) {
|
||||
err = errUnsupported("Log")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) Merge(_ context.Context, _ string) (_ []storage.Conflict, err error) {
|
||||
err = errUnsupported("Merge")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) GetConflicts(_ context.Context) (_ []storage.Conflict, err error) {
|
||||
err = errUnsupported("GetConflicts")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) ResolveConflicts(_ context.Context, _ string, _ string) (err error) {
|
||||
err = errUnsupported("ResolveConflicts")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) History(_ context.Context, _ string) (_ []*storage.HistoryEntry, err error) {
|
||||
err = errUnsupported("History")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) AsOf(_ context.Context, _ string, _ string) (_ *types.Issue, err error) {
|
||||
err = errUnsupported("AsOf")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) Diff(_ context.Context, _ string, _ string) (_ []*storage.DiffEntry, err error) {
|
||||
err = errUnsupported("Diff")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) AddRemote(_ context.Context, _ string, _ string) (err error) {
|
||||
err = errUnsupported("AddRemote")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) RemoveRemote(_ context.Context, _ string) (err error) {
|
||||
err = errUnsupported("RemoveRemote")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) HasRemote(_ context.Context, _ string) (_ bool, err error) {
|
||||
err = errUnsupported("HasRemote")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) ListRemotes(_ context.Context) (_ []storage.RemoteInfo, err error) {
|
||||
err = errUnsupported("ListRemotes")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) Push(_ context.Context) (err error) {
|
||||
err = errUnsupported("Push")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) Pull(_ context.Context) (err error) {
|
||||
err = errUnsupported("Pull")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) ForcePush(_ context.Context) (err error) {
|
||||
err = errUnsupported("ForcePush")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) PushRemote(_ context.Context, _ string, _ bool) (err error) {
|
||||
err = errUnsupported("PushRemote")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) PullRemote(_ context.Context, _ string) (err error) {
|
||||
err = errUnsupported("PullRemote")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) Fetch(_ context.Context, _ string) (err error) {
|
||||
err = errUnsupported("Fetch")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) PushTo(_ context.Context, _ string) (err error) {
|
||||
err = errUnsupported("PushTo")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) PullFrom(_ context.Context, _ string) (_ []storage.Conflict, err error) {
|
||||
err = errUnsupported("PullFrom")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) Sync(_ context.Context, _ string, _ string) (_ *storage.SyncResult, err error) {
|
||||
err = errUnsupported("Sync")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) SyncStatus(_ context.Context, _ string) (_ *storage.SyncStatus, err error) {
|
||||
err = errUnsupported("SyncStatus")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) AddFederationPeer(_ context.Context, _ *storage.FederationPeer) (err error) {
|
||||
err = errUnsupported("AddFederationPeer")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) GetFederationPeer(_ context.Context, _ string) (_ *storage.FederationPeer, err error) {
|
||||
err = errUnsupported("GetFederationPeer")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) ListFederationPeers(_ context.Context) (_ []*storage.FederationPeer, err error) {
|
||||
err = errUnsupported("ListFederationPeers")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) RemoveFederationPeer(_ context.Context, _ string) (err error) {
|
||||
err = errUnsupported("RemoveFederationPeer")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) CheckEligibility(_ context.Context, _ string, _ int) (_ bool, _ string, err error) {
|
||||
err = errUnsupported("CheckEligibility")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) ApplyCompaction(_ context.Context, _ string, _ int, _ int, _ int, _ string) (err error) {
|
||||
err = errUnsupported("ApplyCompaction")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) GetTier1Candidates(_ context.Context) (_ []*types.CompactionCandidate, err error) {
|
||||
err = errUnsupported("GetTier1Candidates")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) GetTier2Candidates(_ context.Context) (_ []*types.CompactionCandidate, err error) {
|
||||
err = errUnsupported("GetTier2Candidates")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) SnapshotIssue(_ context.Context, _ string, _ int) (err error) {
|
||||
err = errUnsupported("SnapshotIssue")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) GetCompactionSnapshot(_ context.Context, _ string) (_ *types.IssueSnapshot, err error) {
|
||||
err = errUnsupported("GetCompactionSnapshot")
|
||||
return
|
||||
}
|
||||
|
||||
func (unsupportedDoltStorage) RestoreFromSnapshot(_ context.Context, _ string) (_ *types.IssueSnapshot, err error) {
|
||||
err = errUnsupported("RestoreFromSnapshot")
|
||||
return
|
||||
}
|
||||
|
||||
// NOTE: partial shell (104 methods skipped); the embedding composite must assert storage.DoltStorage itself.
|
||||
|
|
@ -1,83 +0,0 @@
|
|||
package mysqldialect
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// HasPassword reports whether the DSN already carries a password. A DSN that already
|
||||
// has one (a full BEADS_MYSQL_URL, or an explicit --mysql-url) wins outright: the
|
||||
// credential ladder is not applied on top of it.
|
||||
func HasPassword(dsn string) bool {
|
||||
cfg, err := mysql.ParseDSN(dsn)
|
||||
return err == nil && cfg.Passwd != ""
|
||||
}
|
||||
|
||||
// WithCredential returns dsn with the resolved credential placed: the password
|
||||
// always, and the username only when non-empty (a Vault-style dynamic user/password
|
||||
// pair) — an empty username leaves the DSN's user untouched. It uses go-sql-driver's
|
||||
// own parser (never string surgery) and VERIFIES the round-trip.
|
||||
//
|
||||
// The grammar has one sharp edge pgdialect does not: it cannot carry a password
|
||||
// without a username (FormatDSN emits the user:pass@ block only when a user is
|
||||
// present), so a userless result is refused. But a provided username satisfies that
|
||||
// requirement, making a bare tcp(host)/ base DSN plus a dynamic user/password pair a
|
||||
// valid configuration. This is the counterpart to mysql.RedactPassword.
|
||||
func WithCredential(dsn, username, password string) (string, error) {
|
||||
cfg, err := mysql.ParseDSN(dsn)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot place credential into the connection string (unparseable DSN): %w", err)
|
||||
}
|
||||
if username != "" {
|
||||
cfg.User = username
|
||||
}
|
||||
if cfg.User == "" {
|
||||
return "", fmt.Errorf("cannot place a password: the DSN has no username, and the MySQL DSN grammar cannot carry a password without one; add a user to the DSN")
|
||||
}
|
||||
cfg.Passwd = password
|
||||
out := cfg.FormatDSN()
|
||||
|
||||
again, err := mysql.ParseDSN(out)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot place credential into the connection string (unparseable result): %w", err)
|
||||
}
|
||||
if again.Passwd != password {
|
||||
return "", fmt.Errorf("password did not round-trip into the connection string; refusing to connect with a wrong or empty password")
|
||||
}
|
||||
if username != "" && again.User != username {
|
||||
return "", fmt.Errorf("username did not round-trip into the connection string; refusing to connect as the wrong user")
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// WithPassword places only the password, preserving the DSN's user. It is
|
||||
// WithCredential with no username override.
|
||||
func WithPassword(dsn, password string) (string, error) {
|
||||
return WithCredential(dsn, "", password)
|
||||
}
|
||||
|
||||
// HostPort returns the TCP endpoint go-sql-driver would dial for dsn, for keying the
|
||||
// [host:port] credentials-file lookup. ok=false for non-TCP networks (a unix socket
|
||||
// has no port) or an unparseable DSN; the caller degrades the file rung to
|
||||
// not-configured rather than failing.
|
||||
func HostPort(dsn string) (host string, port int, ok bool) {
|
||||
cfg, err := mysql.ParseDSN(dsn)
|
||||
if err != nil {
|
||||
return "", 0, false
|
||||
}
|
||||
if cfg.Net != "tcp" {
|
||||
return "", 0, false
|
||||
}
|
||||
h, p, err := net.SplitHostPort(cfg.Addr)
|
||||
if err != nil {
|
||||
return "", 0, false
|
||||
}
|
||||
port, err = strconv.Atoi(p)
|
||||
if err != nil {
|
||||
return "", 0, false
|
||||
}
|
||||
return h, port, true
|
||||
}
|
||||
|
|
@ -1,81 +0,0 @@
|
|||
package mysqldialect
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func mustUser(t *testing.T, out string) string {
|
||||
t.Helper()
|
||||
cfg, err := mysql.ParseDSN(out)
|
||||
if err != nil {
|
||||
t.Fatalf("mysql.ParseDSN(%q): %v", out, err)
|
||||
}
|
||||
return cfg.User
|
||||
}
|
||||
|
||||
func TestWithCredentialOverridesUser(t *testing.T) {
|
||||
out, err := WithCredential("old@tcp(h:3306)/db", "vu", "pw")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u := mustUser(t, out); u != "vu" {
|
||||
t.Fatalf("user = %q, want vu", u)
|
||||
}
|
||||
if pw := mustPassword(t, out); pw != "pw" {
|
||||
t.Fatalf("password = %q, want pw", pw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithCredentialEmptyUsernameKeepsUser(t *testing.T) {
|
||||
out, err := WithCredential("old@tcp(h:3306)/db", "", "pw")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u := mustUser(t, out); u != "old" {
|
||||
t.Fatalf("user = %q, want old (preserved)", u)
|
||||
}
|
||||
}
|
||||
|
||||
// A provided username satisfies the grammar's user requirement, making a userless base
|
||||
// DSN plus a dynamic user/password pair valid.
|
||||
func TestWithCredentialUsernameSatisfiesUserlessDSN(t *testing.T) {
|
||||
out, err := WithCredential("tcp(h:3306)/db", "vu", "pw")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u := mustUser(t, out); u != "vu" {
|
||||
t.Fatalf("user = %q, want vu", u)
|
||||
}
|
||||
if pw := mustPassword(t, out); pw != "pw" {
|
||||
t.Fatalf("password = %q, want pw", pw)
|
||||
}
|
||||
}
|
||||
|
||||
// Password-only against a userless DSN still fails (the guard is intact).
|
||||
func TestWithCredentialUserlessStillFails(t *testing.T) {
|
||||
if _, err := WithCredential("tcp(h:3306)/db", "", "pw"); err == nil {
|
||||
t.Fatal("expected error: a userless DSN cannot carry a password without a username")
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostPort(t *testing.T) {
|
||||
cases := []struct {
|
||||
dsn string
|
||||
host string
|
||||
port int
|
||||
ok bool
|
||||
}{
|
||||
{"u@tcp(10.0.0.5:55441)/", "10.0.0.5", 55441, true},
|
||||
{"u@/", "127.0.0.1", 3306, true}, // tcp Addr defaults to 127.0.0.1:3306
|
||||
{"u@tcp(dbhost)/", "dbhost", 3306, true}, // ensureHavePort appends :3306
|
||||
{"u@unix(/tmp/mysql.sock)/", "", 0, false}, // non-tcp -> not ok
|
||||
}
|
||||
for _, c := range cases {
|
||||
h, p, ok := HostPort(c.dsn)
|
||||
if ok != c.ok || (ok && (h != c.host || p != c.port)) {
|
||||
t.Errorf("HostPort(%q) = (%q,%d,%v), want (%q,%d,%v)", c.dsn, h, p, ok, c.host, c.port, c.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,82 +0,0 @@
|
|||
package mysqldialect
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
func TestHasPassword(t *testing.T) {
|
||||
cases := []struct {
|
||||
dsn string
|
||||
want bool
|
||||
}{
|
||||
{"bts:secret@tcp(127.0.0.1:3306)/db", true},
|
||||
{"bts@tcp(127.0.0.1:3306)/db", false},
|
||||
{"bts@/", false}, // no-tcp form parses, no password
|
||||
{"", false}, // empty parses to a default config
|
||||
{"bts:pw@tcp(127.0.0.1:3306)", false}, // missing trailing slash -> parse error
|
||||
}
|
||||
for _, c := range cases {
|
||||
if got := HasPassword(c.dsn); got != c.want {
|
||||
t.Errorf("HasPassword(%q) = %v, want %v", c.dsn, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// mustPassword parses out and returns the password go-sql-driver sees.
|
||||
func mustPassword(t *testing.T, out string) string {
|
||||
t.Helper()
|
||||
cfg, err := mysql.ParseDSN(out)
|
||||
if err != nil {
|
||||
t.Fatalf("mysql.ParseDSN(%q): %v", out, err)
|
||||
}
|
||||
return cfg.Passwd
|
||||
}
|
||||
|
||||
func TestWithPasswordBasic(t *testing.T) {
|
||||
out, err := WithPassword("bts@tcp(127.0.0.1:55441)/", "s3cr3t")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pw := mustPassword(t, out); pw != "s3cr3t" {
|
||||
t.Fatalf("password = %q, want s3cr3t", pw)
|
||||
}
|
||||
cfg, _ := mysql.ParseDSN(out)
|
||||
if cfg.User != "bts" {
|
||||
t.Fatalf("user = %q, want bts (must be preserved)", cfg.User)
|
||||
}
|
||||
}
|
||||
|
||||
// go-sql-driver writes the password verbatim; a token full of DSN-significant
|
||||
// characters (as an RDS-IAM token carries) must round-trip byte-for-byte.
|
||||
func TestWithPasswordSpecialChars(t *testing.T) {
|
||||
for _, tok := range []string{"p@ss:w/rd?x=1&y=2", "X-Amz=abc&Sig=d/e+f%20", `a'b\c d`} {
|
||||
out, err := WithPassword("bts@tcp(h:3306)/db", tok)
|
||||
if err != nil {
|
||||
t.Fatalf("token %q: %v", tok, err)
|
||||
}
|
||||
if pw := mustPassword(t, out); pw != tok {
|
||||
t.Fatalf("password = %q, want %q", pw, tok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithPasswordReplacesExisting(t *testing.T) {
|
||||
out, err := WithPassword("bts:old@tcp(h:3306)/db", "new")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pw := mustPassword(t, out); pw != "new" {
|
||||
t.Fatalf("password = %q, want new", pw)
|
||||
}
|
||||
}
|
||||
|
||||
// The MySQL sharp edge: a userless DSN cannot carry a password (FormatDSN drops it),
|
||||
// so WithPassword must refuse rather than silently connect without one.
|
||||
func TestWithPasswordUserlessDSNFails(t *testing.T) {
|
||||
_, err := WithPassword("tcp(127.0.0.1:55441)/", "orphan")
|
||||
if err == nil {
|
||||
t.Fatal("expected an error for a userless DSN (password would be silently dropped)")
|
||||
}
|
||||
}
|
||||
|
|
@ -1,91 +0,0 @@
|
|||
// Package mysqldialect is the MySQL backend's thin driver wrapper. bd's canonical SQL
|
||||
// is MySQL, so unlike pgdialect there is no translation — the ONE rewrite is the
|
||||
// self-referential is_blocked recompute UPDATE, which MySQL 8 rejects (error 1093: a
|
||||
// table may not be updated while referenced in its own subquery) but Dolt and Postgres
|
||||
// permit. The rewrite is isolated here, so the shared issueops/sqlkit layers and the
|
||||
// Dolt/Postgres backends are entirely untouched.
|
||||
//
|
||||
// A portable, perf-reviewed rewrite in shared issueops is tracked as a backlog item;
|
||||
// this wrapper is the per-backend containment chosen to avoid touching Dolt's hot path.
|
||||
package mysqldialect
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
|
||||
"github.com/go-sql-driver/mysql"
|
||||
)
|
||||
|
||||
// Open returns a *sql.DB over go-sql-driver whose Prepare rewrites the recompute
|
||||
// UPDATE (see rewriteSelfRefUpdate); every other statement passes through verbatim.
|
||||
// Like pgdialect, the wrapper exposes no Execer/Queryer fast path, so database/sql
|
||||
// routes every statement through Prepare — the one place the rewrite happens.
|
||||
func Open(dsn string) (*sql.DB, error) {
|
||||
base, err := (mysql.MySQLDriver{}).OpenConnector(dsn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sql.OpenDB(&connector{base: base}), nil
|
||||
}
|
||||
|
||||
type connector struct{ base driver.Connector }
|
||||
|
||||
func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
|
||||
cn, err := c.base.Connect(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &conn{base: cn}, nil
|
||||
}
|
||||
|
||||
func (c *connector) Driver() driver.Driver { return c.base.Driver() }
|
||||
|
||||
// conn wraps the mysql driver.Conn and rewrites the recompute UPDATE at prepare time.
|
||||
// It deliberately implements no Execer/Queryer so database/sql prepares every stmt.
|
||||
type conn struct{ base driver.Conn }
|
||||
|
||||
func (c *conn) Prepare(query string) (driver.Stmt, error) {
|
||||
return c.base.Prepare(rewriteSelfRefUpdate(query))
|
||||
}
|
||||
|
||||
func (c *conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
|
||||
q := rewriteSelfRefUpdate(query)
|
||||
if p, ok := c.base.(driver.ConnPrepareContext); ok {
|
||||
return p.PrepareContext(ctx, q)
|
||||
}
|
||||
return c.base.Prepare(q)
|
||||
}
|
||||
|
||||
func (c *conn) Close() error { return c.base.Close() }
|
||||
|
||||
//nolint:staticcheck // SA1019: driver.Conn requires the deprecated Begin; BeginTx forwarded below.
|
||||
func (c *conn) Begin() (driver.Tx, error) { return c.base.Begin() }
|
||||
|
||||
func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
|
||||
if b, ok := c.base.(driver.ConnBeginTx); ok {
|
||||
return b.BeginTx(ctx, opts)
|
||||
}
|
||||
return c.base.Begin() //nolint:staticcheck
|
||||
}
|
||||
|
||||
func (c *conn) ResetSession(ctx context.Context) error {
|
||||
if rs, ok := c.base.(driver.SessionResetter); ok {
|
||||
return rs.ResetSession(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) IsValid() bool {
|
||||
if v, ok := c.base.(driver.Validator); ok {
|
||||
return v.IsValid()
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (c *conn) Ping(ctx context.Context) error {
|
||||
if p, ok := c.base.(driver.Pinger); ok {
|
||||
return p.Ping(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -1,43 +0,0 @@
|
|||
package mysqldialect
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// selfRefUpdateRe matches bd's is_blocked recompute shape:
|
||||
//
|
||||
// UPDATE <table> <alias> SET <...> WHERE <alias>.id IN (<placeholders>) <rest>
|
||||
//
|
||||
// The SET is non-greedy up to the first WHERE (SET clauses contain no WHERE); the
|
||||
// placeholder list is the batched `?,?,…`.
|
||||
var selfRefUpdateRe = regexp.MustCompile(`(?is)^(\s*UPDATE\s+(\w+)\s+(\w+)\s+SET\s+.+?)\s+WHERE\s+(\w+)\.id\s+IN\s+\(([^)]*)\)\s*(.*)$`)
|
||||
|
||||
// rewriteSelfRefUpdate rewrites the is_blocked recompute UPDATE into a MySQL-8-safe
|
||||
// double-nested form so the target table's self-reference is materialized (avoiding
|
||||
// error 1093). Everything else is returned verbatim. The transform is semantics-
|
||||
// preserving: the id set (batch ∩ conditions) is computed in a materialized derived
|
||||
// table, and the outer UPDATE writes exactly those rows. Positional placeholders keep
|
||||
// their order because the WHERE body is moved intact.
|
||||
func rewriteSelfRefUpdate(q string) string {
|
||||
// Fast reject: only the is_blocked recompute both sets is_blocked and self-joins.
|
||||
if !strings.Contains(q, "is_blocked") {
|
||||
return q
|
||||
}
|
||||
m := selfRefUpdateRe.FindStringSubmatch(q)
|
||||
if m == nil {
|
||||
return q
|
||||
}
|
||||
head, table, alias, whereAlias, placeholders, rest := m[1], m[2], m[3], m[4], m[5], m[6]
|
||||
// Only the recompute has further conditions after the IN clause (the self-ref
|
||||
// EXISTS block); a plain `UPDATE … WHERE id IN (…)` needs no rewrite.
|
||||
if whereAlias != alias || strings.TrimSpace(rest) == "" {
|
||||
return q
|
||||
}
|
||||
// Rebind the outer alias to the derived-table alias inside the moved conditions.
|
||||
reb := regexp.MustCompile(`\b` + regexp.QuoteMeta(alias) + `\.`)
|
||||
restRebound := reb.ReplaceAllString(rest, "_b.")
|
||||
return fmt.Sprintf("%s WHERE %s.id IN (SELECT id FROM (SELECT _b.id FROM %s _b WHERE _b.id IN (%s) %s) AS _blk)",
|
||||
head, alias, table, placeholders, restRebound)
|
||||
}
|
||||
|
|
@ -1,123 +0,0 @@
|
|||
package mysqldialect
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/issueops"
|
||||
)
|
||||
|
||||
func TestRewriteSelfRefUpdate(t *testing.T) {
|
||||
recompute := `UPDATE issues i SET i.is_blocked = 1, i.updated_at = i.updated_at
|
||||
WHERE i.id IN (?,?,?)
|
||||
AND i.is_blocked = 0
|
||||
AND i.status <> 'closed' AND i.status <> 'pinned'
|
||||
AND (EXISTS (SELECT 1 FROM dependencies d JOIN issues t ON t.id = d.depends_on_issue_id
|
||||
WHERE d.issue_id = i.id AND d.type = 'blocks' AND t.status <> 'closed'))`
|
||||
|
||||
got := rewriteSelfRefUpdate(recompute)
|
||||
if got == recompute {
|
||||
t.Fatal("recompute UPDATE was not rewritten")
|
||||
}
|
||||
// The outer target must no longer be referenced directly in a subquery: the
|
||||
// materialized derived table carries the self-reference.
|
||||
if !strings.Contains(got, "SELECT id FROM (SELECT _b.id FROM issues _b") {
|
||||
t.Fatalf("missing double-nested derived table:\n%s", got)
|
||||
}
|
||||
// The correlation must be rebound to the derived-table alias.
|
||||
if !strings.Contains(got, "d.issue_id = _b.id") {
|
||||
t.Fatalf("alias not rebound to _b:\n%s", got)
|
||||
}
|
||||
// The outer SET still targets the real alias.
|
||||
if !strings.HasPrefix(strings.TrimSpace(got), "UPDATE issues i SET i.is_blocked = 1") {
|
||||
t.Fatalf("outer UPDATE target changed:\n%s", got)
|
||||
}
|
||||
// Placeholder count preserved (positional binding).
|
||||
if a, b := strings.Count(recompute, "?"), strings.Count(got, "?"); a != b {
|
||||
t.Fatalf("placeholder count changed: %d -> %d", a, b)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRewriteMatchesRealRecomputeTemplates drives the rewrite from the ACTUAL
|
||||
// issueops is_blocked recompute templates, not a hand-copied fixture. The rewrite
|
||||
// regex is coupled to the exact shape those templates emit (UPDATE <tbl> <alias> SET
|
||||
// … WHERE <alias>.id IN (…) <self-ref EXISTS>); if a template later grows a predicate
|
||||
// before `id IN`, backticks the table, renames the alias, or changes whitespace, the
|
||||
// regex would silently stop matching and MySQL 8 would raise error 1093 at runtime on
|
||||
// a core write path. That failure would otherwise only surface under the env-gated
|
||||
// MySQL conformance suite — this non-gated test makes template drift fail `go test`.
|
||||
func TestRewriteMatchesRealRecomputeTemplates(t *testing.T) {
|
||||
templates := issueops.SelfReferentialRecomputeTemplates()
|
||||
if len(templates) == 0 {
|
||||
t.Fatal("issueops.SelfReferentialRecomputeTemplates() returned no templates")
|
||||
}
|
||||
// Parse the UPDATE target table+alias so the assertions can target the OUTER
|
||||
// self-reference specifically (the inner materialized `_b.id IN (?…)` is expected).
|
||||
updateHead := regexp.MustCompile(`(?is)^\s*UPDATE\s+(\w+)\s+(\w+)\s+SET\b`)
|
||||
for i, tmpl := range templates {
|
||||
m := updateHead.FindStringSubmatch(tmpl)
|
||||
if m == nil {
|
||||
t.Errorf("template[%d] is not an `UPDATE <table> <alias> SET …` shape:\n%s", i, tmpl)
|
||||
continue
|
||||
}
|
||||
table, alias := m[1], m[2]
|
||||
|
||||
got := rewriteSelfRefUpdate(tmpl)
|
||||
if got == tmpl {
|
||||
t.Errorf("template[%d] was NOT rewritten — the rewrite regex no longer matches an is_blocked "+
|
||||
"recompute template; MySQL error 1093 would return at runtime:\n%s", i, tmpl)
|
||||
continue
|
||||
}
|
||||
// The target table must be materialized in a derived table so the outer UPDATE
|
||||
// no longer references it directly in a subquery (the 1093 avoidance).
|
||||
if want := "SELECT id FROM (SELECT _b.id FROM " + table + " _b"; !strings.Contains(got, want) {
|
||||
t.Errorf("template[%d] rewrite missing the double-nested derived table %q:\n%s", i, want, got)
|
||||
}
|
||||
// The OUTER target alias must now select from the materialized set, not a bare
|
||||
// self-referential `<alias>.id IN (?…)`.
|
||||
outerMaterialized := regexp.MustCompile(`(?i)\b` + alias + `\.id\s+IN\s+\(\s*SELECT`)
|
||||
if !outerMaterialized.MatchString(got) {
|
||||
t.Errorf("template[%d] outer %s.id IN (…) was not materialized into a subselect:\n%s", i, alias, got)
|
||||
}
|
||||
outerRaw := regexp.MustCompile(`(?i)\b` + alias + `\.id\s+IN\s+\(\s*\?`)
|
||||
if outerRaw.MatchString(got) {
|
||||
t.Errorf("template[%d] outer %s.id IN (?…) is still a raw self-reference after rewrite (MySQL 1093):\n%s", i, alias, got)
|
||||
}
|
||||
// Positional binding is preserved (placeholders unchanged).
|
||||
if a, b := strings.Count(tmpl, "?"), strings.Count(got, "?"); a != b {
|
||||
t.Errorf("template[%d] placeholder count changed: %d -> %d", i, a, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRewritePassThrough(t *testing.T) {
|
||||
cases := []string{
|
||||
"SELECT * FROM issues WHERE id = ?",
|
||||
"UPDATE issues SET title = ? WHERE id = ?", // no is_blocked, no IN
|
||||
"UPDATE issues i SET i.is_blocked = 1 WHERE i.id IN (?,?)", // batch update, no self-ref conditions
|
||||
"INSERT INTO issues (id) VALUES (?)",
|
||||
"DELETE FROM issues WHERE id = ?",
|
||||
}
|
||||
for _, q := range cases {
|
||||
if got := rewriteSelfRefUpdate(q); got != q {
|
||||
t.Errorf("query was rewritten but should pass through:\n in: %s\nout: %s", q, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The rewrite must not leave the outer target table referenced in a subquery in a way
|
||||
// that regex could double-apply; a second pass is a no-op on already-rewritten SQL.
|
||||
func TestRewriteIdempotentShape(t *testing.T) {
|
||||
q := `UPDATE wisps w SET w.is_blocked = 0, w.updated_at = w.updated_at
|
||||
WHERE w.id IN (?) AND w.is_blocked = 1 AND (w.status = 'closed')`
|
||||
once := rewriteSelfRefUpdate(q)
|
||||
if once == q {
|
||||
t.Fatal("expected rewrite")
|
||||
}
|
||||
// A plain "?,?" derived form should still parse-shape sanely (no target alias left
|
||||
// as a bare self-join outside the derived table).
|
||||
if regexp.MustCompile(`WHERE w\.id IN \(\?`).MatchString(once) {
|
||||
t.Fatalf("outer WHERE still has a raw placeholder IN (self-ref not materialized):\n%s", once)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,430 +0,0 @@
|
|||
package pgdialect_test
|
||||
|
||||
// This file is the PREPARE gate the package doc promises. It enumerates every
|
||||
// distinct statement family bd's shared layer (sqlbuild + issueops) emits on
|
||||
// the read/search path, runs each through pgdialect.Translate, and — when a
|
||||
// live Postgres is configured via BEADS_PG_TEST_URL — PREPAREs the translated
|
||||
// text against a scratch schema. An untranslatable construct fails the test
|
||||
// loudly (Translate error, surviving MySQL residue, or a PREPARE-time Postgres
|
||||
// error) instead of silently mistranslating a row set.
|
||||
//
|
||||
// It is an external test package (pgdialect_test) so it can live alongside the
|
||||
// white-box translate_test.go while only touching pgdialect's exported surface.
|
||||
//
|
||||
// The scratch schema is built here rather than via postgres.InitSchema on
|
||||
// purpose: pgdialect is a lower layer than the postgres backend (postgres
|
||||
// imports pgdialect), so importing postgres into pgdialect's tests would invert
|
||||
// the dependency and couple this gate to that package's build state. The tables
|
||||
// are generated from sqlbuild.IssueSelectColumns so they cannot drift out of
|
||||
// sync with the column list the queries project. Only the shapes the corpus
|
||||
// exercises are created; types are faithful where an operator depends on them
|
||||
// (jsonb metadata, integer priority, smallint flags, timestamp columns) and
|
||||
// text elsewhere.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/steveyegge/beads/internal/storage/pgdialect"
|
||||
"github.com/steveyegge/beads/internal/storage/sqlbuild"
|
||||
"github.com/steveyegge/beads/internal/types"
|
||||
)
|
||||
|
||||
type corpusStmt struct {
|
||||
name string
|
||||
sql string
|
||||
}
|
||||
|
||||
func strp(s string) *string { return &s }
|
||||
func intp(i int) *int { return &i }
|
||||
|
||||
// searchSelectSQL mirrors issueops.searchTableInTx Pattern A: the full-column
|
||||
// filtered scan for one table family (incl. the label-driven JOIN plan).
|
||||
func searchSelectSQL(tables sqlbuild.FilterTables, query string, filter types.IssueFilter) (string, error) {
|
||||
plan := sqlbuild.BuildLabelDrivenSearch(filter, tables)
|
||||
where, _, err := sqlbuild.BuildIssueFilterClauses(query, plan.Filter, tables)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
where, _ = plan.MergeInto(where, nil)
|
||||
whereSQL := ""
|
||||
if len(where) > 0 {
|
||||
whereSQL = "WHERE " + strings.Join(where, " AND ")
|
||||
}
|
||||
selectKw := "SELECT "
|
||||
if plan.Distinct {
|
||||
selectKw = "SELECT DISTINCT "
|
||||
}
|
||||
return fmt.Sprintf("%s%s FROM %s %s %s %s",
|
||||
selectKw, sqlbuild.IssueSelectColumns, plan.FromSQL, sqlbuild.LeaseJoin(tables.Main), whereSQL,
|
||||
sqlbuild.OrderBy(filter.SortBy, filter.SortDesc, "")), nil
|
||||
}
|
||||
|
||||
// searchCountsSQL mirrors issueops.runFilterSearchQueryInTx: the counts
|
||||
// mega-query (the DATE_FORMAT / JSON_OBJECT / CAST / JSON_ARRAYAGG surface).
|
||||
func searchCountsSQL(tables sqlbuild.FilterTables, query string, filter types.IssueFilter, includeWispReverseDeps bool) (string, error) {
|
||||
where, _, err := sqlbuild.BuildIssueFilterClauses(query, filter, tables)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
whereSQL := ""
|
||||
if len(where) > 0 {
|
||||
whereSQL = "WHERE " + strings.Join(where, " AND ")
|
||||
}
|
||||
sql, _ := sqlbuild.SearchCountsSQL(tables, nil, whereSQL, sqlbuild.OrderBy(filter.SortBy, filter.SortDesc, "i"), "", includeWispReverseDeps, filter.SkipLabels)
|
||||
return sql, nil
|
||||
}
|
||||
|
||||
// readySelectSQL mirrors issueops.GetReadyWorkInTx: SELECT id FROM <main> with
|
||||
// the ready-work WHERE/ORDER.
|
||||
func readySelectSQL(tables sqlbuild.FilterTables, filter types.WorkFilter) (string, error) {
|
||||
whereSQL, _, err := sqlbuild.BuildReadyWorkWhere(filter, tables, sqlbuild.ReadyWorkWhereInputs{})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
order := sqlbuild.BuildReadyWorkOrder(filter.SortPolicy, "created_at", "priority")
|
||||
return fmt.Sprintf("SELECT id FROM %s %s %s", tables.Main, whereSQL, order.SQL), nil
|
||||
}
|
||||
|
||||
// readyCountsSQL mirrors issueops.GetReadyWorkWithCountsInTx: the counts
|
||||
// mega-query fed the ready-work predicates.
|
||||
func readyCountsSQL(tables sqlbuild.FilterTables, filter types.WorkFilter, includeWispReverseDeps bool) (string, error) {
|
||||
whereSQL, _, err := sqlbuild.BuildReadyWorkWhere(filter, tables, sqlbuild.ReadyWorkWhereInputs{})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
order := sqlbuild.BuildReadyWorkOrder(filter.SortPolicy, "created_at", "priority")
|
||||
sql, _ := sqlbuild.SearchCountsSQL(tables, nil, whereSQL, order.SQL, "", includeWispReverseDeps, false)
|
||||
return sql, nil
|
||||
}
|
||||
|
||||
// adaptiveIDInstrSQL is issueops.GetAdaptiveIDLengthTx's create-path query
|
||||
// (CONCAT + INSTR + SUBSTRING). Reproduced verbatim so the corpus covers the
|
||||
// non-search statement family the smoke test's explicit-ID creates never hit.
|
||||
const adaptiveIDInstrSQL = `SELECT COUNT(*)
|
||||
FROM issues
|
||||
WHERE id LIKE CONCAT(?, '-%')
|
||||
AND INSTR(SUBSTRING(id, LENGTH(?) + 2), '.') = 0`
|
||||
|
||||
func buildCorpus(t *testing.T) []corpusStmt {
|
||||
t.Helper()
|
||||
|
||||
// Representative filters: priority, an AND label, metadata has-key + eq,
|
||||
// and a parent filter (each drives a distinct clause shape).
|
||||
richIssue := types.IssueFilter{
|
||||
Priority: intp(1),
|
||||
Labels: []string{"backlog"},
|
||||
HasMetadataKey: "sprint",
|
||||
MetadataFields: map[string]string{"team": "core"},
|
||||
ParentID: strp("bd-1"),
|
||||
SortBy: "priority",
|
||||
}
|
||||
richWork := types.WorkFilter{
|
||||
Status: types.Status("open"),
|
||||
Priority: intp(1),
|
||||
Labels: []string{"backlog"},
|
||||
HasMetadataKey: "sprint",
|
||||
MetadataFields: map[string]string{"team": "core"},
|
||||
ParentID: strp("bd-1"),
|
||||
}
|
||||
metaOnly := types.IssueFilter{HasMetadataKey: "sprint", MetadataFields: map[string]string{"team": "core"}}
|
||||
|
||||
var corpus []corpusStmt
|
||||
add := func(name, sqlText string, err error) {
|
||||
if err != nil {
|
||||
t.Fatalf("build corpus %q: %v", name, err)
|
||||
}
|
||||
corpus = append(corpus, corpusStmt{name: name, sql: sqlText})
|
||||
}
|
||||
|
||||
s, err := searchSelectSQL(sqlbuild.IssuesFilterTables, "", types.IssueFilter{})
|
||||
add("search/issues/plain", s, err)
|
||||
s, err = searchSelectSQL(sqlbuild.IssuesFilterTables, "bd-1", richIssue)
|
||||
add("search/issues/rich", s, err)
|
||||
s, err = searchSelectSQL(sqlbuild.WispsFilterTables, "", richIssue)
|
||||
add("search/wisps/rich", s, err)
|
||||
s, err = searchSelectSQL(sqlbuild.IssuesFilterTables, "", metaOnly)
|
||||
add("search/issues/metadata-only", s, err)
|
||||
|
||||
s, err = searchCountsSQL(sqlbuild.IssuesFilterTables, "", types.IssueFilter{}, false)
|
||||
add("counts/issues/plain", s, err)
|
||||
s, err = searchCountsSQL(sqlbuild.IssuesFilterTables, "", richIssue, true)
|
||||
add("counts/issues/rich", s, err)
|
||||
s, err = searchCountsSQL(sqlbuild.WispsFilterTables, "", richIssue, true)
|
||||
add("counts/wisps/rich", s, err)
|
||||
|
||||
s, err = readySelectSQL(sqlbuild.IssuesFilterTables, richWork)
|
||||
add("ready/issues", s, err)
|
||||
s, err = readySelectSQL(sqlbuild.WispsFilterTables, richWork)
|
||||
add("ready/wisps", s, err)
|
||||
s, err = readyCountsSQL(sqlbuild.IssuesFilterTables, richWork, true)
|
||||
add("ready-counts/issues", s, err)
|
||||
s, err = readyCountsSQL(sqlbuild.WispsFilterTables, richWork, true)
|
||||
add("ready-counts/wisps", s, err)
|
||||
|
||||
corpus = append(corpus,
|
||||
corpusStmt{"adaptive-id/instr", adaptiveIDInstrSQL},
|
||||
// Literal-path gate check (issueops/blocked_state.go): keeps the literal
|
||||
// JSON rule covered alongside the dynamic one.
|
||||
corpusStmt{"metadata/literal-gate", `SELECT id FROM issues WHERE JSON_UNQUOTE(JSON_EXTRACT(metadata, '$.gate')) = ?`},
|
||||
// Dep JSON aggregation in isolation (DATE_FORMAT + JSON_OBJECT + CAST).
|
||||
corpusStmt{"dep-json/arrayagg", fmt.Sprintf(`SELECT issue_id, JSON_ARRAYAGG(%s) AS deps_json FROM dependencies GROUP BY issue_id`, sqlbuild.DepJSONObject)},
|
||||
)
|
||||
return corpus
|
||||
}
|
||||
|
||||
// forbiddenResidue are lowercased markers of MySQL constructs that must never
|
||||
// survive translation. JSON_ARRAYAGG is intentionally absent: Postgres 16
|
||||
// supports SQL-standard JSON_ARRAYAGG natively, so the wedge leaves it in place.
|
||||
var forbiddenResidue = []string{
|
||||
"date_format(", "json_object(", "json_unquote(", "json_extract(",
|
||||
" as char", "cast(", "insert ignore", "replace into", "on duplicate",
|
||||
"utc_timestamp", "now()", "`", "?",
|
||||
}
|
||||
|
||||
func assertNoResidue(t *testing.T, name, translated string) {
|
||||
t.Helper()
|
||||
low := strings.ToLower(translated)
|
||||
for _, marker := range forbiddenResidue {
|
||||
if strings.Contains(low, marker) {
|
||||
t.Errorf("%s: untranslated MySQL residue %q survived:\n%s", name, strings.TrimSpace(marker), translated)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestCorpusTranslates runs with no database: it proves every statement family
|
||||
// translates without error and leaves no MySQL residue. The fail-loud property
|
||||
// is a build-time guarantee, so this half of the gate always runs.
|
||||
func TestCorpusTranslates(t *testing.T) {
|
||||
for _, c := range buildCorpus(t) {
|
||||
translated, err := pgdialect.Translate(c.sql)
|
||||
if err != nil {
|
||||
t.Errorf("%s: Translate error: %v\n-- sql:\n%s", c.name, err, c.sql)
|
||||
continue
|
||||
}
|
||||
assertNoResidue(t, c.name, translated)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCorpusPrepare is the live half: PREPARE every translated statement against
|
||||
// a real Postgres so a construct that is syntactically valid but references a
|
||||
// missing column/function/operator still fails loudly. Gated on BEADS_PG_TEST_URL.
|
||||
func TestCorpusPrepare(t *testing.T) {
|
||||
dsn := os.Getenv("BEADS_PG_TEST_URL")
|
||||
if dsn == "" {
|
||||
t.Skip("BEADS_PG_TEST_URL not set; skipping live Postgres PREPARE gate")
|
||||
}
|
||||
ctx := context.Background()
|
||||
schema := fmt.Sprintf("corpus_%d", time.Now().UnixNano())
|
||||
|
||||
db, err := pgdialect.Open(dsn, schema)
|
||||
if err != nil {
|
||||
t.Fatalf("pgdialect.Open: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_, _ = db.ExecContext(context.Background(), fmt.Sprintf(`DROP SCHEMA IF EXISTS %q CASCADE`, schema))
|
||||
_ = db.Close()
|
||||
})
|
||||
if err := createScratchSchema(ctx, db, schema); err != nil {
|
||||
t.Fatalf("create scratch schema: %v", err)
|
||||
}
|
||||
|
||||
for _, c := range buildCorpus(t) {
|
||||
t.Run(c.name, func(t *testing.T) {
|
||||
translated, err := pgdialect.Translate(c.sql)
|
||||
if err != nil {
|
||||
t.Fatalf("Translate error: %v\n-- sql:\n%s", err, c.sql)
|
||||
}
|
||||
assertNoResidue(t, c.name, translated)
|
||||
// PREPARE the RAW statement through the translating driver — the exact
|
||||
// production path (driver.Translate then server Parse).
|
||||
stmt, err := db.PrepareContext(ctx, c.sql)
|
||||
if err != nil {
|
||||
t.Fatalf("PREPARE failed: %v\n-- translated:\n%s", err, translated)
|
||||
}
|
||||
_ = stmt.Close()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// createScratchSchema builds just enough of the wedge schema for the corpus to
|
||||
// PREPARE against: the issues/wisps families (generated from IssueSelectColumns
|
||||
// so they track the projected column list), their label/dependency/comment
|
||||
// side tables, and the bd_mysql_jsonkey helper the dynamic-path rewrite calls
|
||||
// (in production schema.go/InitSchema owns this function).
|
||||
func createScratchSchema(ctx context.Context, db *sql.DB, schema string) error {
|
||||
stmts := []string{
|
||||
fmt.Sprintf(`CREATE SCHEMA IF NOT EXISTS %q`, schema),
|
||||
issueTableDDL("issues"),
|
||||
issueTableDDL("wisps"),
|
||||
`CREATE TABLE leases (issue_id text, holder text, granted_at timestamp(0),
|
||||
lease_expires_at timestamp(0), heartbeat_at timestamp(0))`,
|
||||
`CREATE TABLE labels (issue_id text, label text)`,
|
||||
`CREATE TABLE wisp_labels (issue_id text, label text)`,
|
||||
depTableDDL("dependencies"),
|
||||
depTableDDL("wisp_dependencies"),
|
||||
`CREATE TABLE comments (issue_id text)`,
|
||||
`CREATE TABLE wisp_comments (issue_id text)`,
|
||||
`CREATE OR REPLACE FUNCTION bd_mysql_jsonkey(p text) RETURNS text LANGUAGE sql IMMUTABLE AS $$
|
||||
SELECT CASE WHEN p LIKE '$."%"' THEN substring(p from 4 for length(p)-4) ELSE substring(p from 3) END
|
||||
$$`,
|
||||
}
|
||||
for _, s := range stmts {
|
||||
if _, err := db.ExecContext(ctx, s); err != nil {
|
||||
return fmt.Errorf("exec %q: %w", firstLine(s), err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// issueTableDDL renders a CREATE TABLE for the issues/wisps family from
|
||||
// IssueBaseColumns plus the derived is_blocked column the ready-work WHERE
|
||||
// reads. The lease overlay columns live in the separate leases table
|
||||
// (bd-lrgn1), created explicitly in createScratchSchema. Column types are
|
||||
// faithful where an operator depends on them.
|
||||
func issueTableDDL(table string) string {
|
||||
var defs []string
|
||||
for _, name := range splitColumnList(sqlbuild.IssueBaseColumns) {
|
||||
defs = append(defs, name+" "+pgColumnType(name))
|
||||
}
|
||||
defs = append(defs, "is_blocked smallint NOT NULL DEFAULT 0")
|
||||
return fmt.Sprintf("CREATE TABLE %s (%s)", table, strings.Join(defs, ", "))
|
||||
}
|
||||
|
||||
func depTableDDL(table string) string {
|
||||
return fmt.Sprintf(`CREATE TABLE %s (
|
||||
issue_id text, depends_on_issue_id text, depends_on_wisp_id text,
|
||||
depends_on_external text, type text, created_by text,
|
||||
created_at timestamp(0), thread_id text, metadata jsonb)`, table)
|
||||
}
|
||||
|
||||
func pgColumnType(name string) string {
|
||||
switch name {
|
||||
case "metadata":
|
||||
return "jsonb"
|
||||
case "created_at", "updated_at", "started_at", "closed_at", "compacted_at", "due_at", "defer_until":
|
||||
return "timestamp(0)"
|
||||
case "priority", "estimated_minutes", "compaction_level", "original_size", "timeout_ns":
|
||||
return "integer"
|
||||
case "pinned", "ephemeral", "is_template", "no_history":
|
||||
return "smallint"
|
||||
default:
|
||||
return "text"
|
||||
}
|
||||
}
|
||||
|
||||
// splitColumnList splits a select-column constant into bare column names.
|
||||
func splitColumnList(cols string) []string {
|
||||
raw := strings.NewReplacer("\n", " ", "\t", " ").Replace(cols)
|
||||
var names []string
|
||||
for _, p := range strings.Split(raw, ",") {
|
||||
if n := strings.TrimSpace(p); n != "" {
|
||||
names = append(names, n)
|
||||
}
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func firstLine(s string) string {
|
||||
if i := strings.IndexByte(s, '\n'); i >= 0 {
|
||||
return s[:i]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// TestTranslateNewRules pins the four red-team translator rules (DATE_FORMAT,
|
||||
// JSON_OBJECT comma form, CAST AS CHAR, dynamic ?-path JSON metadata), including
|
||||
// their fail-loud (left-untranslated) branches.
|
||||
func TestTranslateNewRules(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
in string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "DATE_FORMAT ISO8601 -> to_char",
|
||||
in: `SELECT DATE_FORMAT(created_at, '%Y-%m-%dT%H:%i:%sZ') FROM issues`,
|
||||
want: `SELECT to_char(created_at, 'YYYY-MM-DD"T"HH24:MI:SS"Z"') FROM issues`,
|
||||
},
|
||||
{
|
||||
name: "DATE_FORMAT unknown format left loud",
|
||||
in: `SELECT DATE_FORMAT(created_at, '%Y-%m-%d') FROM issues`,
|
||||
want: `SELECT DATE_FORMAT(created_at, '%Y-%m-%d') FROM issues`,
|
||||
},
|
||||
{
|
||||
name: "JSON_OBJECT even -> jsonb_build_object",
|
||||
in: `SELECT JSON_OBJECT('a', x, 'b', y) FROM t`,
|
||||
want: `SELECT jsonb_build_object('a', x, 'b', y) FROM t`,
|
||||
},
|
||||
{
|
||||
name: "JSON_OBJECT odd arg count left loud",
|
||||
in: `SELECT JSON_OBJECT('a', x, 'b') FROM t`,
|
||||
want: `SELECT JSON_OBJECT('a', x, 'b') FROM t`,
|
||||
},
|
||||
{
|
||||
name: "CAST AS CHAR -> ::text",
|
||||
in: `SELECT CAST(metadata AS CHAR) FROM t`,
|
||||
want: `SELECT (metadata)::text FROM t`,
|
||||
},
|
||||
{
|
||||
name: "CAST AS CHAR(n) -> ::text",
|
||||
in: `SELECT CAST(metadata AS CHAR(64)) FROM t`,
|
||||
want: `SELECT (metadata)::text FROM t`,
|
||||
},
|
||||
{
|
||||
name: "CAST AS SIGNED left loud",
|
||||
in: `SELECT CAST(x AS SIGNED) FROM t`,
|
||||
want: `SELECT CAST(x AS SIGNED) FROM t`,
|
||||
},
|
||||
{
|
||||
name: "dynamic has-key JSON_EXTRACT -> -> bd_mysql_jsonkey",
|
||||
in: `SELECT id FROM issues WHERE JSON_EXTRACT(metadata, ?) IS NOT NULL`,
|
||||
want: `SELECT id FROM issues WHERE (metadata -> bd_mysql_jsonkey($1)) IS NOT NULL`,
|
||||
},
|
||||
{
|
||||
name: "dynamic eq JSON_UNQUOTE(JSON_EXTRACT) -> ->> bd_mysql_jsonkey",
|
||||
in: `SELECT id FROM issues WHERE JSON_UNQUOTE(JSON_EXTRACT(metadata, ?)) = ?`,
|
||||
want: `SELECT id FROM issues WHERE (metadata ->> bd_mysql_jsonkey($1)) = $2`,
|
||||
},
|
||||
{
|
||||
name: "literal path rule still applies (regression)",
|
||||
in: `SELECT 1 WHERE JSON_UNQUOTE(JSON_EXTRACT(d.metadata, '$.gate')) = 'any-children'`,
|
||||
want: `SELECT 1 WHERE (d.metadata #>> '{gate}') = 'any-children'`,
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got, err := pgdialect.Translate(tc.in)
|
||||
if err != nil {
|
||||
t.Fatalf("Translate error: %v", err)
|
||||
}
|
||||
if got != tc.want {
|
||||
t.Errorf("mismatch\n in: %s\n got: %s\n want: %s", tc.in, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTranslateDepJSONObject exercises the real counts.go DepJSONObject constant
|
||||
// (DATE_FORMAT + JSON_OBJECT + CAST composed) end to end.
|
||||
func TestTranslateDepJSONObject(t *testing.T) {
|
||||
got, err := pgdialect.Translate(sqlbuild.DepJSONObject)
|
||||
if err != nil {
|
||||
t.Fatalf("Translate error: %v", err)
|
||||
}
|
||||
for _, want := range []string{
|
||||
"jsonb_build_object(",
|
||||
`to_char(created_at, 'YYYY-MM-DD"T"HH24:MI:SS"Z"')`,
|
||||
"(metadata)::text",
|
||||
} {
|
||||
if !strings.Contains(got, want) {
|
||||
t.Errorf("DepJSONObject translation missing %q:\n%s", want, got)
|
||||
}
|
||||
}
|
||||
assertNoResidue(t, "DepJSONObject", got)
|
||||
}
|
||||
|
|
@ -1,121 +0,0 @@
|
|||
package pgdialect
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// kvUserRe matches a libpq keyword/value user token, whose value is either
|
||||
// single-quoted or a run of non-space chars. Like kvPasswordRe (redact.go) it is a
|
||||
// token-level regex, not a full parser, so a ` user=` sequence inside another token's
|
||||
// quoted value could be matched — an exotic case (a keyword-form DSN whose quoted
|
||||
// value contains ` user=`, combined with a username-bearing credential). The
|
||||
// round-trip verify in WithCredential catches the loud failures; the silent case is a
|
||||
// documented sharp edge shared with kvPasswordRe.
|
||||
var kvUserRe = regexp.MustCompile(`(?i)(^|\s)user\s*=\s*(?:'(?:[^'\\]|\\.)*'|\S*)`)
|
||||
|
||||
// kvHasPasswordRe detects a libpq keyword/value connection password token (password=,
|
||||
// not sslpassword=) for the DSN-embedded check in HasPassword.
|
||||
var kvHasPasswordRe = regexp.MustCompile(`(?i)(^|\s)password\s*=`)
|
||||
|
||||
// HasPassword reports whether the connection string itself EMBEDS a password — URL
|
||||
// userinfo, a URL ?password= query param, or a libpq password= token. It deliberately
|
||||
// does NOT report whether pgx would resolve a password from the ambient environment:
|
||||
// pgx.ParseConfig folds PGPASSWORD and ~/.pgpass into cfg.Password, so parsing here
|
||||
// would let a stale PGPASSWORD short-circuit the whole ladder and mask a configured
|
||||
// command or credentials file. An embedded password is the only thing that wins
|
||||
// outright; ambient PGPASSWORD/~/.pgpass remain the driver-native fallback, consulted
|
||||
// at connect only when no ladder rung is configured.
|
||||
func HasPassword(dsn string) bool {
|
||||
if u, err := url.Parse(dsn); err == nil && u.Scheme != "" {
|
||||
if u.User != nil {
|
||||
if _, ok := u.User.Password(); ok {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return u.Query().Get("password") != ""
|
||||
}
|
||||
return kvHasPasswordRe.MatchString(dsn)
|
||||
}
|
||||
|
||||
// WithCredential returns dsn with the resolved credential placed: the password
|
||||
// always, and the username only when non-empty (a Vault-style dynamic user/password
|
||||
// pair) — an empty username leaves the DSN's user untouched. It handles both grammars
|
||||
// pgx accepts (URL userinfo for scheme:// DSNs, single-quoted keyword/value tokens
|
||||
// otherwise) and VERIFIES with the same parser pgx uses that both fields round-tripped,
|
||||
// failing loudly rather than emit a DSN that silently drops or mangles the secret.
|
||||
func WithCredential(dsn, username, password string) (string, error) {
|
||||
var out string
|
||||
if u, err := url.Parse(dsn); err == nil && u.Scheme != "" {
|
||||
// URL form: set the userinfo. url.String() percent-encodes special characters
|
||||
// (an RDS-IAM token's '&'/'='/'/'), which pgx.ParseConfig decodes back — the
|
||||
// verify below proves it.
|
||||
user := username
|
||||
if user == "" && u.User != nil {
|
||||
user = u.User.Username()
|
||||
}
|
||||
u.User = url.UserPassword(user, password)
|
||||
out = u.String()
|
||||
} else {
|
||||
// libpq keyword/value form: drop any existing password token (and, when we are
|
||||
// overriding the user, the user token too), then append single-quoted tokens.
|
||||
stripped := kvPasswordRe.ReplaceAllString(dsn, "$1")
|
||||
if username != "" {
|
||||
stripped = kvUserRe.ReplaceAllString(stripped, "$1")
|
||||
}
|
||||
stripped = strings.TrimSpace(strings.Join(strings.Fields(stripped), " "))
|
||||
if username != "" {
|
||||
stripped += " user='" + escapeKeywordValue(username) + "'"
|
||||
}
|
||||
out = stripped + " password='" + escapeKeywordValue(password) + "'"
|
||||
}
|
||||
|
||||
cfg, err := pgx.ParseConfig(out)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot place credential into the connection string (unparseable result): %w", err)
|
||||
}
|
||||
if cfg.Password != password {
|
||||
return "", fmt.Errorf("password did not round-trip into the connection string; refusing to connect with a wrong or empty password")
|
||||
}
|
||||
if username != "" && cfg.User != username {
|
||||
return "", fmt.Errorf("username did not round-trip into the connection string; refusing to connect as the wrong user")
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// WithPassword places only the password, preserving the DSN's user. It is
|
||||
// WithCredential with no username override. This is the counterpart to
|
||||
// RedactPassword: RedactPassword strips the password for persistence, WithPassword
|
||||
// re-injects the resolved password at open time.
|
||||
func WithPassword(dsn, password string) (string, error) {
|
||||
return WithCredential(dsn, "", password)
|
||||
}
|
||||
|
||||
// HostPort returns the TCP endpoint pgx would connect to for dsn, for keying the
|
||||
// [host:port] credentials-file lookup. ok=false when the DSN does not parse or
|
||||
// resolves to a unix-socket directory (an absolute-path host) — a peer-auth socket
|
||||
// has no place in a password file, and the caller degrades the file rung to
|
||||
// not-configured. For a multi-host DSN this is the primary endpoint only. IPv6 hosts
|
||||
// are returned bare (e.g. "::1"); the credentials-file key format does not bracket
|
||||
// them, so an IPv6 endpoint effectively degrades to not-found in the file.
|
||||
func HostPort(dsn string) (host string, port int, ok bool) {
|
||||
cfg, err := pgx.ParseConfig(dsn)
|
||||
if err != nil {
|
||||
return "", 0, false
|
||||
}
|
||||
if cfg.Host == "" || strings.HasPrefix(cfg.Host, "/") {
|
||||
return "", 0, false
|
||||
}
|
||||
return cfg.Host, int(cfg.Port), true
|
||||
}
|
||||
|
||||
// escapeKeywordValue escapes a value for a single-quoted libpq keyword/value token.
|
||||
func escapeKeywordValue(s string) string {
|
||||
s = strings.ReplaceAll(s, `\`, `\\`)
|
||||
s = strings.ReplaceAll(s, `'`, `\'`)
|
||||
return s
|
||||
}
|
||||
|
|
@ -1,148 +0,0 @@
|
|||
package pgdialect
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// TestMain neutralizes ambient PGPASSWORD/~/.pgpass so redaction and HasPassword tests
|
||||
// are hermetic (pgx.ParseConfig folds them into a connection). The ambient-immunity
|
||||
// tests re-set PGPASSWORD locally via t.Setenv.
|
||||
func TestMain(m *testing.M) {
|
||||
dir, err := os.MkdirTemp("", "beads-pgdialect-test")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
os.Unsetenv("PGPASSWORD")
|
||||
os.Setenv("PGPASSFILE", filepath.Join(dir, "no-pgpass"))
|
||||
code := m.Run()
|
||||
_ = os.RemoveAll(dir)
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func mustUser(t *testing.T, out string) string {
|
||||
t.Helper()
|
||||
cfg, err := pgx.ParseConfig(out)
|
||||
if err != nil {
|
||||
t.Fatalf("pgx.ParseConfig(%q): %v", out, err)
|
||||
}
|
||||
return cfg.User
|
||||
}
|
||||
|
||||
func TestWithCredentialURLForm(t *testing.T) {
|
||||
out, err := WithCredential("postgres://old@h:5432/db", "vu", "pw")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u := mustUser(t, out); u != "vu" {
|
||||
t.Fatalf("user = %q, want vu", u)
|
||||
}
|
||||
if pw := mustPassword(t, out); pw != "pw" {
|
||||
t.Fatalf("password = %q, want pw", pw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithCredentialURLEmptyUsernameKeepsUser(t *testing.T) {
|
||||
out, err := WithCredential("postgres://old@h:5432/db", "", "pw")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u := mustUser(t, out); u != "old" {
|
||||
t.Fatalf("user = %q, want old (preserved)", u)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithCredentialKeywordForm(t *testing.T) {
|
||||
out, err := WithCredential("host=h user=old dbname=db", "vu", "pw")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u := mustUser(t, out); u != "vu" {
|
||||
t.Fatalf("user = %q, want vu (old stripped)", u)
|
||||
}
|
||||
if pw := mustPassword(t, out); pw != "pw" {
|
||||
t.Fatalf("password = %q, want pw", pw)
|
||||
}
|
||||
}
|
||||
|
||||
// WithCredential's own round-trip verify guards escaping; a user and password full of
|
||||
// quotes/backslashes must survive the keyword grammar.
|
||||
func TestWithCredentialKeywordEscaping(t *testing.T) {
|
||||
wantU, wantP := `v'u\x`, `p'w\y`
|
||||
out, err := WithCredential("host=h dbname=db", wantU, wantP)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u := mustUser(t, out); u != wantU {
|
||||
t.Fatalf("user = %q, want %q", u, wantU)
|
||||
}
|
||||
if pw := mustPassword(t, out); pw != wantP {
|
||||
t.Fatalf("password = %q, want %q", pw, wantP)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithCredentialURLSpecialCharsPassword(t *testing.T) {
|
||||
tok := "p@ss:w/rd&x=1"
|
||||
out, err := WithCredential("postgres://h:5432/db", "dynuser", tok)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if u := mustUser(t, out); u != "dynuser" {
|
||||
t.Fatalf("user = %q, want dynuser", u)
|
||||
}
|
||||
if pw := mustPassword(t, out); pw != tok {
|
||||
t.Fatalf("password = %q, want %q", pw, tok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostPort(t *testing.T) {
|
||||
cases := []struct {
|
||||
dsn string
|
||||
host string
|
||||
port int
|
||||
ok bool
|
||||
}{
|
||||
{"postgres://u@127.0.0.1:6543/db", "127.0.0.1", 6543, true},
|
||||
{"postgres://u@db.corp/db", "db.corp", 5432, true}, // pgx defaults the port to 5432
|
||||
{"host=db.corp user=u", "db.corp", 5432, true}, // keyword form
|
||||
{"host=/var/run/postgresql user=u", "", 0, false}, // unix-socket dir -> not ok
|
||||
}
|
||||
for _, c := range cases {
|
||||
h, p, ok := HostPort(c.dsn)
|
||||
if ok != c.ok || (ok && (h != c.host || p != c.port)) {
|
||||
t.Errorf("HostPort(%q) = (%q,%d,%v), want (%q,%d,%v)", c.dsn, h, p, ok, c.host, c.port, c.ok)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// HasPassword must reflect only an embedded password, not one pgx would resolve from
|
||||
// ambient PGPASSWORD/~/.pgpass — otherwise a stale PGPASSWORD would short-circuit the
|
||||
// whole ladder.
|
||||
func TestHasPasswordIgnoresAmbientPGPASSWORD(t *testing.T) {
|
||||
t.Setenv("PGPASSWORD", "from-env")
|
||||
if HasPassword("postgres://bts@127.0.0.1:5432/db") {
|
||||
t.Fatal("HasPassword must not report a password from ambient PGPASSWORD")
|
||||
}
|
||||
if !HasPassword("postgres://bts:embedded@127.0.0.1:5432/db") {
|
||||
t.Fatal("HasPassword must detect an embedded URL password")
|
||||
}
|
||||
if !HasPassword("postgres://bts@127.0.0.1:5432/db?password=q") {
|
||||
t.Fatal("HasPassword must detect a ?password= query param")
|
||||
}
|
||||
}
|
||||
|
||||
// RedactPassword must not be fooled by an ambient PGPASSWORD (which pgx would fold in
|
||||
// but which is never persisted) into refusing a password-less --pg-url at init.
|
||||
func TestRedactPasswordIgnoresAmbientPGPASSWORD(t *testing.T) {
|
||||
t.Setenv("PGPASSWORD", "from-env")
|
||||
got, err := RedactPassword("postgres://bts@127.0.0.1:5432/db")
|
||||
if err != nil {
|
||||
t.Fatalf("RedactPassword must not be fooled by ambient PGPASSWORD: %v", err)
|
||||
}
|
||||
if HasPassword(got) {
|
||||
t.Fatalf("redacted DSN still carries an embedded password: %q", got)
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue