db

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package db provides unified SQLite database management for bc CLI.

This package consolidates SQLite connection management, ensuring consistent configuration across all database operations. It provides:

  • Connection pooling optimized for SQLite's single-writer model
  • Consistent pragma settings for WAL mode and performance
  • Automatic directory creation for database files
  • Graceful shutdown handling

Usage

db, err := db.Open("/path/to/database.db")
if err != nil {
    return err
}
defer db.Close()

Configuration

All connections use these settings:

  • WAL journal mode for better concurrency
  • Foreign keys enabled
  • 30 second busy timeout
  • Single connection pool (SQLite limitation)
  • Optimized cache and synchronous settings

Index

Constants

View Source
const DefaultBusyTimeout = 30000 // milliseconds

DefaultBusyTimeout is the default timeout for SQLite busy handling. Set to 30s to handle concurrent agent access; SQLite returns as soon as the lock is available — this is just the worst-case upper bound.

View Source
const DefaultCacheSize = 2000

DefaultCacheSize is the default SQLite page cache size in KB.

View Source
const DefaultPostgresDSN = "postgres://bc:bc@localhost:5432/bc"

DefaultPostgresDSN is the connection string for the bcdb (TimescaleDB) container.

Variables

This section is empty.

Functions

func BCDBPath

func BCDBPath(workspaceRoot string) string

BCDBPath returns the path to the unified bc database for a workspace.

func CloseShared

func CloseShared() error

CloseShared closes the shared connection.

func DefaultPassword

func DefaultPassword() string

DefaultPassword returns the database password from BC_DB_PASSWORD env var, falling back to "bc" for local development with a warning log. Production deployments should always set BC_DB_PASSWORD.

func IsPostgresEnabled

func IsPostgresEnabled() bool

IsPostgresEnabled returns true if DATABASE_URL is set, indicating Postgres should be used.

func OpenPostgres

func OpenPostgres(dsn string) (*sql.DB, error)

OpenPostgres opens a connection pool to Postgres using the given DSN. The DSN should be a postgres:// URL (e.g. postgres://bc:bc@localhost:5432/bc).

func OpenWorkspaceDB

func OpenWorkspaceDB(workspaceRoot string) (*sql.DB, string, error)

OpenWorkspaceDB opens the workspace database based on configuration. Priority: DATABASE_URL env var > settings.json storage config > SQLite default.

func OpenWorkspaceDBWithConfig

func OpenWorkspaceDBWithConfig(workspaceRoot string, cfg *StorageSettings) (*sql.DB, string, error)

OpenWorkspaceDBWithConfig opens the workspace database using settings.json config. If DATABASE_URL env var is set, it takes priority (for Docker/CI). Otherwise, settings.json storage.default determines the backend.

func PostgresDSN

func PostgresDSN() string

PostgresDSN returns the Postgres connection string from DATABASE_URL env var, or the default bcdb DSN if not set.

func SetShared

func SetShared(db *sql.DB, driver string)

SetShared sets the shared workspace database connection. Call once at startup (in cmd/bcd or CLI init).

func Shared

func Shared() *sql.DB

Shared returns the shared workspace database connection. Returns nil if not set (stores should fall back to opening their own).

func SharedDriver

func SharedDriver() string

SharedDriver returns "sqlite" or "timescale".

func TryOpenPostgres

func TryOpenPostgres() (*sql.DB, error)

TryOpenPostgres attempts to open a Postgres connection. Returns (nil, nil) if DATABASE_URL is not set (Postgres not configured). Returns (nil, err) only when DATABASE_URL is set but connection fails.

Types

type Config

type Config struct {
	// BusyTimeout is the SQLite busy timeout in milliseconds.
	// Default: 30000 (30 seconds)
	BusyTimeout int

	// CacheSize is the SQLite page cache size in KB.
	// Default: 2000 (2MB)
	CacheSize int

	// ReadOnly opens the database in read-only mode.
	ReadOnly bool
}

Config holds database configuration options.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns the default database configuration.

type DB

type DB struct {
	*sql.DB
	// contains filtered or unexported fields
}

DB wraps a sql.DB with bc-specific functionality.

func Open

func Open(path string) (*DB, error)

Open opens a SQLite database at the given path with default configuration. The directory containing the database file is created if it doesn't exist.

func OpenWithConfig

func OpenWithConfig(path string, cfg Config) (*DB, error)

OpenWithConfig opens a SQLite database with custom configuration.

func SharedWrapped

func SharedWrapped() *DB

SharedWrapped returns the shared database as a *DB wrapper. Returns nil if no shared connection is set.

func (*DB) Path

func (d *DB) Path() string

Path returns the database file path.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages multiple named database connections. Use this when you need to share connections across packages.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new database registry.

func (*Registry) Close

func (r *Registry) Close() error

Close closes all open database connections.

func (*Registry) CloseOne

func (r *Registry) CloseOne(name string) error

CloseOne closes a specific database connection by name.

func (*Registry) Get

func (r *Registry) Get(name string) (*DB, error)

Get returns a database connection by name, opening it if needed.

func (*Registry) Register

func (r *Registry) Register(name, path string)

Register registers a database path with a name. The database is not opened until Get is called.

type SQLiteSettings

type SQLiteSettings struct {
	Path string // base directory for bc.db (default: workspace .bc/ dir)
}

SQLiteSettings configures the SQLite database path.

type StorageSettings

type StorageSettings struct {
	Default   string // "sqlite" or "timescale"
	SQLite    SQLiteSettings
	Timescale TimescaleSettings
}

StorageSettings holds the storage configuration from settings.json. Used by OpenWorkspaceDB to determine the database backend.

type TimescaleSettings

type TimescaleSettings struct {
	Host     string
	User     string
	Password string
	Database string
	Port     int
}

TimescaleSettings configures the TimescaleDB (Postgres) connection.

func (TimescaleSettings) DSN

func (p TimescaleSettings) DSN() string

DSN builds a Postgres connection string from config fields.

Jump to

Keyboard shortcuts

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