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 ConsentGiven(ctx context.Context, db Querier, profileID string) (bool, error)
- func CreateAccount(ctx context.Context, db Beginner, uid string, codeSHA256 []byte) (string, error)
- func CreateInvite(ctx context.Context, db Querier, codeSHA256 []byte, tariff string) error
- 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 MigrateFiles(ctx context.Context, url string, files fs.FS) (applied int, version int64, err error)
- func Open(ctx context.Context, url string) (*pgxpool.Pool, error)
- func ProfileOf(ctx context.Context, db Querier, accountID, profileID string) (bool, error)
- func TouchAccount(ctx context.Context, db Querier, accountID string) error
- func WriteUnderAccount(ctx context.Context, db Beginner, accountID string, fn func(pgx.Tx) error) error
- type Account
- type Beginner
- type ChildConsent
- type Profile
- type Querier
- type TxBeginner
- type View
Constants ¶
This section is empty.
Variables ¶
var ( // ErrAccountExists is a user who has an account already. ErrAccountExists = errors.New("store: the account exists") // ErrCodeNotAccepted is a code that is not there, or was redeemed. ErrCodeNotAccepted = errors.New("store: the code is not accepted") )
The refusals of the creation of an account (R04; contract, CreateAccount).
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 ConsentGiven ¶ added in v0.1.4
ConsentGiven says whether the profile's consent is confirmed: what a learning request of E3 asks before it does anything (architecture 3.1, step 5; П-1). A profile has one consent at most, and a consent that waits has no profile.
func CreateAccount ¶ added in v0.1.5
CreateAccount creates the account of the sign-in service's UID by the code of codeSHA256, with the owner's statement that they are 18 or older dated now, and returns its ID (R04; architecture 3.1, steps 2 and 3). It is one transaction, in the order of the contract: an account the UID has already is ErrAccountExists — or ErrAccountDeleting, when it is marked — before the code is looked at, so that a repeat is not refused for the code it redeemed; then a code not there or redeemed is ErrCodeNotAccepted, and no account is left. The code is bound to the account, and its tariff passes to it.
Two creations race safely. Two by one UID: the second waits on the UID's unique index and finds the first's account. Two for one code: the second waits on the code's row and finds it redeemed. Either way, a first one that rolls back leaves the second to go on.
func CreateInvite ¶ added in v0.1.5
CreateInvite keeps a new invite code by its SHA-256 alone, with the tariff it passes to the account that redeems it (R04, R11).
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 of api it has not had, and returns how many it applied and the version the schema is at (R29).
func MigrateFiles ¶ added in v0.1.2
func MigrateFiles(ctx context.Context, url string, files fs.FS) (applied int, version int64, err error)
MigrateFiles applies the migrations in files — api's, or OpenFGA's for the rights (R31) — to the database at url that it has not had. 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, but one its file says may not.
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 ProfileOf ¶ added in v0.1.4
ProfileOf says whether the account has the profile. It is read after the right to the profile, never before (architecture 3.1): a tuple whose profile is gone — dangling, R21 — gives a right to nothing.
func TouchAccount ¶ added in v0.1.4
TouchAccount writes today, by UTC, as the date of the account's last request, once a day at most: the date the account is deleted by after 12 months with no request (R05, R33). A plain UPDATE takes FOR NO KEY UPDATE, which the writes under the account, by FOR KEY SHARE, do not wait for.
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 ¶
type Account ¶ added in v0.1.4
type Account struct {
// ID is the account's own ID, the subject of the rights.
ID string
// Deleting says the account is marked "deleting" (R21).
Deleting bool
// LastRequestOn is the date, by UTC, of the account's last request; zero if
// none was written yet.
LastRequestOn time.Time
}
Account is what a request knows of the account of its token (R04).
func AccountByUID ¶ added in v0.1.4
AccountByUID is the account of the sign-in service's UID, or ErrNoAccount.
type ChildConsent ¶ added in v0.1.5
type ChildConsent struct {
ID string
AgeCategory string
Country string
// State is "waiting" or "confirmed".
State string
}
ChildConsent is a consent for a child that waits for its profile.
type Profile ¶ added in v0.1.5
type Profile struct {
ID string
Owner bool
Pseudonym string
Avatar string
AgeCategory string
Country string
VoicingEnabled bool
VoicingSpeed string
DefaultMode string
InterfaceLanguage *string
Grade *int16
AskBeforeShowing bool
}
Profile is a profile's row, with the contract's codes as the schema keeps them.
type Querier ¶ added in v0.1.4
type Querier interface {
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
}
Querier is what a read runs on: a pool, a connection or a transaction.
type TxBeginner ¶ added in v0.1.5
TxBeginner is what a transaction with its options starts from: a pool, or a connection.
type View ¶ added in v0.1.5
type View struct {
// ID is the account's ID.
ID string
// PinSet says the PIN of the section for adults is set.
PinSet bool
// Profiles are the account's profiles: the owner's first, then by when they
// were created.
Profiles []Profile
// ChildConsents are the consents for a child that have no profile, by when
// they were asked for.
ChildConsents []ChildConsent
}
View is the account as GetAccount gives it (contract, Account): whether its PIN is set, its profiles, and its consents for a child with no profile yet.
func AccountView ¶ added in v0.1.5
AccountView reads the account in one snapshot, so that its profiles and its consents are of one moment: a device erases the profiles the list does not hold (contract, GetAccount). An account that is not there is ErrNoAccount, and one marked "deleting" ErrAccountDeleting.
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). |