Files
db-sqlite/config.go

29 lines
1.1 KiB
Go
Raw Normal View History

feat(db-sqlite): initial implementation — database/sql SQLite with lifecycle and UnitOfWork (v1.0.0) Introduces code.nochebuena.dev/einherjar/db-sqlite — the SQLite database starter for the Einherjar framework. Absorbs the sqlite package from micro-lib using the pure-Go modernc.org/sqlite driver (no CGO, cross-compilation compatible). Interfaces (CT-6: one TypeSpec per file): - Executor — ExecContext, QueryContext, QueryRowContext (database/sql types) - Tx — Executor + Commit(), Rollback() — no ctx; honest database/sql contract - Provider — GetExecutor, Begin, Ping, HandleError - Component — lifecycle.Component + observability.Checkable + Provider - UnitOfWork — Do(ctx, fn) Implementation: - New(logger, cfg) Component — pool not created until OnInit - OnInit: sql.Open with DSN (Path + Pragmas); sets MaxOpenConns, MaxIdleConns - OnStart: Ping with 5s timeout; logs "sqlite: connected" - OnStop: db.Close(); logs "sqlite: closing pool" - GetExecutor: returns active Tx from context (ctxTxKey) or *sql.DB; explicit nil return when db uninitialized to avoid typed-nil interface pitfall - Begin: wraps db.BeginTx; wrapped in xerrors on error - HealthCheck: delegates to Ping; Priority LevelDegraded (file DB; absence is non-fatal on a server restart — file always present if configured) - NewUnitOfWork(logger, provider) UnitOfWork — acquires writeMu before Begin to prevent SQLITE_BUSY under concurrent goroutines (SQLite single-writer model) - writeMu: extracted from *sqliteImpl via type assertion in NewUnitOfWork; gracefully skipped when provider is a mock (nil mu) - HandleError: sql.ErrNoRows→ErrNotFound, unique/pk constraint→ErrAlreadyExists, foreign key→ErrInvalidInput, all others→ErrInternal Config (EINHERJAR_SQLITE_* env vars): Path(required), MaxOpenConns(1), MaxIdleConns(1), Pragmas(?_journal=WAL&_timeout=5000&_fk=true) - Component interface embeds observability.Identifiable; identifiable.go implements ModulePath and ModuleVersion via runtime/debug.ReadBuildInfo() — prints in launcher banner
2026-05-29 15:56:45 +00:00
package sqlite
// Config holds SQLite connection settings. Path is required; all other fields
// have production-safe defaults via [DefaultConfig].
type Config struct {
// Path is the SQLite file path. Use ":memory:" for in-memory databases.
Path string `env:"EINHERJAR_SQLITE_PATH,required"`
MaxOpenConns int `env:"EINHERJAR_SQLITE_MAX_OPEN_CONNS" envDefault:"1"`
MaxIdleConns int `env:"EINHERJAR_SQLITE_MAX_IDLE_CONNS" envDefault:"1"`
// Pragmas are appended to the DSN as query parameters.
// Default enables WAL journal mode, 5-second busy timeout, and FK enforcement.
Pragmas string `env:"EINHERJAR_SQLITE_PRAGMAS" envDefault:"?_journal=WAL&_timeout=5000&_fk=true"`
}
// DefaultConfig returns a Config with all optional fields set to production-safe
// defaults. Callers must supply Path.
func DefaultConfig() Config {
return Config{
MaxOpenConns: 1,
MaxIdleConns: 1,
Pragmas: "?_journal=WAL&_timeout=5000&_fk=true",
}
}
// DSN constructs the SQLite connection string from the configuration.
func (c Config) DSN() string {
return c.Path + c.Pragmas
}