feat: add global beads_global database for shared-server mode
Adds a project-agnostic beads_global database to shared-server mode so clients can track issues globally across all projects on the server. - Add EnsureGlobalDatabase() to create the DB during shared server init - Add --global persistent flag to switch CLI commands to the global DB - Add global_dolt_database field to metadata.json for connection info - Seed global DB with issue_prefix="global" and sentinel project ID - Export/import default to global-issues.jsonl with --global flag - --global errors when not in shared-server mode (additive, no breakage) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
95bfbad43c
commit
fa87dd2bdd
8 changed files with 257 additions and 6 deletions
|
|
@ -78,7 +78,11 @@ func maybeAutoExport(ctx context.Context) {
|
|||
// Determine output path
|
||||
exportPath := config.GetString("export.path")
|
||||
if exportPath == "" {
|
||||
exportPath = "issues.jsonl"
|
||||
if globalFlag {
|
||||
exportPath = "global-issues.jsonl"
|
||||
} else {
|
||||
exportPath = "issues.jsonl"
|
||||
}
|
||||
}
|
||||
fullPath := filepath.Join(beadsDir, exportPath)
|
||||
|
||||
|
|
|
|||
|
|
@ -50,12 +50,16 @@ func runImport(cmd *cobra.Command, args []string) error {
|
|||
if len(args) > 0 {
|
||||
jsonlPath = args[0]
|
||||
} else {
|
||||
// Default: .beads/issues.jsonl
|
||||
// Default: .beads/issues.jsonl (or .beads/global-issues.jsonl with --global)
|
||||
beadsDir := beads.FindBeadsDir()
|
||||
if beadsDir == "" {
|
||||
return fmt.Errorf("%s — %s", activeWorkspaceNotFoundError(), diagHint())
|
||||
}
|
||||
jsonlPath = filepath.Join(beadsDir, "issues.jsonl")
|
||||
if globalFlag {
|
||||
jsonlPath = filepath.Join(beadsDir, "global-issues.jsonl")
|
||||
} else {
|
||||
jsonlPath = filepath.Join(beadsDir, "issues.jsonl")
|
||||
}
|
||||
}
|
||||
|
||||
// Check file exists
|
||||
|
|
|
|||
|
|
@ -587,6 +587,27 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the global beads_global database exists on the shared server.
|
||||
// This is idempotent — safe to run on every init.
|
||||
globalHost := configfile.DefaultDoltServerHost
|
||||
if serverHost != "" {
|
||||
globalHost = serverHost
|
||||
}
|
||||
globalPort := initPort
|
||||
if globalPort == 0 {
|
||||
globalPort = doltserver.DefaultSharedServerPort
|
||||
}
|
||||
globalUser := configfile.DefaultDoltServerUser
|
||||
if serverUser != "" {
|
||||
globalUser = serverUser
|
||||
}
|
||||
if err := doltserver.EnsureGlobalDatabase(globalHost, globalPort, globalUser, ""); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to create global database: %v\n", err)
|
||||
// Non-fatal — project init should succeed even if global DB creation fails
|
||||
} else if !quiet {
|
||||
fmt.Printf(" %s Global database %s available\n", ui.RenderPass("✓"), doltserver.GlobalDatabaseName)
|
||||
}
|
||||
}
|
||||
|
||||
store, err := newDoltStore(ctx, doltCfg, embeddeddolt.WithLock(initLock))
|
||||
|
|
@ -595,6 +616,13 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Initialize global database schema and config in shared-server mode.
|
||||
// Opens a separate store connection to beads_global with CreateIfMissing
|
||||
// to trigger schema migration, then seeds the issue prefix and project ID.
|
||||
if sharedServer || doltserver.IsSharedServerMode() {
|
||||
initGlobalDatabaseConfig(ctx, doltCfg, quiet)
|
||||
}
|
||||
|
||||
// Configure the remote in the Dolt store so bd dolt push/pull
|
||||
// work immediately after bootstrap. Also add the remote when
|
||||
// sync.remote is configured but bootstrap was skipped (DB already
|
||||
|
|
@ -723,6 +751,12 @@ Non-interactive mode (--non-interactive or BD_NON_INTERACTIVE=1):
|
|||
cfg.DoltDatabase = strings.ReplaceAll(cfg.DoltDatabase, ".", "_")
|
||||
}
|
||||
|
||||
// Set global database name for shared-server mode projects.
|
||||
// This gives each project the connection info to reach beads_global.
|
||||
if sharedServer || doltserver.IsSharedServerMode() {
|
||||
cfg.GlobalDoltDatabase = doltserver.GlobalDatabaseName
|
||||
}
|
||||
|
||||
// Persist the connection mode matching this build.
|
||||
if isEmbeddedMode() {
|
||||
cfg.DoltMode = configfile.DoltModeEmbedded
|
||||
|
|
@ -1677,3 +1711,49 @@ func verifyMetadata(ctx context.Context, store storage.DoltStorage, key, value s
|
|||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// initGlobalDatabaseConfig opens a store connection to the beads_global database
|
||||
// and seeds its configuration (issue prefix, project ID). The database must already
|
||||
// exist (created by EnsureGlobalDatabase). This function is idempotent — it only
|
||||
// sets config values that are not already present.
|
||||
func initGlobalDatabaseConfig(ctx context.Context, projectCfg *dolt.Config, quiet bool) {
|
||||
globalCfg := &dolt.Config{
|
||||
Path: projectCfg.Path,
|
||||
BeadsDir: projectCfg.BeadsDir,
|
||||
Database: doltserver.GlobalDatabaseName,
|
||||
ServerHost: projectCfg.ServerHost,
|
||||
ServerPort: projectCfg.ServerPort,
|
||||
ServerUser: projectCfg.ServerUser,
|
||||
ServerPassword: projectCfg.ServerPassword,
|
||||
ServerMode: true,
|
||||
CreateIfMissing: true,
|
||||
AutoStart: false, // server is already running
|
||||
}
|
||||
|
||||
globalStore, err := newDoltStore(ctx, globalCfg)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to open global database: %v\n", err)
|
||||
return
|
||||
}
|
||||
defer func() { _ = globalStore.Close() }()
|
||||
|
||||
// Set issue prefix (only if not already configured)
|
||||
existing, _ := globalStore.GetConfig(ctx, "issue_prefix")
|
||||
if existing == "" {
|
||||
if err := globalStore.SetConfig(ctx, "issue_prefix", doltserver.GlobalIssuePrefix); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to set global issue prefix: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Set well-known project ID for the global database
|
||||
existingID, _ := globalStore.GetMetadata(ctx, "_project_id")
|
||||
if existingID == "" {
|
||||
if err := globalStore.SetMetadata(ctx, "_project_id", doltserver.GlobalProjectID); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Warning: failed to set global project ID: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
if !quiet {
|
||||
fmt.Printf(" %s Global database schema initialized\n", ui.RenderPass("✓"))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ var (
|
|||
)
|
||||
var (
|
||||
sandboxMode bool
|
||||
globalFlag bool // Use the global shared-server database (beads_global)
|
||||
serverMode bool // True when using external dolt sql-server (dolt_mode=server)
|
||||
readonlyMode bool // Read-only mode: block write operations (for worker sandboxes)
|
||||
storeIsReadOnly bool // Track if store was opened read-only (for staleness checks)
|
||||
|
|
@ -398,6 +399,7 @@ func init() {
|
|||
_ = rootCmd.PersistentFlags().MarkHidden("format") // Hidden alias for CLI ergonomics
|
||||
rootCmd.PersistentFlags().BoolVar(&sandboxMode, "sandbox", false, "Sandbox mode: disables auto-sync")
|
||||
rootCmd.PersistentFlags().BoolVar(&readonlyMode, "readonly", false, "Read-only mode: block write operations (for worker sandboxes)")
|
||||
rootCmd.PersistentFlags().BoolVar(&globalFlag, "global", false, "Use the global shared-server database (beads_global)")
|
||||
rootCmd.PersistentFlags().StringVar(&doltAutoCommit, "dolt-auto-commit", "", "Dolt auto-commit policy (off|on|batch). 'on': commit after each write. 'batch': defer commits to bd dolt commit; uncommitted changes persist in the working set until then. SIGTERM/SIGHUP flush pending batch commits. Default: off. Override via config key dolt.auto-commit")
|
||||
rootCmd.PersistentFlags().BoolVar(&profileEnabled, "profile", false, "Generate CPU profile for performance analysis")
|
||||
rootCmd.PersistentFlags().BoolVarP(&verboseFlag, "verbose", "v", false, "Enable verbose/debug output")
|
||||
|
|
@ -766,6 +768,15 @@ var rootCmd = &cobra.Command{
|
|||
}
|
||||
doltCfg.SyncRemote = resolveSyncRemote()
|
||||
|
||||
// --global flag: switch to the global shared-server database.
|
||||
// Must be in shared-server mode; errors otherwise.
|
||||
if globalFlag {
|
||||
if !doltserver.IsSharedServerMode() {
|
||||
FatalError("--global requires shared-server mode (set BEADS_DOLT_SHARED_SERVER=1 or dolt.shared-server: true in config.yaml)")
|
||||
}
|
||||
doltCfg.Database = doltserver.GlobalDatabaseName
|
||||
}
|
||||
|
||||
// Keep standalone CLI auto-start behavior centralized so doctor and
|
||||
// other helper paths stay in lockstep with the main command path.
|
||||
dolt.ApplyCLIAutoStart(beadsDir, doltCfg)
|
||||
|
|
@ -809,13 +820,15 @@ var rootCmd = &cobra.Command{
|
|||
// Skip auto-import when the user is explicitly running "bd import" —
|
||||
// the import command handles JSONL files itself and auto-importing
|
||||
// first would interfere (double-import / upsert confusion).
|
||||
if store != nil && !useReadOnly && cmd.Name() != "import" {
|
||||
if store != nil && !useReadOnly && !globalFlag && cmd.Name() != "import" {
|
||||
maybeAutoImportJSONL(rootCtx, store, beadsDir)
|
||||
}
|
||||
|
||||
// Validate workspace identity for write commands (GH#2438, GH#2372)
|
||||
// Skip for read-only commands since they can't corrupt data
|
||||
if !useReadOnly && os.Getenv("BEADS_SKIP_IDENTITY_CHECK") != "1" {
|
||||
// Skip for read-only commands since they can't corrupt data.
|
||||
// Skip for --global: the global database uses a sentinel project ID
|
||||
// that won't match any project's metadata.json.
|
||||
if !useReadOnly && !globalFlag && os.Getenv("BEADS_SKIP_IDENTITY_CHECK") != "1" {
|
||||
validateWorkspaceIdentity(rootCtx, beadsDir)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -38,6 +38,11 @@ type Config struct {
|
|||
// to the wrong Dolt server (GH#2372).
|
||||
ProjectID string `json:"project_id,omitempty"`
|
||||
|
||||
// GlobalDoltDatabase is the SQL database name for the project-agnostic
|
||||
// global issue database in shared-server mode. Set during bd init when
|
||||
// shared-server mode is active. Empty means no global database available.
|
||||
GlobalDoltDatabase string `json:"global_dolt_database,omitempty"`
|
||||
|
||||
// Stale closed issues check configuration
|
||||
// 0 = disabled (default), positive = threshold in days
|
||||
StaleClosedIssuesDays int `json:"stale_closed_issues_days,omitempty"`
|
||||
|
|
@ -317,6 +322,12 @@ func (c *Config) GetDoltDatabase() string {
|
|||
return DefaultDoltDatabase
|
||||
}
|
||||
|
||||
// GetGlobalDoltDatabase returns the global database name for shared-server mode.
|
||||
// Returns empty string if no global database is configured.
|
||||
func (c *Config) GetGlobalDoltDatabase() string {
|
||||
return c.GlobalDoltDatabase
|
||||
}
|
||||
|
||||
// GetDoltServerPassword returns the Dolt server password.
|
||||
// Checks in order:
|
||||
// 1. BEADS_DOLT_PASSWORD env var (highest priority, existing behavior)
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package configfile
|
|||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
|
@ -577,3 +578,58 @@ func TestIsDoltServerMode_NoEnvRespectsMetadata(t *testing.T) {
|
|||
t.Error("IsDoltServerMode() = true with no env overrides + embedded metadata, want false")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlobalDoltDatabase_RoundTrip(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0750); err != nil {
|
||||
t.Fatalf("failed to create .beads directory: %v", err)
|
||||
}
|
||||
|
||||
cfg := DefaultConfig()
|
||||
cfg.GlobalDoltDatabase = "beads_global"
|
||||
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("Save() failed: %v", err)
|
||||
}
|
||||
|
||||
loaded, err := Load(beadsDir)
|
||||
if err != nil {
|
||||
t.Fatalf("Load() failed: %v", err)
|
||||
}
|
||||
if loaded.GlobalDoltDatabase != "beads_global" {
|
||||
t.Errorf("GlobalDoltDatabase = %q, want %q", loaded.GlobalDoltDatabase, "beads_global")
|
||||
}
|
||||
if loaded.GetGlobalDoltDatabase() != "beads_global" {
|
||||
t.Errorf("GetGlobalDoltDatabase() = %q, want %q", loaded.GetGlobalDoltDatabase(), "beads_global")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlobalDoltDatabase_EmptyByDefault(t *testing.T) {
|
||||
cfg := DefaultConfig()
|
||||
if cfg.GetGlobalDoltDatabase() != "" {
|
||||
t.Errorf("GetGlobalDoltDatabase() = %q, want empty string for default config", cfg.GetGlobalDoltDatabase())
|
||||
}
|
||||
}
|
||||
|
||||
func TestGlobalDoltDatabase_OmittedFromJSON(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
beadsDir := filepath.Join(tmpDir, ".beads")
|
||||
if err := os.MkdirAll(beadsDir, 0750); err != nil {
|
||||
t.Fatalf("failed to create .beads directory: %v", err)
|
||||
}
|
||||
|
||||
cfg := DefaultConfig()
|
||||
if err := cfg.Save(beadsDir); err != nil {
|
||||
t.Fatalf("Save() failed: %v", err)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(filepath.Join(beadsDir, ConfigFileName))
|
||||
if err != nil {
|
||||
t.Fatalf("ReadFile() failed: %v", err)
|
||||
}
|
||||
|
||||
if strings.Contains(string(data), "global_dolt_database") {
|
||||
t.Error("global_dolt_database should be omitted from JSON when empty")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -88,6 +88,18 @@ const maxEphemeralPortAttempts = 10
|
|||
// Uses 3308 to avoid conflict with the orchestrator which uses 3307.
|
||||
const DefaultSharedServerPort = 3308
|
||||
|
||||
// GlobalDatabaseName is the SQL database name for the project-agnostic
|
||||
// global issue database in shared-server mode.
|
||||
const GlobalDatabaseName = "beads_global"
|
||||
|
||||
// GlobalIssuePrefix is the issue prefix used in the global database.
|
||||
const GlobalIssuePrefix = "global"
|
||||
|
||||
// GlobalProjectID is the well-known sentinel UUID for the global database.
|
||||
// Used for project identity verification — the global DB doesn't belong to
|
||||
// any single project, so it uses this fixed value instead of a random UUID.
|
||||
const GlobalProjectID = "00000000-0000-0000-0000-000000000000"
|
||||
|
||||
// IsSharedServerMode returns true if shared server mode is enabled.
|
||||
// Checks (in priority order):
|
||||
// 1. BEADS_DOLT_SHARED_SERVER env var ("1" or "true")
|
||||
|
|
@ -831,6 +843,48 @@ func Start(beadsDir string) (*State, error) {
|
|||
}, nil
|
||||
}
|
||||
|
||||
// EnsureGlobalDatabase connects to the shared Dolt server and creates the
|
||||
// beads_global database if it doesn't already exist. This is idempotent and
|
||||
// safe to call on every shared server init. Schema initialization and config
|
||||
// seeding (issue prefix, project ID) are handled by the store layer when the
|
||||
// global database is first opened with CreateIfMissing=true.
|
||||
//
|
||||
// Returns nil if the database already exists or was successfully created.
|
||||
func EnsureGlobalDatabase(host string, port int, user, password string) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
dsn := doltutil.ServerDSN{
|
||||
Host: host,
|
||||
Port: port,
|
||||
User: user,
|
||||
Password: password,
|
||||
}.String()
|
||||
db, err := sql.Open("mysql", dsn)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ensure global db: failed to open connection: %w", err)
|
||||
}
|
||||
defer db.Close()
|
||||
db.SetMaxOpenConns(1)
|
||||
db.SetConnMaxLifetime(10 * time.Second)
|
||||
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
return fmt.Errorf("ensure global db: server not reachable: %w", err)
|
||||
}
|
||||
|
||||
// CREATE DATABASE IF NOT EXISTS is idempotent — safe on every call.
|
||||
// GlobalDatabaseName is a constant ("beads_global"), not user input.
|
||||
_, err = db.ExecContext(ctx, fmt.Sprintf("CREATE DATABASE IF NOT EXISTS `%s`", GlobalDatabaseName)) //nolint:gosec // G201: constant database name
|
||||
if err != nil {
|
||||
errLower := strings.ToLower(err.Error())
|
||||
if !strings.Contains(errLower, "database exists") && !strings.Contains(errLower, "1007") {
|
||||
return fmt.Errorf("ensure global db: failed to create %s: %w", GlobalDatabaseName, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// FlushWorkingSet connects to the running Dolt server and commits any uncommitted
|
||||
// working set changes across all databases. This prevents data loss when the server
|
||||
// is about to be stopped or restarted. Returns nil if there's nothing to flush or
|
||||
|
|
|
|||
|
|
@ -1774,3 +1774,32 @@ func firstOrEmpty(s []string) string {
|
|||
}
|
||||
return s[0]
|
||||
}
|
||||
|
||||
func TestGlobalDatabaseConstants(t *testing.T) {
|
||||
if GlobalDatabaseName == "" {
|
||||
t.Error("GlobalDatabaseName must not be empty")
|
||||
}
|
||||
if GlobalDatabaseName != "beads_global" {
|
||||
t.Errorf("GlobalDatabaseName = %q, want %q", GlobalDatabaseName, "beads_global")
|
||||
}
|
||||
if GlobalIssuePrefix == "" {
|
||||
t.Error("GlobalIssuePrefix must not be empty")
|
||||
}
|
||||
if GlobalIssuePrefix != "global" {
|
||||
t.Errorf("GlobalIssuePrefix = %q, want %q", GlobalIssuePrefix, "global")
|
||||
}
|
||||
if GlobalProjectID == "" {
|
||||
t.Error("GlobalProjectID must not be empty")
|
||||
}
|
||||
if GlobalProjectID != "00000000-0000-0000-0000-000000000000" {
|
||||
t.Errorf("GlobalProjectID = %q, want sentinel UUID", GlobalProjectID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestEnsureGlobalDatabase_ServerNotReachable(t *testing.T) {
|
||||
// EnsureGlobalDatabase should return an error when the server is not reachable.
|
||||
err := EnsureGlobalDatabase("127.0.0.1", 19999, "root", "")
|
||||
if err == nil {
|
||||
t.Error("expected error when server is not reachable")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue