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
This commit is contained in:
2026-05-29 15:56:45 +00:00
commit 0acf4da97e
20 changed files with 2041 additions and 0 deletions

22
go.mod Normal file
View File

@@ -0,0 +1,22 @@
module code.nochebuena.dev/einherjar/db-sqlite
go 1.26
require (
code.nochebuena.dev/einherjar/contracts v1.0.0
code.nochebuena.dev/einherjar/core v1.0.0
modernc.org/sqlite v1.37.1
)
require (
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 // indirect
golang.org/x/sys v0.39.0 // indirect
modernc.org/libc v1.65.7 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.11.0 // indirect
)