sqldb

package
v0.0.0-...-610d9af Latest Latest
Warning

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

Go to latest
Published: Nov 23, 2026 License: GPL-3.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

func DSN(path string) string

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

func Open(ctx context.Context, path string) (*sql.DB, error)

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

func OpenTemp(ctx context.Context) (*sql.DB, func(), error)

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

func Schema(ctx context.Context, db *sql.DB) error

Schema applies only the schema (no sample rows). Useful for tests that insert their own data.

func Setup

func Setup(ctx context.Context, db *sql.DB) error

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.

Jump to

Keyboard shortcuts

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