Documentation
¶
Overview ¶
Package sqldb holds the small helpers shared by every lesson in the SQL track: opening a SQLite database with sensible settings and loading the sample schema + data.
It is deliberately tiny. In a real service this code lives in an `internal/db` or `internal/store` package and also owns pool settings, health checks and error mapping.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DSN ¶
DSN builds a modernc.org/sqlite connection string for a file path.
Pragmas are applied to EVERY new connection the pool opens, which matters because:
- foreign_keys is OFF by default in SQLite and is a per-connection setting. Without the pragma in the DSN, only the first connection would enforce FKs and bugs would appear randomly.
- journal_mode(WAL) lets readers and one writer work at the same time.
- busy_timeout(5000) makes a blocked writer wait up to 5s instead of failing instantly with "database is locked".
IN-MEMORY GOTCHA: with ":memory:" every connection in the pool gets its OWN private empty database. Tables created on one connection are invisible on the next one, giving confusing "no such table" errors. Fix: db.SetMaxOpenConns(1), or (what we do) use a temp file.
func Open ¶
Open opens (creating if needed) the SQLite database at path and verifies it is reachable. sql.Open alone never touches the database; PingContext forces a real connection so that configuration mistakes fail here, at startup, rather than on the first query.
func OpenTemp ¶
OpenTemp creates a fresh database file in a new temp directory, loads schema + seed, and returns the handle plus a cleanup function. Every lesson uses this so runs are repeatable: the previous run's data can never leak into the next one.
func Schema ¶
Schema applies only the schema (no sample rows). Useful for tests that insert their own data.
func Setup ¶
Setup creates the tables and inserts the sample data (sql/schema.sql then sql/seed.sql). database/sql lets a single Exec contain many statements with this driver, which is convenient for scripts. Do not rely on that with other drivers (pgx wants one per call unless you use its simple protocol).
Types ¶
This section is empty.