Documentation
¶
Overview ¶
Package store is api's database: its schema, the migrations that make it, the pool requests take their connections from, and the transaction a write under an account runs in (R29).
Index ¶
- Variables
- func MarkDeleting(ctx context.Context, tx pgx.Tx, accountID string) error
- func Migrate(ctx context.Context, url string) (applied int, version int64, err error)
- func Open(ctx context.Context, url string) (*pgxpool.Pool, error)
- func WriteUnderAccount(ctx context.Context, db Beginner, accountID string, fn func(pgx.Tx) error) error
- type Beginner
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoAccount is a write under an account that is not there. ErrNoAccount = errors.New("store: no account") // ErrAccountDeleting is a write under an account marked "deleting" (R21). ErrAccountDeleting = errors.New("store: the account is being deleted") )
The refusals of a write under an account.
Functions ¶
func MarkDeleting ¶
MarkDeleting takes the account's row FOR UPDATE in tx and marks it "deleting", the first step of the deletion of an account (R21). FOR UPDATE is what waits for every write that holds the row by FOR KEY SHARE: a plain UPDATE of the mark takes FOR NO KEY UPDATE, which does not conflict with FOR KEY SHARE, and would let a write that does not touch the row commit after the mark. The deletion clears the account's data in the same tx, after this.
func Migrate ¶
Migrate applies to the database at url every migration it has not had, and returns how many it applied and the version the schema is at. url is the direct connection: goose holds an advisory lock of the session while it migrates, so that two runs never migrate at once, and Neon's pooler keeps no session (R29). Each migration runs in a transaction of its own.
func Open ¶
Open is a pool of connections to the database at url. The pool takes them as it needs them, and pgx keeps its prepared statements by the protocol, which Neon's pooler keeps too (R29).
func WriteUnderAccount ¶
func WriteUnderAccount(ctx context.Context, db Beginner, accountID string, fn func(pgx.Tx) error) error
WriteUnderAccount runs fn in a transaction that holds the account's row by FOR KEY SHARE and has found it not marked "deleting" (R21). The deletion of an account takes the row FOR UPDATE, so a write that got here before the mark commits before the mark is set, and one after it sees the mark and writes nothing. FOR KEY SHARE, rather than FOR SHARE, lets fn itself update fields of the row — the date of the request, the PIN's count — without locking out a neighbouring request of the same account. The transaction commits when fn returns nil, and rolls back on its error, which it returns as it is.
Types ¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package storetest runs the tests of api's database on a real PostgreSQL with no Docker, as the devcontainer and CI have none: embedded-postgres starts one server for a test binary, when its first test asks for a database, migrates a template database once, and gives each test a database of its own made from it, which is a copy and takes milliseconds (R29; amendment of 2026-11-02 to R05).
|
Package storetest runs the tests of api's database on a real PostgreSQL with no Docker, as the devcontainer and CI have none: embedded-postgres starts one server for a test binary, when its first test asks for a database, migrates a template database once, and gives each test a database of its own made from it, which is a copy and takes milliseconds (R29; amendment of 2026-11-02 to R05). |