Documentation
¶
Overview ¶
Package data holds Fletch's thin database helpers: carrying a *sql.DB in a context, read-only transactions, and directory migrations. It depends on nothing outside the standard library and has no ORM; drivers live in data/sqlite and data/postgres, and code generation is left to Bob.
Index ¶
- Constants
- func DB(ctx context.Context) *sql.DB
- func MigrateDir(ctx context.Context, db *sql.DB, fsys fs.FS) ([]string, error)
- func ObserveConnector(c driver.Connector, o Observer) driver.Connector
- func OpenObserved(driverName, dsn string, o Observer) (*sql.DB, error)
- func ReadOnlyTx(ctx context.Context, db *sql.DB, dialect Dialect, ...) (err error)
- func RedactDSN(dsn string) string
- func ScanTime(src any) (time.Time, error)
- func WithDB(ctx context.Context, db *sql.DB) context.Context
- type Describer
- type Description
- type Dialect
- type Migration
- type NullTime
- type Observer
- type Time
Constants ¶
const ( OpQuery = "query" OpExec = "exec" )
The ops StartQuery reports.
const MigrationsTable = "fletch_schema_migrations"
MigrationsTable records which migrations have been applied.
const Redacted = "[redacted]"
Redacted replaces secret setting values in a Description.
Variables ¶
This section is empty.
Functions ¶
func MigrateDir ¶
MigrateDir applies the not-yet-applied *.sql files of fsys in file-name order, each in its own transaction, and records them in fletch_schema_migrations. It needs no external tool and works on SQLite and Postgres. A file that has already been applied is never re-run, even if its contents changed. It returns the versions it applied.
The files are those of DialectMigrations for DialectOf(db): the root of fsys plus its sqlite/ or postgres/ subdirectory, when there is one.
On Postgres it holds a session advisory lock on one connection for the whole run, so app replicas may all call it at startup: one applies the files and the others then find them applied. Concurrent calls against one SQLite database are not coordinated; run it from one process.
func ObserveConnector ¶
ObserveConnector returns a Connector whose connections report every query and exec to o. Open the database with sql.OpenDB.
func OpenObserved ¶
OpenObserved opens a database like sql.Open, with every query and exec reported to o. Like sql.Open it does no I/O.
func ReadOnlyTx ¶
func ReadOnlyTx(ctx context.Context, db *sql.DB, dialect Dialect, fn func(ctx context.Context, tx *sql.Tx) error) (err error)
ReadOnlyTx runs fn in a transaction that cannot write. On SQLite it pins a connection and sets PRAGMA query_only for the duration; on Postgres it issues SET TRANSACTION READ ONLY. The transaction commits when fn returns nil and rolls back otherwise.
func RedactDSN ¶
RedactDSN hides credentials in a connection string: the password of a URL's userinfo, secret-looking URL query parameters, and secret-looking key=value pairs. The result is safe to log and to show in Describe.
func ScanTime ¶
ScanTime converts a database value to a time.Time. It accepts a time.Time, and text or bytes in SQLite's layouts, RFC 3339, or Go's Time.String (with or without a monotonic reading). Text without a zone is UTC. SQLite returns expressions such as MIN(created_at) as text, since an expression has no declared type; ScanTime reads them like the column.
Types ¶
type Describer ¶
type Describer interface {
Describe() Description
}
Describer is implemented by the handles that data/sqlite and data/postgres return.
type Description ¶
type Description struct {
Provider string `json:"provider"`
Settings map[string]string `json:"settings,omitempty"`
}
Description is what a database handle reports about itself for `fletch ax inspect`. Settings are already redacted.
type Migration ¶
Migration is one *.sql file.
func DialectMigrations ¶
DialectMigrations reads the *.sql files at the root of fsys, shared by every dialect, together with those of the dialect's subdirectory (sqlite/ or postgres/), in version order. A dialect file replaces the shared file of the same version, so 0001_init.sql at the root can hold portable SQL while postgres/0001_init.sql overrides it on Postgres. A missing subdirectory, or an empty dialect, gives the root files alone; subdirectories of other dialects are never read.
type Observer ¶
type Observer interface {
// StartQuery is called before a statement runs. op is "query" or
// "exec". The driver gets the returned context. end gets the driver's
// error; for a query it is called when the driver returns the rows, not
// when they are read. It gets driver.ErrSkip when the driver asked
// database/sql to prepare the statement instead, and the prepared
// statement's run is then observed on its own.
StartQuery(ctx context.Context, op, query string) (context.Context, func(err error))
}
Observer sees the statements a database runs, for traces and metrics. Its methods use only standard library types, so an implementation does not need to import data. Methods are called from many goroutines at once and must not block.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package migrate runs the external tools Fletch delegates to: Atlas for schema diffing and migration, and Bob's generators for typed query code.
|
Package migrate runs the external tools Fletch delegates to: Atlas for schema diffing and migration, and Bob's generators for typed query code. |
|
postgres
module
|
|
|
sqlite
module
|