Documentation
¶
Overview ¶
Package database opens and bounds the connection pool, and hides the two drivers behind one surface: PostgreSQL (github.com/jackc/pgx/v5, driven through database/sql by its stdlib shim — the production source of truth) and SQLite (modernc.org/sqlite, the local test stack).
It carries the one difference that would otherwise leak into every repository: unique-violation detection, so a racing insert becomes a 409 rather than a 500. Both drivers are pure Go, so CGO_ENABLED=0 builds work — which is what lets .goreleaser.yaml cross-compile four platforms from one runner and the Dockerfile produce a distroless image.
There is deliberately no large-document binding helper here. A settings tree binds as an ordinary Go string straight into a text column at any size, so the repositories pass documents as plain strings and there is no size-specific wrapper type to remember.
sqlite.go additionally defines the local schema and seed data by hand. Nothing keeps that in step with migrations/, and because every test runs against SQLite, a divergence shows up as a green build.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsUniqueViolation ¶
IsUniqueViolation reports whether err is the driver's unique-constraint violation, for either backend. The repositories check the natural key before inserting, but two concurrent creates both pass that check and one of them then hits the constraint; without this the loser gets a 500 where the pre-check would have produced a 409.
The Postgres side matches on SQLSTATE and not on the message. Postgres renders "duplicate key value violates unique constraint …" in whatever lc_messages the server is configured with and names the constraint inside it, so a message match would break on a non-English server and again on the first constraint rename — silently, and in the direction that costs a 500.
func NewPostgresDBWithPool ¶
func NewPostgresDBWithPool(dsn string, pool PoolOptions) (*sql.DB, error)
func NewSQLiteDB ¶
NewSQLiteDB opens the local test-stack database. It is NOT a production path: it exists so the whole JetStream distribution flow (write-through, watch, reconcile) can be exercised without a Postgres instance. Postgres is the source of truth in a deployed setup; see DB_DRIVER in app config.
dsn is a file path, or ":memory:" for an ephemeral database.
Types ¶
type PoolOptions ¶
type PoolOptions struct {
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime time.Duration
ConnMaxIdleTime time.Duration
}
PoolOptions bounds the connection pool. A zero field takes the default above.
func PoolOptionsFromEnv ¶
func PoolOptionsFromEnv() PoolOptions
PoolOptionsFromEnv reads the pool limits from the environment. They are read here rather than carried on the application Config because the pool is this package's concern and nothing above it has an opinion on the numbers.