Documentation
¶
Overview ¶
Package db wraps the SQLite store (pure Go driver, no CGO) and migrations.
Index ¶
- Variables
- func BuildAt(ctx context.Context, path string, v Versions) (*sql.DB, error)
- func Now() int64
- func OpenForReading(dataDir string) (*sql.DB, error)
- func OpenForeign(path string) (*sql.DB, error)
- func OpenPlainFile(path string) (*sql.DB, error)
- func QuoteIdent(s string) string
- func RemoveOrphans(ctx context.Context, d *sql.DB) (int64, error)
- func RestoreTriggers(ctx context.Context, path string) error
- func SnapshotTo(ctx context.Context, live *sql.DB, path string) error
- type DB
- func (d *DB) Columns(ctx context.Context, table string) ([]TableColumn, error)
- func (d *DB) GetSetting(ctx context.Context, key, def string) string
- func (d *DB) Optimize(ctx context.Context)
- func (d *DB) SetSetting(ctx context.Context, key, value string) error
- func (d *DB) Settings(ctx context.Context) (map[string]string, error)
- func (d *DB) Tables(ctx context.Context) ([]string, error)
- type Recovered
- type RecoveredTable
- type TableColumn
- type Versions
Constants ¶
This section is empty.
Variables ¶
var ErrNoDatabase = errors.New("there is no database in this folder")
ErrNoDatabase: there's no database in that folder, which is what a new install looks like.
var Trace func(query string, took time.Duration)
Trace, when set, is told of every statement and how long it took, a query's until its rows are closed. Only builds for measuring set it (docs/PERFORMANCE.md), before the database opens; otherwise it costs one comparison per statement.
Functions ¶
func BuildAt ¶
BuildAt makes an empty database in dir with exactly these versions of the schema, and returns it open on one connection with foreign keys off, ready to take rows.
It and OpenPlainFile are for working copies only, never the live database, so their rollback journal stays in memory: on disk, every statement made and deleted a journal file, and on Windows each delete can wait on the virus scanner, which held a backup's check for minutes. A crash loses only the copy, which is made again.
func OpenForReading ¶
OpenForReading opens the database of a data folder for looking only: no migrations, no writes, and a short wait if a server is holding it. For tools that run beside a server, or in place of one that won't start, where a broken database has to be an answer rather than a failure. The caller closes it.
func OpenForeign ¶
OpenForeign opens a database file from outside, to read, trusting nothing in it.
func OpenPlainFile ¶
OpenPlainFile opens a working copy with no pool and no checks on foreign keys, for rewriting one before it's used.
func QuoteIdent ¶
QuoteIdent writes a table or column name for a statement.
func RemoveOrphans ¶
RemoveOrphans deletes rows whose parent has gone, and says how many.
func RestoreTriggers ¶
RestoreTriggers puts this program's triggers on a database they were taken off for a bulk rewrite, from a clean build of the same schema rather than anything in the file.
Types ¶
type DB ¶
func OpenExisting ¶
OpenExisting opens <dataDir>/wms.db without migrating or creating it. readOnly keeps every statement from writing. The first read happens here, so a file that isn't a database at all answers with an error rather than a handle.
func (*DB) GetSetting ¶
Setting helpers.
func (*DB) Optimize ¶
Optimize lets SQLite refresh its query statistics; cheap, and done after a scan has changed a lot of rows.
type Recovered ¶
type Recovered struct {
Tables []RecoveredTable `json:"tables"`
Copied int64 `json:"copied"`
Lost int64 `json:"lost"`
Refused int64 `json:"refused"`
Unlinked int64 `json:"unlinked"` // rows removed because what they pointed at was lost
Unread int `json:"unread"` // tables that couldn't be read at all
}
Recovered is the result of a recovery.
func RecoverInto ¶
RecoverInto writes a new wms.db in dst from what can be read of the one in src. dst must be a new, empty folder of the caller's. A panic in the driver is turned into an error: a damaged file must never take down whoever is trying to repair it.
type RecoveredTable ¶
type RecoveredTable struct {
Name string `json:"name"`
Copied int64 `json:"copied"`
// Lost is what couldn't be copied. Where a damaged stretch hides how many rows it
// held, this is how many row numbers it spans, so it can be more than there were.
Lost int64 `json:"lost"`
Refused int64 `json:"refused"` // rows the new table wouldn't take
Failed bool `json:"failed"` // nothing of this table could be read at all
}
RecoveredTable says how one table came through.
type TableColumn ¶
TableColumn is one column of a table, as the database says.
type Versions ¶
type Versions struct {
Main int `json:"main"`
TV int `json:"tv"`
Metadata int `json:"metadata"`
Music int `json:"music"`
Games int `json:"games"`
// LibTranscode: library transcoding's tables.
LibTranscode int `json:"libTranscode"`
}
Versions says how far each track of migrations has gone.