data

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 6, 2026 License: Apache-2.0, MIT Imports: 16 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	OpQuery = "query"
	OpExec  = "exec"
)

The ops StartQuery reports.

View Source
const MigrationsTable = "fletch_schema_migrations"

MigrationsTable records which migrations have been applied.

View Source
const Redacted = "[redacted]"

Redacted replaces secret setting values in a Description.

Variables

This section is empty.

Functions

func DB

func DB(ctx context.Context) *sql.DB

DB returns the database stored by WithDB, or nil.

func MigrateDir

func MigrateDir(ctx context.Context, db *sql.DB, fsys fs.FS) ([]string, error)

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

func ObserveConnector(c driver.Connector, o Observer) driver.Connector

ObserveConnector returns a Connector whose connections report every query and exec to o. Open the database with sql.OpenDB.

func OpenObserved

func OpenObserved(driverName, dsn string, o Observer) (*sql.DB, error)

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

func RedactDSN(dsn string) string

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

func ScanTime(src any) (time.Time, error)

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.

func WithDB

func WithDB(ctx context.Context, db *sql.DB) context.Context

WithDB returns a context that carries db.

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 Dialect

type Dialect string

Dialect selects how a transaction is made read-only.

const (
	SQLite   Dialect = "sqlite"
	Postgres Dialect = "postgres"
)

The dialects ReadOnlyTx supports.

func DialectOf

func DialectOf(db *sql.DB) Dialect

DialectOf reports the dialect of db from the package of its driver: SQLite for modernc.org/sqlite and other sqlite drivers, Postgres for pgx and lib/pq, and "" for anything else. It does no I/O.

type Migration

type Migration struct {
	Version string // the file name without .sql
	SQL     string
}

Migration is one *.sql file.

func DialectMigrations

func DialectMigrations(fsys fs.FS, dialect Dialect) ([]Migration, error)

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.

func Migrations

func Migrations(fsys fs.FS) ([]Migration, error)

Migrations reads the *.sql files at the root of fsys in file-name order.

type NullTime

type NullTime struct {
	Time  time.Time
	Valid bool
}

NullTime is sql.NullTime that scans from the text forms of ScanTime.

func (*NullTime) Scan

func (n *NullTime) Scan(src any) error

Scan implements sql.Scanner.

func (NullTime) Value

func (n NullTime) Value() (driver.Value, error)

Value implements driver.Valuer.

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.

type Time

type Time struct {
	time.Time
}

Time is a time.Time that scans from the text forms of ScanTime, for SQLite aggregates of TIMESTAMP columns. NULL is an error; use NullTime.

func (*Time) Scan

func (t *Time) Scan(src any) error

Scan implements sql.Scanner.

func (Time) Value

func (t Time) Value() (driver.Value, error)

Value implements driver.Valuer.

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL