Documentation
¶
Overview ¶
Package dbaccess is the Postgres data-access layer abstraction (NFR-ARC-2): a typed query layer (pgx + sqlc) and versioned migrations (goose) behind a small Config, instead of raw provider-specific calls scattered across services. See ../README.md for a worked example of switching endpoints, and ./sqlc for the sample migration + typed queries this package ships as a reference for future services to pattern-match.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Connect ¶
Connect builds a pgx connection pool from cfg and fails fast: it pings the database before returning, so misconfiguration surfaces immediately rather than on the first query.
func Migrate ¶
Migrate applies all pending "up" migrations found in migrations (an os.DirFS or embed.FS rooted at a migrations directory) against dsn.
It goes through database/sql (not pgxpool) because that's what goose's Provider expects; the app's own queries still go through the pgxpool pool returned by Connect.
func MigrationsFS ¶
MigrationsFS returns this package's own demo migrations (see ./sqlc), rooted at the migrations directory as goose.NewProvider expects. It exists so the integration test — and any caller wanting to see the pattern in action — doesn't need to know this package's internal layout.
func UnscopedTables ¶
func UnscopedTables(ctx context.Context, pool *pgxpool.Pool, schema string, exemptTables ...string) ([]string, error)
UnscopedTables inspects every base table in schema and returns the ones that do NOT carry an organization_id column, excluding exemptTables (the documented tenancy exceptions — ADR-0002 "tenancy exception": a global identity table, or the organizations table itself, which IS the tenant root rather than something owned BY a tenant) and goose's own version table (never domain data).
This is the automated form of FR-TEN-2 / #30's AC "every owned row carries an organization_id": rather than a one-time manual audit, a service's own test suite can call this against its migrated schema so a future migration that adds an owned table without organization_id fails CI immediately, instead of depending on someone remembering to check (data-model.md §5).
Types ¶
type Config ¶
type Config struct {
Host string
Port string // defaults to "5432" if empty
User string
Password string
Database string
SSLMode string // e.g. "require", "disable"; defaults to "require"
// SearchPath, when set, is applied as the connection's schema search path.
// A least-privilege per-service role (schema-per-service, D-6) has no rights
// on `public`, so this points it at its own schema — where its tables live
// and where goose creates its version table.
SearchPath string
}
Config holds the connection details for a Postgres database. Populate it from environment/config/secrets — never hardcode credentials.