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 ¶
- Constants
- Variables
- func AllowCountry(ctx context.Context, db Querier, country string) (bool, error)
- func CanonicalID(id string) bool
- func ClearAccount(ctx context.Context, tx pgx.Tx, accountID string) error
- func CloseAbandonedAttempts(ctx context.Context, db Querier, before, at time.Time) (int64, error)
- func ConnectModule(ctx context.Context, db Querier, profileID, moduleID string) error
- func ConsentGiven(ctx context.Context, db Querier, profileID string) (bool, error)
- func Countries(ctx context.Context, db Lister) ([]string, error)
- func CountryAccepted(ctx context.Context, db Querier, country 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 DeleteAccountRow(ctx context.Context, db Beginner, accountID string) error
- func DeleteProfileRows(ctx context.Context, tx pgx.Tx, profileID string) error
- func DisconnectModule(ctx context.Context, db Querier, profileID, moduleID string) error
- func DropConsentRecords(ctx context.Context, db Querier, before time.Time) (int64, error)
- func DropKeys(ctx context.Context, db Querier, before time.Time) (int64, error)
- func EndAbandonedRuns(ctx context.Context, db Querier, before, at time.Time) (int64, error)
- func HasOwnerProfile(ctx context.Context, tx pgx.Tx, accountID string) (bool, error)
- func HaveAccounts(ctx context.Context, db Lister, uids []string) (map[string]bool, error)
- func HoldAccount(ctx context.Context, tx pgx.Tx, accountID string) error
- func HoldPin(ctx context.Context, tx pgx.Tx, accountID string) (pin.State, error)
- func MarkDeleting(ctx context.Context, tx pgx.Tx, accountID string) error
- func MarkIdle(ctx context.Context, tx pgx.Tx, accountID string, before time.Time) (bool, 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 ProfileIdle(ctx context.Context, tx pgx.Tx, accountID, profileID string, before time.Time) (bool, error)
- func ProfileOf(ctx context.Context, db Querier, accountID, profileID string) (bool, time.Time, error)
- func ProfilesThere(ctx context.Context, db Lister, ids []string) (map[string]string, error)
- func RecordDone(ctx context.Context, tx pgx.Tx, accountID, key, method, fingerprint string, ...) error
- func RemoveCountry(ctx context.Context, db Querier, country string) (bool, error)
- func TodayByUTC(on, now time.Time) bool
- func TouchAccount(ctx context.Context, db Querier, accountID string) error
- func TouchProfile(ctx context.Context, db Querier, profileID string) error
- func WaitingConsents(ctx context.Context, tx pgx.Tx, accountID string) (int, error)
- func WritePin(ctx context.Context, tx pgx.Tx, accountID string, state pin.State) error
- func WriteProfile(ctx context.Context, tx pgx.Tx, p Profile) error
- func WriteUnderAccount(ctx context.Context, db Beginner, accountID string, fn func(pgx.Tx) error) error
- type Account
- type AccountRef
- type Beginner
- type ChildConsent
- type ConnectedModule
- type Consent
- type KeyRecord
- type Learner
- type Lister
- type Profile
- func CreateChildProfile(ctx context.Context, tx pgx.Tx, accountID string, c Consent, p Profile) (Profile, error)
- func CreateOwnerProfile(ctx context.Context, tx pgx.Tx, accountID string, p Profile) (Profile, error)
- func ProfileFor(ctx context.Context, tx pgx.Tx, accountID, profileID string) (Profile, error)
- type ProfileRef
- type Querier
- type TxBeginner
- type View
Constants ¶
const ( ConsentWaiting = "waiting" ConsentConfirmed = "confirmed" )
The states of a consent.
const ( // KeyGoing is a request that is still going. KeyGoing = "going" // KeyDone is a request done, whose answer the record holds. KeyDone = "done" )
The states of a record of an idempotency key.
const ConsentByOwnerStatement = "owner_statement"
The ways a consent was given (R36). The owner's own is the statement that they are 18 or older, dated with the account (R118 spec).
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 ( // ErrProfileExists is a profile there already: the owner's second, or the // profile of a consent that has one. ErrProfileExists = errors.New("store: the profile exists") // ErrNoProfile is a profile that is not the account's. ErrNoProfile = errors.New("store: no such profile") )
The refusals of a profile (contract, AccountService).
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.
var ErrNoConsent = errors.New("store: no such consent")
ErrNoConsent is a consent for a child that is not the account's.
Functions ¶
func AllowCountry ¶ added in v0.1.7
AllowCountry adds country to the list, and says whether it was not there.
func CanonicalID ¶ added in v0.1.7
CanonicalID says whether id is a UUID as the database writes one: lowercase, with its dashes. Any other ID names no row, and nothing is asked of it: OpenFGA would refuse the colons of urn:uuid:, and hold no tuple of an uppercase one.
func ClearAccount ¶ added in v0.1.8
ClearAccount deletes, in the transaction that marked the account "deleting", all the database keeps of it but its ID, its UID and the time of the mark (R21; architecture 3.10, step 1): every profile as DeleteProfileRows deletes one, the consents that wait and the adults', the records of its keys, its fields, and the binding of its invite code — the code stays redeemed.
func CloseAbandonedAttempts ¶ added in v0.1.14
CloseAbandonedAttempts closes "abandoned", at at, the open attempts with no action since before, a batch at a time, and says how many it closed (R10). One clock judges the term and dates the close: the pass's. An attempt a request holds is skipped, and the next pass finds it if it is still idle; the term is checked again on the row the UPDATE takes, so an action that came meanwhile keeps the attempt open. The close moves no date of the profile's activity.
func ConnectModule ¶ added in v0.1.13
ConnectModule connects the module to the profile, now; a module connected already keeps the time it was connected. A profile that is gone is ErrNoProfile: the row refers to it.
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; R116 spec). A profile has one consent at most, and a consent that waits has no profile.
func CountryAccepted ¶ added in v0.1.7
CountryAccepted says whether the deployment takes learners from country (R118, R119 spec; R36). None is taken until the operator adds it.
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 DeleteAccountRow ¶ added in v0.1.8
DeleteAccountRow is the last step of the deletion of an account (R21; architecture 3.10, step 4): in one transaction, it clears again what a row written late may have left, and deletes the account's row. An account that is not there is deleted already.
func DeleteProfileRows ¶ added in v0.1.8
DeleteProfileRows deletes, in the transaction that holds the profile, all that the database keeps of it (R21; architecture 3.10): its consent — for a child into its minimal record, an adult's whole — its records of idempotency keys, and the profile. What epic E3 keeps of a profile goes with it by its keys.
func DisconnectModule ¶ added in v0.1.13
DisconnectModule disconnects the module from the profile; a module not connected changes nothing.
func DropConsentRecords ¶ added in v0.1.11
DropConsentRecords deletes the minimal records of consents for children whose account or profile was deleted before before, a batch at a time, and says how many went: a record is kept 5 years after the deletion and then erased (R117 spec; R21). A stop between batches keeps what went.
func DropKeys ¶ added in v0.1.9
DropKeys deletes the records of idempotency keys made before before, a batch at a time, and says how many went: a key is kept for a day (R17). A stop between batches keeps what went.
func EndAbandonedRuns ¶ added in v0.1.14
EndAbandonedRuns ends "abandoned", at at, the open lesson runs with no action since before, a batch at a time, as CloseAbandonedAttempts closes attempts (R13).
func HasOwnerProfile ¶ added in v0.1.7
HasOwnerProfile says whether the account has the owner's profile.
func HaveAccounts ¶ added in v0.1.9
HaveAccounts is the UIDs, of uids, that have an account, marked or not: the daily job removes the users of the sign-in service that have none (R21).
func HoldAccount ¶ added in v0.1.7
HoldAccount takes the account's row FOR NO KEY UPDATE in tx: what a request under an idempotency key holds first, so that a repeat beside it waits and finds its record. The lock conflicts with itself and not with FOR KEY SHARE, so the other writes under the account do not wait for it (R29, R35).
func HoldPin ¶ added in v0.1.6
HoldPin takes the account's row FOR NO KEY UPDATE and reads its PIN. The lock conflicts with itself and not with FOR KEY SHARE: the requests of the PIN of one account — from two devices, or a repeat beside its first — go one after the other, and its count loses no wrong PIN, while the other writes under the account do not wait for them (R29).
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 MarkIdle ¶ added in v0.1.9
MarkIdle marks the account "deleting" in tx, as MarkDeleting does, only if it still had no request since before, and says whether it did. The term is read under the row's lock: a request that wrote today's date while the job chose the account keeps it, since FOR UPDATE waits for that write and reads the row again (R21; П-9).
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 ProfileIdle ¶ added in v0.1.9
func ProfileIdle(ctx context.Context, tx pgx.Tx, accountID, profileID string, before time.Time) (bool, error)
ProfileIdle holds the account's profile FOR UPDATE in tx if it was still active last before before, and says whether it was: a request that touched it today while the job chose it keeps it (П-9).
func ProfileOf ¶ added in v0.1.4
func ProfileOf(ctx context.Context, db Querier, accountID, profileID string) (bool, time.Time, error)
ProfileOf says whether the account has the profile, and the date of the profile's last activity. 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 ProfilesThere ¶ added in v0.1.9
ProfilesThere is the profiles, of ids, that are there, each with its account: a tuple whose profile is not, or is another account's, is dangling (R21). An ID that is no UUID as the database writes one names no row, and is not asked.
func RecordDone ¶ added in v0.1.6
func RecordDone(ctx context.Context, tx pgx.Tx, accountID, key, method, fingerprint string, response []byte) error
RecordDone records a request done under the account's key, with its answer, in the transaction of the action, so that the action and its record commit together or not at all. A request on the account itself names no profile.
func RemoveCountry ¶ added in v0.1.7
RemoveCountry takes country off the list, and says whether it was there. The profiles of the country stay: a new one is refused.
func TodayByUTC ¶ added in v0.1.7
TodayByUTC says whether on is the date of now by UTC: the date written once a day, of an account or a profile, that needs no writing again.
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 TouchProfile ¶ added in v0.1.7
TouchProfile writes today, by UTC, as the date of the profile's last activity, once a day at most: the date a profile is deleted by after 12 months with none (П-9; R36), as TouchAccount writes the account's.
func WaitingConsents ¶ added in v0.1.11
WaitingConsents is how many consents for a child wait under the account. It takes no lock: its caller holds the account's row FOR NO KEY UPDATE, by HoldAccount, as every request under a key does, so that two requests beside each other count one after the other (R35, R36).
func WritePin ¶ added in v0.1.6
WritePin writes the account's PIN as state has it: after an action that left one set, which every action of the PIN does.
func WriteProfile ¶ added in v0.1.7
WriteProfile writes the settings of p, the profile ProfileFor held: what an adult changes, and nothing the profile was created with.
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 AccountRef ¶ added in v0.1.9
AccountRef is an account the daily job works on: its ID and its UID at the sign-in service.
func IdleAccounts ¶ added in v0.1.9
func IdleAccounts(ctx context.Context, db Lister, before time.Time, after string, limit int) ([]AccountRef, error)
IdleAccounts is up to limit accounts not marked whose last request was before before, whose ID comes after after, by ID: those the daily job deletes after 12 months without a request (П-9). An account with no date of a request yet is none of them.
func MarkedBefore ¶ added in v0.1.9
func MarkedBefore(ctx context.Context, db Lister, before time.Time, after string, limit int) ([]AccountRef, error)
MarkedBefore is up to limit accounts marked "deleting" before before, whose ID comes after after — empty for the first page —, by ID: the marks a request left unfinished, which the daily job finishes (R21). The job pages by the last ID it saw, so that an account it fails on is not chosen again in the same pass.
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.
func CreateChildConsent ¶ added in v0.1.7
func CreateChildConsent(ctx context.Context, tx pgx.Tx, accountID, category, country string) (ChildConsent, error)
CreateChildConsent records a consent for a child that waits, under the account, with the age category and the country alone: no pseudonym, no other field of the profile, and not the age in years (privacy 3.3 spec; R36).
type ConnectedModule ¶ added in v0.1.13
ConnectedModule is a module connected to a profile (R07): its ID and when it was connected, with no version.
func ConnectedModules ¶ added in v0.1.13
ConnectedModules is the modules connected to the profile, in the order they were connected, and by ID among those connected at one time.
type Consent ¶ added in v0.1.7
type Consent struct {
ChildConsent
// ProfileID is the profile created by it, or empty while there is none.
ProfileID string
}
Consent is a consent for a child, as its profile is created by it.
type KeyRecord ¶ added in v0.1.6
KeyRecord is what is recorded of a request under its idempotency key (R17, R35): its method, its fingerprint, its state and, once done, its answer.
type Learner ¶ added in v0.1.13
Learner is what a learning request reads of its profile, in one query: the age category, by which a module's audience suits it (R07), and whether its consent is confirmed, as ConsentGiven says.
type Lister ¶ added in v0.1.7
Lister is what a list is read from: a pool, a connection or a transaction.
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.
func CreateChildProfile ¶ added in v0.1.7
func CreateChildProfile(ctx context.Context, tx pgx.Tx, accountID string, c Consent, p Profile) (Profile, error)
CreateChildProfile makes the profile of a confirmed consent, which ConsentFor held, of its category and country, binds the consent to it, and makes the row of its skill state. The date the age was given is the date the consent was asked for, by UTC.
func CreateOwnerProfile ¶ added in v0.1.7
func CreateOwnerProfile(ctx context.Context, tx pgx.Tx, accountID string, p Profile) (Profile, error)
CreateOwnerProfile makes the owner's profile in tx, of the category adult by the owner's statement, with its consent: confirmed now, given by that statement (R36; architecture 3.1), and the row of its skill state. The date the age was given is the date of the statement, and every date is by UTC, as TouchProfile writes it. The account has one owner's profile: a second is ErrProfileExists.
type ProfileRef ¶ added in v0.1.9
ProfileRef is a profile the daily job works on, with its account.
func IdleProfiles ¶ added in v0.1.9
func IdleProfiles(ctx context.Context, db Lister, before time.Time, after string, limit int) ([]ProfileRef, error)
IdleProfiles is up to limit profiles of accounts not marked, active last before before, whose ID comes after after, by ID: those the daily job deletes after 12 months without activity (П-9).
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.
Source Files
¶
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). |