Documentation
¶
Overview ¶
Package db opens the Postgres pool and runs schema migrations (goose for ReputeCast's own tables, rivermigrate for River's job tables).
Index ¶
- Constants
- func AfterCommit(tx pgx.Tx, fn func()) bool
- func Configure(cfg *pgxpool.Config)
- func EnsureEventPartitions(ctx context.Context, pool *pgxpool.Pool, now time.Time) ([]string, error)
- func EscapeLike(s string) string
- func EventsPartitioned(ctx context.Context, q interface{ ... }) (bool, error)
- func HasPending(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger) (bool, error)
- func MigrateDown(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger) error
- func MigrateUp(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger) error
- func Open(ctx context.Context, url string, maxConns int32) (*pgxpool.Pool, error)
- func SetUpSegmentsRole(ctx context.Context, pool *pgxpool.Pool) error
- func Tx(ctx context.Context, pool *pgxpool.Pool, fn func(pgx.Tx) error) (err error)
- type EventPartition
- type MigrationStatus
- type PartitionResult
- type SchemaState
Constants ¶
const EventPartitionsAhead = 2
EventPartitionsAhead is how many months past the current one always have a partition.
const UpgradeSteps = "op11-events-partitioned"
UpgradeSteps names the steps MigrateUp takes after the schema's migrations; a test database made before they changed is made again.
Variables ¶
This section is empty.
Functions ¶
func AfterCommit ¶
AfterCommit runs fn once the transaction tx belongs to has committed, and reports true, when that transaction is a Tx; otherwise it reports false and fn doesn't run, so the caller does the work its old way. fn doesn't run when the transaction rolls back, but does when only a savepoint it was handed in rolls back: use it for work that is harmless if nothing changed (waking a queue), not for effects of the transaction's writes. Work that holds a lock until commit, such as inserting a unique job, belongs here instead (review 2026-11 OP-13).
func Configure ¶
Configure sets up a pool's connections the way ReputeCast uses them; Open does it, and so do the test databases (internal/testdb).
JIT is off: Postgres compiles a plan whose estimated cost passes jit_above_cost with JIT, again on every execution, and ReputeCast's queries are short (counting a 48,000-contact audience for Review & send spent 0.5 s compiling and 0.1 s running). It is set once connected rather than in the startup packet, which poolers such as PgBouncer refuse for settings they don't know. UUIDs travel as bytes (registerUUID).
func EnsureEventPartitions ¶
func EnsureEventPartitions(ctx context.Context, pool *pgxpool.Pool, now time.Time) ([]string, error)
EnsureEventPartitions makes the partitions of the current month and the EventPartitionsAhead months after it, when delivery_events is partitioned. It returns the partitions it made.
func EscapeLike ¶
EscapeLike makes s match itself in a LIKE pattern: a search for "50%" or "first_name" finds those characters, not any text.
func EventsPartitioned ¶
func EventsPartitioned(ctx context.Context, q interface { QueryRow(context.Context, string, ...any) pgx.Row }) (bool, error)
EventsPartitioned reports whether delivery_events is partitioned.
func HasPending ¶
HasPending reports whether any goose migration is not yet applied.
func MigrateDown ¶
MigrateDown rolls back the latest goose migration, holding the migration lock.
func MigrateUp ¶
MigrateUp applies all pending goose migrations, then River's migrations, holding the migration lock throughout.
After the schema's migrations it does the upgrade steps a goose migration can't (review 2026-11 OP-11): delivery_events becomes monthly partitions when `reputecast migrate partition-events` hasn't made it so already, and the unpartitioned table's unique dedupe index goes.
func SetUpSegmentsRole ¶
SetUpSegmentsRole runs again what migrations 00008 and 00151 do for SQL segments: create the reputecast_segments role when the app's user may, grant it to the app's user, and own the function segments run through. pg_dump doesn't carry roles, so a database restored on a new server needs it once the role exists there (docs/deploy/backup-restore.md).
Types ¶
type EventPartition ¶
EventPartition is one monthly partition.
func EventPartitions ¶
EventPartitions lists the monthly partitions, oldest first (not the legacy and default ones).
type MigrationStatus ¶
MigrationStatus describes one goose migration.
type PartitionResult ¶
type PartitionResult struct {
Already bool
KeysCopied int64
FirstMonth time.Time
Partitions []string
Took time.Duration
}
PartitionResult says what the conversion did.
func PartitionDeliveryEvents ¶
func PartitionDeliveryEvents(ctx context.Context, pool *pgxpool.Pool, log *slog.Logger, now time.Time) (PartitionResult, error)
PartitionDeliveryEvents converts delivery_events to monthly partitions without copying its rows. It holds the migration lock, can run while ReputeCast runs (the final swap takes the table's lock for a moment), and can run again after an interruption.
type SchemaState ¶
type SchemaState struct {
// Expected is the newest migration this binary carries; Applied the
// newest the database has.
Expected, Applied int64
// Pending are this binary's migrations the database doesn't have yet.
Pending []int64
// Newer is true when the database has migrations this binary doesn't
// know: a newer release migrated it.
Newer bool
// RiverCurrent is true when the job queue's own migrations are applied.
RiverCurrent bool
}
SchemaState compares the database's schema with the migrations this binary carries (review 2026-11 OP-2, OP-3).
func Schema ¶
Schema reads the schema's state. It is one small query per table, cheap enough for a readiness probe.
func (SchemaState) Current ¶
func (s SchemaState) Current() bool
Current reports whether every migration this binary needs is applied.