Documentation
¶
Overview ¶
Package sqlstore is the shared SQL persistence adapter for SQLite and PostgreSQL (02).
Index ¶
- Variables
- func CheckSQLiteFile(ctx context.Context, path string) (version int64, err error)
- func DiskUsage(dir string) (free, total int64, err error)
- func FreeBytes(dir string) (int64, error)
- func LockDataDir(ctx context.Context, dir string) (func(), error)
- func LockRun(dir string) (func(), error)
- func ProgramVersion(fsys fs.FS) (int64, error)
- func SnapshotFile(ctx context.Context, src, dest string) error
- func SnapshotSpace(path string) int64
- type Config
- type DB
- func (db *DB) CheckVersion(ctx context.Context, o MigrateOptions) (current, target int64, err error)
- func (db *DB) Close() error
- func (db *DB) CountForTest(ctx context.Context, table string) (int, error)
- func (db *DB) Dialect() Dialect
- func (db *DB) DialectName() string
- func (db *DB) ExecForTest(ctx context.Context, q string, args ...any) error
- func (db *DB) Migrate(ctx context.Context, o MigrateOptions) (MigrateResult, error)
- func (db *DB) Path() string
- func (db *DB) Ping(ctx context.Context) error
- func (db *DB) Read(ctx context.Context, fn func(app.Store) error) error
- func (db *DB) SelectForTest(ctx context.Context, dest any, q string, args ...any) error
- func (db *DB) SizeBytes(ctx context.Context) (int64, error)
- func (db *DB) Write(ctx context.Context, fn func(app.Store) error) error
- type Dialect
- type MigrateOptions
- type MigrateResult
- type NullTime
- type SchemaNewerError
- type Time
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadyRunning = errors.New("another Liturgist process is using this data folder")
ErrAlreadyRunning means another Liturgist process holds the data folder's run lock (14 §3).
Functions ¶
func CheckSQLiteFile ¶
CheckSQLiteFile opens the database file at path read-only, runs SQLite's integrity and foreign-key checks, and returns its schema version. A file that is not a Liturgist database is an error.
func LockDataDir ¶
LockDataDir takes the migration lock of the data folder (waits up to 30 s).
func LockRun ¶
LockRun takes the run lock of the data folder, without waiting. serve holds it for its whole life; restore needs it (14 §3, H-2). The returned function releases it.
func ProgramVersion ¶
ProgramVersion is the newest schema version of the migrations in fsys, from the numeric prefix of the file names (00012_x.sql is 12).
func SnapshotFile ¶
SnapshotFile writes a consistent copy of the SQLite database src to dest (which must not exist) with VACUUM INTO. It uses a connection of its own, so it works while a server has the file open and never queues behind its writer. When the disk is full the error wraps app.ErrStorageFull.
func SnapshotSpace ¶
SnapshotSpace is the free space a copy of the SQLite file at path needs: the database and its WAL, plus 20 %.
Types ¶
type Config ¶
type Config struct {
Driver string // "sqlite" | "postgres"
DataDir string // SQLite file lives at DataDir/liturgist.db
URL string // PostgreSQL
MaxConns int // PostgreSQL pool; 0 = 10
MaxReaders int // SQLite readers; 0 = 4
Logger *slog.Logger // warnings about unknown stored scopes; nil = discard
}
Config selects and sizes the database.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB implements app.Tx.
func (*DB) CheckVersion ¶
func (db *DB) CheckVersion(ctx context.Context, o MigrateOptions) (current, target int64, err error)
CheckVersion returns the database and program schema versions. It fails with *SchemaNewerError when the database is ahead, unless AllowNewerSchema.
func (*DB) CountForTest ¶
CountForTest counts the rows of a table, over all churches. Tests only.
func (*DB) DialectName ¶
DialectName is "sqlite" or "postgres"; tests use it to skip interleavings that only the second can produce.
func (*DB) ExecForTest ¶
ExecForTest runs a statement outside any Tx. It is for tests (DDL, and states that no use case can reach yet); production code does not use it.
func (*DB) Migrate ¶
func (db *DB) Migrate(ctx context.Context, o MigrateOptions) (MigrateResult, error)
Migrate applies pending migrations (02 §5). On SQLite it holds the lock file for the whole run and makes the pre-upgrade copy first. It never runs on a newer schema.
func (*DB) SelectForTest ¶
SelectForTest reads rows into dest (a pointer to a slice of structs with db tags) outside any Tx. Tests only.
type Dialect ¶
type Dialect interface {
Name() string // "sqlite" | "postgres"
Rebind(query string) string
// MapError turns driver errors into app errors (02 §8); nil and unknown errors pass through.
MapError(err error) error
// Retryable reports busy, serialisation and deadlock errors (02 §2).
Retryable(err error) bool
LockInstall(ctx context.Context, tx *sqlx.Tx) error
LockUser(ctx context.Context, tx *sqlx.Tx, id string) error
LockChurch(ctx context.Context, tx *sqlx.Tx, id string) error
SetTenant(ctx context.Context, tx *sqlx.Tx, churchID string) error // RLS hook (02 §7)
TimeArg(t time.Time) any // value to bind for a timestamp
// Song search (06 §5): the only place the two databases differ in how
// text is matched. Folded text contains only letters, digits and single
// spaces, so every tokenizer splits at the same places.
WriteSongIndex(ctx context.Context, tx *sqlx.Tx, churchID, songID, language, head, lyrics string) error // replaces the song's rows
DeleteSongIndex(ctx context.Context, tx *sqlx.Tx, churchID, songID string) error
DeleteChurchIndex(ctx context.Context, tx *sqlx.Tx, churchID string) error
// TermMatch is the predicate "some token of the head (headOnly) or of head or
// lyrics starts with term" for the song_search row aliased ss.
TermMatch(term string, headOnly bool) (sql string, args []any)
// Contains is the predicate "col contains the bound value" (Chinese substring search).
Contains(col string) string
// OrderBytes orders a text column bytewise, so both databases sort alike.
OrderBytes(col string) string
}
Dialect holds every database difference (decisions log: one adapter, small dialect layer).
type MigrateOptions ¶
type MigrateOptions struct {
FS fs.FS // migrations for this dialect
AllowNewerSchema bool // only skips the refusal to start (02 §5.1)
RequireCopy bool // LITURGIST_REQUIRE_PREUPGRADE_COPY
Logger *slog.Logger
Now func() time.Time
}
MigrateOptions configures CheckVersion and Migrate.
type MigrateResult ¶
MigrateResult reports what Migrate did.
type SchemaNewerError ¶
type SchemaNewerError struct{ DB, Program int64 }
SchemaNewerError means the database was migrated by a newer program (exit code 3).
func (*SchemaNewerError) Error ¶
func (e *SchemaNewerError) Error() string
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package sqlstoretest provides migrated databases for tests (02 §6).
|
Package sqlstoretest provides migrated databases for tests (02 §6). |