GitHubGet started

package sqlstore

import "github.com/nsgo-dev/game/pkg/online/accounts/sqlstore"

Overview

Package sqlstore is experimental: its API and documented formats may change in a minor release. See the API stability policy. is an accounts.Store over database/sql, shared by the SQLite (package sqlitestore) and Postgres (package pgstore) stores. Open a store with one of those; use this package directly only to add another SQL database through a Dialect.

The schema is three tables, created by New when missing and versioned in accounts_schema:

  • accounts: id, email (unique, null for none), password_hash, created_at, and the suspension: suspended_at (null for none), suspended_until (null for no end), suspension_reason, suspended_by
  • account_identities: provider and subject (the key), account_id (unique per provider), created_at
  • account_sessions: id, account_id, secret_hash, created_at

Times are Unix milliseconds. Uniqueness is enforced by the database's constraints, so concurrent services sharing one database cannot give one email address or identity to two accounts.

Index

Constants

const SchemaVersion = 2

SchemaVersion is the schema version New creates and accepts. Version 2 added the suspension columns of accounts; New adds them to a version 1 database.

Types

type Dialect struct {
	// Name names the database in errors.
	Name string
	// Numbered is true for "$1" placeholders, false for "?".
	Numbered bool
	// IsUnique reports whether err is a unique or primary key constraint violation.
	IsUnique func(err error) bool
	// MigrateLock, when set, runs first in the migration's transaction to serialize migrations of several processes, such as Postgres's pg_advisory_xact_lock.
	MigrateLock string
	// RowLock is appended to a SELECT of one account in a transaction to lock its row until the transaction ends, such as Postgres's " FOR UPDATE". The store reads the account this way before it checks and changes its credentials and sessions, so that UnlinkIdentity, ChangePasswordHash and CreateSession on one account run one at a time. Leave it empty only for a database whose write transactions already run one at a time, such as SQLite with _txlock=immediate.
	RowLock string
}

Dialect is what differs between SQL databases.

type Stats struct {
	// Accounts is the number of accounts.
	Accounts int64
	// Sessions is the number of open device sessions: sessions signed in and not yet signed out, replaced or ended with their account.
	Sessions int64
}

Stats is how much a store holds, for a service's metrics.

func ReadStats(ctx context.Context, db *sql.DB) (Stats, error)

ReadStats is Store.Stats on db, another handle on a store's database, such as the read-only one sqlitestore.OpenReader opens, so the full scans run beside the store's connection rather than on it.

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

Store is an accounts.Store in a SQL database.

func New(ctx context.Context, db *sql.DB, d Dialect) (*Store, error)

New returns a store on db, creating the schema when it is missing. It refuses a database whose schema is newer than SchemaVersion.

func (s *Store) Account(ctx context.Context, id string) (accounts.Account, error)

Account implements accounts.Store.

func (s *Store) AccountByEmail(ctx context.Context, email string) (accounts.Account, string, error)

AccountByEmail implements accounts.Store.

func (s *Store) AccountByIdentity(ctx context.Context, identity accounts.Identity) (accounts.Account, error)

AccountByIdentity implements accounts.Store.

func (s *Store) ChangePasswordHash(ctx context.Context, id, passwordHash, keepSession string) error

ChangePasswordHash implements accounts.Store. It locks the account's row, so a CreateSession of the account waits for it or runs before it.

func (s *Store) Close() error

Close closes the store's database.

func (s *Store) CreateAccount(ctx context.Context, a accounts.Account, passwordHash string, identities ...accounts.Identity) error

CreateAccount implements accounts.Store.

func (s *Store) CreateSession(ctx context.Context, sess accounts.Session) error

CreateSession implements accounts.Store. It locks the account's row, so it runs either before a ChangePasswordHash or SetSuspension of the account or after it, never in between.

func (s *Store) DB() *sql.DB

DB returns the store's database.

func (s *Store) DeleteAccount(ctx context.Context, id string) error

DeleteAccount implements accounts.Store. Foreign-key cascades remove all identities and sessions atomically.

func (s *Store) DeleteSession(ctx context.Context, id string) error

DeleteSession implements accounts.Store.

func (s *Store) DeleteSessions(ctx context.Context, id string) (int, error)

DeleteSessions implements accounts.Store. It locks the account's row, so a session the account opens at the same time is either deleted or created after it.

func (s *Store) Identities(ctx context.Context, id string) ([]accounts.Identity, error)

Identities implements accounts.Store.

func (s *Store) LinkIdentity(ctx context.Context, id string, identity accounts.Identity, at time.Time) error

LinkIdentity implements accounts.Store.

func (s *Store) RemoveIdentity(ctx context.Context, id, provider string) error

RemoveIdentity implements accounts.Store.

func (s *Store) SearchAccounts(ctx context.Context, q accounts.AccountQuery, cursor string, limit int) (accounts.AccountPage, error)

SearchAccounts implements accounts.Store. A prefix is compared with substr rather than LIKE, so '%' and '_' in it match only themselves. Pages are ordered by the database's ordering of IDs, which for the service's lowercase hex IDs is their byte order.

func (s *Store) Session(ctx context.Context, id string) (accounts.Session, error)

Session implements accounts.Store.

func (s *Store) Sessions(ctx context.Context, id string) ([]accounts.Session, error)

Sessions implements accounts.Store.

func (s *Store) SetEmail(ctx context.Context, id, email, passwordHash string) error

SetEmail implements accounts.Store.

func (s *Store) SetPasswordHash(ctx context.Context, id, oldHash, newHash string) error

SetPasswordHash implements accounts.Store.

func (s *Store) SetSuspension(ctx context.Context, id string, sp *accounts.Suspension) error

SetSuspension implements accounts.Store. It locks the account's row, so a CreateSession of the account runs before it or after it.

func (s *Store) Stats(ctx context.Context) (Stats, error)

Stats counts the store's accounts and sessions. It reads both tables in full, so call it at a scrape's pace, not per request.

func (s *Store) UnlinkIdentity(ctx context.Context, id, provider string) error

UnlinkIdentity implements accounts.Store. It locks the account's row before it counts the account's credentials, so two unlinks of one account run one at a time and the second sees what the first removed.