doltserver

package
v0.62.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package doltserver manages the lifecycle of a local dolt sql-server process. It provides transparent auto-start so that `bd init` and `bd <command>` work without manual server management.

Port assignment uses OS-assigned ephemeral ports by default. When no explicit port is configured (env var, config.yaml, metadata.json), Start() asks the OS for a free port via net.Listen(":0"), passes it to dolt sql-server, and writes the actual port to dolt-server.port. This eliminates the birthday-problem collisions that plagued the old hash-derived port scheme (GH#2098, GH#2372).

Users with explicit port config via BEADS_DOLT_SERVER_PORT env var or config.yaml always use that port instead, with conflict detection via reclaimPort.

Server state files (PID, port, log, lock) live in the .beads/ directory.

Index

Constants

View Source
const DefaultSharedServerPort = 3308

DefaultSharedServerPort is the default port for shared server mode. Uses 3308 to avoid conflict with Gas Town which uses 3307.

Variables

This section is empty.

Functions

func EnsurePortFile

func EnsurePortFile(beadsDir string, port int) error

EnsurePortFile makes the repo-local port file match the connected server port. This is a best-effort repair path for upgraded repos that are missing .beads/dolt-server.port even though commands can still connect.

func EnsureRunning

func EnsureRunning(beadsDir string) (int, error)

EnsureRunning starts the server if it is not already running. This is the main auto-start entry point. Thread-safe via file lock. Returns the port the server is listening on.

func FlushWorkingSet

func FlushWorkingSet(host string, port int) error

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 if the server is not reachable (best-effort).

func IsPreV56DoltDir

func IsPreV56DoltDir(doltDir string) bool

IsPreV56DoltDir returns true if doltDir contains a .dolt/ directory that was NOT created by bd 0.56+ (missing .bd-dolt-ok marker). These databases were created by the old embedded Dolt mode and may be incompatible. Used by doctor checks to detect potentially problematic dolt databases.

func IsSharedServerMode

func IsSharedServerMode() bool

IsSharedServerMode returns true if shared server mode is enabled. Checks (in priority order):

  1. BEADS_DOLT_SHARED_SERVER env var ("1" or "true")
  2. dolt.shared-server in config.yaml

Shared server mode means all projects on this machine share a single dolt sql-server process at SharedServerDir(), each using its own database (already unique via prefix-based naming in bd init).

func KillStaleServers

func KillStaleServers(beadsDir string) ([]int, error)

KillStaleServers finds and kills orphan dolt sql-server processes not tracked by the canonical PID file. Returns the PIDs of killed processes.

func LogPath

func LogPath(beadsDir string) string

LogPath returns the path to the server log file.

func ReadPortFile

func ReadPortFile(beadsDir string) int

ReadPortFile returns the port from the project's dolt-server.port file, or 0 if the file doesn't exist or is invalid. Exported for use by bd init to detect whether this project has its own running server (GH#2336).

func RecoverPreV56DoltDir

func RecoverPreV56DoltDir(doltDir string) (bool, error)

RecoverPreV56DoltDir removes and reinitializes a dolt database that was created by a pre-0.56 bd version. Call this during version upgrade detection (e.g., from autoMigrateOnVersionBump when previousVersion < 0.56).

Pre-0.56 databases used embedded Dolt mode with a different Dolt library version that may produce nil DoltDB values, causing panics (GH#2137). The data is unrecoverable — the fix is to start fresh.

Returns true if recovery was performed, false if not needed.

func ResolveDoltDir

func ResolveDoltDir(beadsDir string) string

ResolveDoltDir returns the dolt data directory for the given beadsDir. It checks the BEADS_DOLT_DATA_DIR env var and metadata.json for a custom dolt_data_dir, falling back to the default .beads/dolt/ path.

Note: we check for metadata.json existence before calling configfile.Load to avoid triggering the config.json → metadata.json migration side effect, which would create files in the .beads/ directory unexpectedly.

func ResolveServerDir

func ResolveServerDir(beadsDir string) string

ResolveServerDir is the exported version of resolveServerDir. CLI commands use this to resolve the server directory before calling Start, Stop, or IsRunning.

func SharedDoltDir

func SharedDoltDir() (string, error)

SharedDoltDir returns the dolt data directory for the shared server. Returns ~/.beads/shared-server/dolt/ (created on first use).

func SharedServerDir

func SharedServerDir() (string, error)

SharedServerDir returns the directory for shared server state files. Returns ~/.beads/shared-server/ (created on first use).

func Stop

func Stop(beadsDir string) error

Stop gracefully stops the managed server and its idle monitor.

func StopWithForce

func StopWithForce(beadsDir string, force bool) error

StopWithForce is like Stop but with an optional force flag.

Types

type Config

type Config struct {
	BeadsDir string // Path to .beads/ directory
	Port     int    // MySQL protocol port (0 = allocate ephemeral port on Start)
	Host     string // Bind address (default: 127.0.0.1)
}

Config holds the server configuration.

func DefaultConfig

func DefaultConfig(beadsDir string) *Config

DefaultConfig returns config with sensible defaults. Priority: env var > port file > config.yaml / global config > metadata.json. Returns port 0 when no source provides a port, meaning Start() should allocate an ephemeral port from the OS.

The port file (dolt-server.port) is written by Start() with the actual port the server is listening on. Consulting it here ensures that commands connecting to an already-running server use the correct port.

type State

type State struct {
	Running bool   `json:"running"`
	PID     int    `json:"pid"`
	Port    int    `json:"port"`
	DataDir string `json:"data_dir"`
}

State holds runtime information about a managed server.

func IsRunning

func IsRunning(beadsDir string) (*State, error)

IsRunning checks if a managed server is running for this beadsDir. Returns a State with Running=true if a valid dolt process is found.

func Start

func Start(beadsDir string) (*State, error)

Start explicitly starts a dolt sql-server for the project. Returns the State of the started server, or an error.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL