store

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 12 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 store is the metering engine's database seam: the pgx connection pool and the migrator that applies the embedded engine chain.

Opening a Store does not dial the database. The connection is established on the first query, so a subcommand that only parses its flags fails on the flags rather than on the database. The engine never changes the schema outside the migrate subcommands of its CLI: a schema change is an operator's decision. EnsureConsoleRole is the one other place that runs DDL: its CREATE ROLE, ALTER ROLE and GRANT provision the login role the console reads this database as, and change no table.

The static queries in queries.sql are compiled by sqlc into the sqlcgen subpackage, which takes the pool as its DBTX. `make generate` rewrites that package, and it is committed so that a plain `go build` needs no generator.

The normative specification is roadmap/03-phase-3-metering-rating.md, WP 3.1.

Index

Constants

View Source
const (
	// ConsoleRole is the login role the console reads the engine database as.
	ConsoleRole = "tally_console"
	// ConsoleReaderRole is the group role engine migration 3 creates and grants
	// SELECT on the engine tables. ConsoleRole reads through its membership.
	ConsoleReaderRole = "tally_console_reader"
)

Variables

This section is empty.

Functions

func Migrate

func Migrate(ctx context.Context, dbURL string) ([]int64, error)

Migrate applies every pending migration of the embedded engine chain and returns the versions it applied, oldest first. An up-to-date database yields an empty slice and no error.

Only the engine CLI and the integration tests call this. No other subcommand runs DDL as a side effect: a schema change is an operator's decision, not something a scheduled run brings along.

func MigrateDownTo

func MigrateDownTo(ctx context.Context, dbURL string, version int64) ([]int64, error)

MigrateDownTo rolls the chain back to version, applying the down migration of everything above it, and returns the versions it rolled back. Passing 0 leaves an empty schema.

It is the counterpart of Migrate: the chain ships down migrations, and a rollback nobody can run is a rollback that does not exist.

Types

type MigrationState

type MigrationState = dbmigrate.MigrationState

MigrationState is one migration of the chain and whether the database carries it. It is what answers "which schema is this database on" before code that assumes a newer one is run against it.

func MigrationStatus

func MigrationStatus(ctx context.Context, dbURL string) ([]MigrationState, error)

MigrationStatus reports every migration of the embedded chain against the database, oldest first.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store owns the connection pool the engine reads and writes through.

func New

func New(ctx context.Context, dbURL string) (*Store, error)

New parses dbURL and prepares the pool for it. Connections are established lazily, so a database that is down does not keep the process from starting.

func (*Store) CheckSchema

func (s *Store) CheckSchema(ctx context.Context) error

CheckSchema refuses a database that carries less of the migration chain than this build was compiled against. It is what every subcommand runs before its first query, so that a deployment whose migrate step lagged behind the image fails on the cause rather than on whichever column the missing migration was going to add, halfway through a run.

A database without this chain's bookkeeping is a database this chain never ran against, which is version 0 and reported as such. The table carries the engine's own name, so a connection string that reaches another service's database — the reporting one, a copy-paste away in a deployment that carries both — reads 0 here rather than that chain's position, which is a number the comparison below has no meaning against. Every other failed read is reported as it came back: a read that did not answer is not a pass, and a gate that lets one through is a gate that disables itself on the deployment it exists for. The relation is schema-qualified for the reason migration 0001 pins the trigger function's search_path.

func (*Store) Close

func (s *Store) Close()

Close releases every connection the pool holds and waits for in-flight ones to be returned.

func (*Store) EnsureConsoleRole added in v0.8.0

func (s *Store) EnsureConsoleRole(ctx context.Context, password string) (created bool, err error)

EnsureConsoleRole creates ConsoleRole as a member of ConsoleReaderRole, or sets its password when it is there already, and reports whether it created the role. The connection has to be a superuser, or hold CREATEROLE and ADMIN OPTION on both roles: PostgreSQL 16 lets a role alter, and grant membership in, only the roles it administers. A role with CREATEROLE administers the roles it creates itself, so it needs the option granted only on a role someone else created.

A role that is there already gets a login only when it is what this function creates: no attribute such as SUPERUSER or CREATEDB, no membership but the one in ConsoleReaderRole, and no member but the connection's own role. ConsoleReaderRole has to be what migration 3 creates: no attribute, no login and no membership, since ConsoleRole inherits its memberships and takes on its attributes through SET ROLE, and no member but the connection's own role and ConsoleRole, the latter without ADMIN OPTION. A login as the reader role or another member of either reads what the console reads, and one holding ADMIN OPTION can make any role such a member. Neither role may own an object in any database on the server: ConsoleRole holds the owner's rights on what either owns, writing and dropping included. Anything else is refused before the password is set. The call itself sets LOGIN, VALID UNTIL 'infinity' and a membership that inherits, so a role made NOLOGIN, NOINHERIT or with an expiry works afterwards.

Every call sets the password, so the deployment's secret is what decides it, and a password someone set by hand is overwritten by the next call. The password reaches the server as a SCRAM-SHA-256 verifier with a fresh salt, so a server that logs DDL logs the verifier and never the cleartext. The caller holds the password to printable ASCII without spaces, which is what lets the verifier skip SASLprep.

The statements run one by one and outside a transaction. A call that stops after the CREATE ROLE, a refusal included, leaves a role without login and password, one that stops after the ALTER ROLE a role that connects and reads nothing, and the next call completes either. Two calls at the same moment can fail the ALTER ROLE with "tuple concurrently updated", and calling again succeeds.

No error carries the password or the verifier: a *pgconn.PgError holds no statement text, and this function adds none.

func (*Store) Pool

func (s *Store) Pool() *pgxpool.Pool

Pool returns the underlying pool, which is what the generated sqlc queries take as their DBTX.

Directories

Path Synopsis
Package storetest hands integration tests a plain PostgreSQL 16 container with the engine migration chain applied.
Package storetest hands integration tests a plain PostgreSQL 16 container with the engine migration chain applied.

Jump to

Keyboard shortcuts

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