Documentation
¶
Overview ¶
Package store is the persistence layer: it implements the repository interfaces that the service package declares, on top of SQLite (via database/sql) and sqlc-generated queries.
Its two jobs besides running SQL:
- translate between sqlc's row types (strings, int64) and domain types (time.Time, TaskStatus);
- translate driver errors into domain errors, so no other package ever imports database/sql or the SQLite driver to interpret a failure.
Index ¶
- func DSN(path string) string
- func Migrate(ctx context.Context, db *sql.DB) error
- func Open(ctx context.Context, path string) (*sql.DB, error)
- type Store
- func (s *Store) ConsumeVerification(ctx context.Context, purpose, valueHash string, now time.Time) (string, error)
- func (s *Store) CreateAccount(ctx context.Context, a domain.Account) error
- func (s *Store) CreateProject(ctx context.Context, p domain.Project) error
- func (s *Store) CreateSession(ctx context.Context, sess domain.Session) error
- func (s *Store) CreateTask(ctx context.Context, t domain.Task) error
- func (s *Store) CreateUserWithAccount(ctx context.Context, u domain.User, a domain.Account) error
- func (s *Store) DeleteExpiredSessions(ctx context.Context, now time.Time) (int64, error)
- func (s *Store) DeleteExpiredVerifications(ctx context.Context, now time.Time) (int64, error)
- func (s *Store) DeleteOtherSessions(ctx context.Context, userID, keepID string) error
- func (s *Store) DeleteProject(ctx context.Context, ownerID, id string) error
- func (s *Store) DeleteSessionByID(ctx context.Context, id string) error
- func (s *Store) DeleteSessionByTokenHash(ctx context.Context, tokenHash string) error
- func (s *Store) DeleteTask(ctx context.Context, ownerID, id string) error
- func (s *Store) DeleteUserSession(ctx context.Context, userID, id string) error
- func (s *Store) ExtendSession(ctx context.Context, id string, expiresAt, at time.Time) error
- func (s *Store) GetAccount(ctx context.Context, providerID, accountID string) (domain.Account, error)
- func (s *Store) GetCredentialAccount(ctx context.Context, userID string) (domain.Account, error)
- func (s *Store) GetProject(ctx context.Context, ownerID, id string) (domain.Project, error)
- func (s *Store) GetSessionByTokenHash(ctx context.Context, tokenHash string) (domain.Session, error)
- func (s *Store) GetTask(ctx context.Context, ownerID, id string) (domain.Task, error)
- func (s *Store) GetUserByEmail(ctx context.Context, email string) (domain.User, error)
- func (s *Store) GetUserByID(ctx context.Context, id string) (domain.User, error)
- func (s *Store) ListActiveSessions(ctx context.Context, userID string, now time.Time) ([]domain.Session, error)
- func (s *Store) ListProjects(ctx context.Context, ownerID, afterID string, limit int) ([]domain.Project, error)
- func (s *Store) ListTasks(ctx context.Context, projectID string, status *domain.TaskStatus, ...) ([]domain.Task, error)
- func (s *Store) MarkEmailVerified(ctx context.Context, id string, at time.Time) error
- func (s *Store) RenameProject(ctx context.Context, ownerID, id, name string, at time.Time) (domain.Project, error)
- func (s *Store) ReplaceVerification(ctx context.Context, v domain.Verification) error
- func (s *Store) ResetCredentialPassword(ctx context.Context, a domain.Account) error
- func (s *Store) UpdateAccountTokens(ctx context.Context, a domain.Account) error
- func (s *Store) UpdatePasswordHash(ctx context.Context, accountID, hash string, at time.Time) error
- func (s *Store) UpdateTask(ctx context.Context, ownerID, id string, apply func(*domain.Task) error) (domain.Task, error)
- func (s *Store) WithTx(ctx context.Context, fn func(tx *Store) error) (err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DSN ¶
DSN builds the connection string. Pragmas listed here run on EVERY connection the pool opens (they are per-connection settings in SQLite):
- foreign_keys(1): off by default; without it ON DELETE CASCADE and REFERENCES do nothing.
- journal_mode(WAL): readers do not block the writer and vice versa.
- synchronous(NORMAL): the standard pairing with WAL; durable across app crashes, and loses at most the last transactions on an OS crash or power cut, never corrupts the database.
- busy_timeout(5000): a blocked writer waits up to 5s instead of failing with "database is locked".
- _txlock=immediate: transactions start with BEGIN IMMEDIATE, taking the write lock up front. A read-then-write transaction that starts as a plain (deferred) read can fail with SQLITE_BUSY when it tries to upgrade, and busy_timeout cannot help with that.
func Migrate ¶
Migrate applies every pending migration. It uses goose's Provider API: explicit *sql.DB and fs.FS arguments, no package-level state, structured results.
Run it from ONE place (the deploy step, or this process at startup when there is a single instance, as here). Several replicas migrating simultaneously is a classic outage. goose takes a lock on PostgreSQL, but the safe habit is "one migrator".
Types ¶
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store implements the service layer's repository interfaces (users, accounts, sessions, verifications, projects, tasks).
func (*Store) ConsumeVerification ¶
func (s *Store) ConsumeVerification(ctx context.Context, purpose, valueHash string, now time.Time) (string, error)
ConsumeVerification atomically finds, deletes and returns the identifier of an unexpired token of the given purpose whose stored value (the token's SHA-256) matches. domain.ErrNotFound when there is none: unknown, already used, expired, or wrong purpose.
func (*Store) CreateAccount ¶
func (*Store) CreateProject ¶
func (*Store) CreateSession ¶
func (*Store) CreateUserWithAccount ¶
CreateUserWithAccount inserts a user and its first account in ONE transaction. A user without any account could never sign in, and an account without a user violates the foreign key, so the two must appear together or not at all. A duplicate email is domain.ErrConflict.
func (*Store) DeleteExpiredSessions ¶
func (*Store) DeleteExpiredVerifications ¶
func (*Store) DeleteOtherSessions ¶
DeleteOtherSessions deletes all of the user's sessions except keepID.
func (*Store) DeleteProject ¶
func (*Store) DeleteSessionByID ¶
func (*Store) DeleteSessionByTokenHash ¶
DeleteSessionByTokenHash and DeleteSessionByID are idempotent: deleting nothing is success (sign-out twice, or two requests racing to delete the same expired session).
func (*Store) DeleteTask ¶
func (*Store) DeleteUserSession ¶
DeleteUserSession deletes one of the USER'S sessions. A session id that belongs to someone else is indistinguishable from a missing one: domain.ErrNotFound.
func (*Store) ExtendSession ¶
ExtendSession pushes expires_at out (sliding expiry).
func (*Store) GetAccount ¶
func (s *Store) GetAccount(ctx context.Context, providerID, accountID string) (domain.Account, error)
GetAccount finds the account of provider by the provider's own user id.
func (*Store) GetCredentialAccount ¶
GetCredentialAccount returns the user's email+password account; ErrNotFound for users who only ever signed in with an OAuth provider.
func (*Store) GetProject ¶
func (*Store) GetSessionByTokenHash ¶
func (s *Store) GetSessionByTokenHash(ctx context.Context, tokenHash string) (domain.Session, error)
GetSessionByTokenHash returns the session regardless of expiry; the service decides what an expired one means (and deletes it).
func (*Store) GetUserByEmail ¶
func (*Store) GetUserByID ¶
func (*Store) ListActiveSessions ¶
func (s *Store) ListActiveSessions(ctx context.Context, userID string, now time.Time) ([]domain.Session, error)
ListActiveSessions returns the user's unexpired sessions, newest first.
func (*Store) ListProjects ¶
func (*Store) ListTasks ¶
func (s *Store) ListTasks(ctx context.Context, projectID string, status *domain.TaskStatus, afterID string, limit int) ([]domain.Task, error)
ListTasks returns up to limit tasks of a project with id > afterID, ordered by id. A nil status means "any status". The caller must already have verified project ownership.
func (*Store) MarkEmailVerified ¶
MarkEmailVerified sets email_verified; domain.ErrNotFound if the user does not exist.
func (*Store) RenameProject ¶
func (*Store) ReplaceVerification ¶
ReplaceVerification stores v after deleting older tokens with the same identifier, so asking for a new reset email invalidates the previous link. One transaction: never zero valid tokens after a failure in between.
func (*Store) ResetCredentialPassword ¶
ResetCredentialPassword sets a user's password after a successful reset and signs them out everywhere, atomically. If the user has no credential account yet (they only used GitHub), a.* describes the one to create. All sessions are deleted in the same transaction: whoever knew the OLD password, or held a stolen session, must not stay logged in after a reset.
func (*Store) UpdateAccountTokens ¶
UpdateAccountTokens refreshes the stored provider credentials of an OAuth account.
func (*Store) UpdatePasswordHash ¶
UpdatePasswordHash replaces the stored hash of one account (rehash on login).
func (*Store) UpdateTask ¶
func (s *Store) UpdateTask(ctx context.Context, ownerID, id string, apply func(*domain.Task) error) (domain.Task, error)
UpdateTask is an atomic read-modify-write: it loads the task (scoped to ownerID), lets apply change it (validation, patch semantics: that is business logic and lives in the service), and writes it back, all in one transaction. Two concurrent PATCHes therefore serialize instead of one silently overwriting the other's fields.