db

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0

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

View Source
const EventPartitionsAhead = 2

EventPartitionsAhead is how many months past the current one always have a partition.

View Source
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

func AfterCommit(tx pgx.Tx, fn func()) bool

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

func Configure(cfg *pgxpool.Config)

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

func EscapeLike(s string) string

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

func HasPending(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger) (bool, error)

HasPending reports whether any goose migration is not yet applied.

func MigrateDown

func MigrateDown(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger) error

MigrateDown rolls back the latest goose migration, holding the migration lock.

func MigrateUp

func MigrateUp(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger) error

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 Open

func Open(ctx context.Context, url string, maxConns int32) (*pgxpool.Pool, error)

Open creates a connection pool and verifies the database is reachable.

func SetUpSegmentsRole

func SetUpSegmentsRole(ctx context.Context, pool *pgxpool.Pool) error

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).

func Tx

func Tx(ctx context.Context, pool *pgxpool.Pool, fn func(pgx.Tx) error) (err error)

Tx runs fn inside a transaction, committing on success and rolling back on error or panic. What fn hands to AfterCommit runs once the commit is done.

Types

type EventPartition

type EventPartition struct {
	Name       string
	From, Till time.Time
}

EventPartition is one monthly partition.

func EventPartitions

func EventPartitions(ctx context.Context, pool *pgxpool.Pool) ([]EventPartition, error)

EventPartitions lists the monthly partitions, oldest first (not the legacy and default ones).

type MigrationStatus

type MigrationStatus struct {
	Version   int64
	Name      string
	Applied   bool
	AppliedAt time.Time
}

MigrationStatus describes one goose migration.

func Status

func Status(ctx context.Context, pool *pgxpool.Pool, logger *slog.Logger) ([]MigrationStatus, error)

Status lists goose migrations and whether they are applied.

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

func Schema(ctx context.Context, pool *pgxpool.Pool) (SchemaState, error)

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.

Jump to

Keyboard shortcuts

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