Documentation
¶
Overview ¶
Package sqlite provides SQLite connection management and schema migration utilities for hamr-scaffolded projects.
It uses the pure-Go modernc.org/sqlite driver so projects compile without CGO. The package mirrors pkg/db's API shape (ConnectContext, Migrate, ...) with simpler semantics appropriate for a local-file database: no retries, no pool tuning, WAL + foreign-keys + busy-timeout pragmas by default.
Index ¶
- func Connect(path string, opts ...ConnectOption) (*sqlx.DB, error)
- func ConnectContext(ctx context.Context, path string, opts ...ConnectOption) (*sqlx.DB, error)
- func Migrate(db *sqlx.DB, cfg MigrateConfig) error
- func MigrateDown(db *sqlx.DB, cfg MigrateConfig) error
- func MigrateForce(db *sqlx.DB, cfg MigrateConfig, version int) error
- func MigrateSteps(db *sqlx.DB, cfg MigrateConfig, n int) error
- func MigrateVersion(db *sqlx.DB, cfg MigrateConfig) (version uint, dirty bool, err error)
- type ConnectConfig
- type ConnectOption
- type MigrateConfig
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Connect ¶
func Connect(path string, opts ...ConnectOption) (*sqlx.DB, error)
Connect opens a SQLite database file using context.Background().
func ConnectContext ¶
ConnectContext opens a SQLite database file, ensuring the parent directory exists and applying the configured pragmas on every connection. It then validates connectivity via PingContext.
func Migrate ¶
func Migrate(db *sqlx.DB, cfg MigrateConfig) error
Migrate runs all up migrations from the embedded filesystem. migrate.ErrNoChange is silently ignored.
func MigrateDown ¶
func MigrateDown(db *sqlx.DB, cfg MigrateConfig) error
MigrateDown rolls back all migrations.
func MigrateForce ¶
func MigrateForce(db *sqlx.DB, cfg MigrateConfig, version int) error
MigrateForce sets the migration version without running any migrations. Use this to fix a dirty migration state.
func MigrateSteps ¶
func MigrateSteps(db *sqlx.DB, cfg MigrateConfig, n int) error
MigrateSteps runs n migration steps. Positive n migrates up, negative migrates down.
func MigrateVersion ¶
MigrateVersion returns the current migration version and dirty flag.
Types ¶
type ConnectConfig ¶
type ConnectConfig struct {
// JournalMode sets PRAGMA journal_mode. Default "WAL".
JournalMode string
// ForeignKeys toggles PRAGMA foreign_keys. Default true.
ForeignKeys bool
// BusyTimeout sets PRAGMA busy_timeout. Default 5s.
BusyTimeout time.Duration
// MaxOpenConns caps the connection pool. Default 1 — SQLite is
// single-writer, so higher values only help read-heavy workloads on WAL.
MaxOpenConns int
}
ConnectConfig holds connection parameters.
type ConnectOption ¶
type ConnectOption func(*ConnectConfig)
ConnectOption configures a ConnectConfig.
func WithBusyTimeout ¶
func WithBusyTimeout(d time.Duration) ConnectOption
WithBusyTimeout overrides PRAGMA busy_timeout.
func WithForeignKeys ¶
func WithForeignKeys(enabled bool) ConnectOption
WithForeignKeys toggles PRAGMA foreign_keys.
func WithJournalMode ¶
func WithJournalMode(mode string) ConnectOption
WithJournalMode overrides PRAGMA journal_mode.
func WithMaxOpenConns ¶
func WithMaxOpenConns(n int) ConnectOption
WithMaxOpenConns overrides the connection pool cap.
type MigrateConfig ¶
MigrateConfig configures the migration runner.