Documentation
¶
Overview ¶
Package pgmem provides a fast, process-local PostgreSQL emulator for Go tests. It follows pg-mem's database-and-schema model while exposing idiomatic context-aware Go APIs and a database/sql adapter.
Pgmem is an emulator, not PostgreSQL. Applications should still run their migrations and integration tests against every PostgreSQL version they support before release.
Index ¶
- Variables
- type Backup
- type DB
- func (db *DB) Backup() (*Backup, error)
- func (db *DB) BackupContext(ctx context.Context) (*Backup, error)
- func (db *DB) Close() error
- func (db *DB) Connector() driver.Connector
- func (db *DB) CreateSchema(ctx context.Context, name string) (*Schema, error)
- func (db *DB) Open() *sql.DB
- func (db *DB) Public() *Schema
- func (db *DB) RegisterFunction(function Function) error
- func (db *DB) SQLDB() *sql.DB
- func (db *DB) Schema(name string) (*Schema, bool)
- func (db *DB) SchemaNames() []string
- type Error
- type Function
- type ITranslator
- type Option
- type QueryInterceptor
- type Result
- type Row
- type ScalarFunction
- type Schema
- func (s *Schema) Connector() driver.Connector
- func (s *Schema) Exec(statement string, arguments ...any) (Result, error)
- func (s *Schema) ExecContext(ctx context.Context, statement string, arguments ...any) (Result, error)
- func (s *Schema) GetTable(name string) (*Table, error)
- func (s *Schema) InterceptQueries(interceptor QueryInterceptor) *Subscription
- func (s *Schema) Many(statement string, arguments ...any) ([]Row, error)
- func (s *Schema) ManyContext(ctx context.Context, statement string, arguments ...any) ([]Row, error)
- func (s *Schema) Name() string
- func (s *Schema) None(statement string, arguments ...any) error
- func (s *Schema) NoneContext(ctx context.Context, statement string, arguments ...any) error
- func (s *Schema) One(statement string, arguments ...any) (Row, error)
- func (s *Schema) OneContext(ctx context.Context, statement string, arguments ...any) (Row, error)
- func (s *Schema) Open() *sql.DB
- func (s *Schema) Query(statement string, arguments ...any) (Result, error)
- func (s *Schema) QueryContext(ctx context.Context, statement string, arguments ...any) (Result, error)
- func (s *Schema) RegisterFunction(function Function) error
- func (s *Schema) Table(name string) (*Table, error)
- func (s *Schema) TableContext(ctx context.Context, name string) (*Table, error)
- type Subscription
- type Table
- func (t *Table) Find(template Row) ([]Row, error)
- func (t *Table) FindContext(ctx context.Context, template Row) ([]Row, error)
- func (t *Table) Insert(row Row) (Row, error)
- func (t *Table) InsertContext(ctx context.Context, row Row) (Row, error)
- func (t *Table) Name() string
- func (t *Table) Schema() *Schema
Constants ¶
This section is empty.
Variables ¶
var ( // ErrClosed is returned after a database has been closed. ErrClosed = errors.New("pgmem: database is closed") // ErrNoRows is returned when One expects a row and receives none. ErrNoRows = errors.New("pgmem: expected one row, got none") // ErrTooManyRows is returned when One receives more than one row. ErrTooManyRows = errors.New("pgmem: expected one row, got many") // ErrInvalidSchema is returned for an invalid or unknown schema name. ErrInvalidSchema = errors.New("pgmem: invalid schema") // ErrUnsupported is returned when valid PostgreSQL syntax is outside the // emulator's documented compatibility surface. ErrUnsupported = errors.New("pgmem: unsupported PostgreSQL feature") // ErrRelationNotFound is returned when a requested table or view does not // exist in a schema. ErrRelationNotFound = errors.New("pgmem: relation does not exist") // ErrSchemaChanged is returned when restoring a backup after the database's // schema definitions or named-schema topology changed. ErrSchemaChanged = errors.New("pgmem: schema changed since backup") )
Functions ¶
This section is empty.
Types ¶
type Backup ¶
type Backup struct {
// contains filtered or unexported fields
}
Backup is an immutable restore point owned by one DB. A backup can be restored repeatedly, including concurrently with ordinary operations.
Restore accepts data changes made after the backup, but rejects schema DDL and named-schema topology changes. That restriction matches pg-mem's backup contract and keeps existing Schema handles valid.
func (*Backup) RestoreContext ¶
RestoreContext resets all data to this restore point. It is repeatable. If any schema definition or the named-schema topology changed since Backup, RestoreContext returns ErrSchemaChanged without changing data.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is one isolated, in-memory database. A DB owns its SQL connection and must be closed when a test or process is finished with it.
func NewContext ¶
NewContext creates an isolated in-memory database and validates its connection before returning.
func (*DB) BackupContext ¶
BackupContext creates a restore point for the public schema and every named schema. The returned backup owns defensive copies of all database images.
func (*DB) Connector ¶
Connector returns a database/sql connector for the public schema.
The returned connector does not own DB. Close the database/sql pool before closing DB.
func (*DB) CreateSchema ¶
CreateSchema creates and returns an isolated named schema.
func (*DB) Open ¶
Open returns a database/sql pool backed by the public schema. The returned pool and DB have independent lifetimes and both must be closed.
func (*DB) RegisterFunction ¶
RegisterFunction registers or atomically replaces one public-schema scalar function overload. Registering functions on separate DB values is isolated.
func (*DB) SQLDB ¶
SQLDB exposes the underlying database/sql pool. Direct calls currently use the internal dialect; prefer Schema methods when PostgreSQL translation is required.
func (*DB) SchemaNames ¶
SchemaNames returns a stable snapshot of known schema names.
type Error ¶
Error adds a PostgreSQL-style SQLSTATE code and statement context to an execution error. Cause remains available through errors.Is and errors.As.
type Function ¶
type Function struct {
Name string
Arity int
Variadic bool
Implementation ScalarFunction
}
Function describes one scalar function overload. Arity is the exact number of positional arguments for a fixed function and the minimum number for a variadic function.
type ITranslator ¶
type ITranslator interface {
Translate(ctx context.Context, defaultSchema, statement string) (string, error)
}
ITranslator converts PostgreSQL syntax into the internal execution dialect. Implementations must be safe for concurrent use.
type Option ¶
type Option func(cfg *config) error
Option configures a database before its first connection is opened.
func WithTranslator ¶
func WithTranslator(translator ITranslator) Option
WithTranslator replaces the PostgreSQL compatibility translator. This is primarily useful for adding project-specific syntax during tests.
type QueryInterceptor ¶
type QueryInterceptor func(ctx context.Context, statement string, arguments []any) (result Result, handled bool, err error)
QueryInterceptor can replace a statement with an ad-hoc result. Returning handled=false passes the original statement to the next interceptor and, eventually, the embedded SQL engine.
Interceptors receive the original PostgreSQL statement and a defensive copy of its arguments. They run in registration order, and the first handled result wins.
type ScalarFunction ¶
ScalarFunction implements a registered scalar SQL function. Arguments use the embedded engine's scalar representations: int64, float64, string, []byte, and nil. The returned value must be one of those types or an ordinary Go integer or float. bool and time.Time returns are accepted but the embedded engine determines how they are materialized.
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
Schema is a namespace inside a DB.
func (*Schema) Connector ¶
Connector returns a database/sql connector whose unqualified statements use this schema. The returned connector does not own the schema's DB.
func (*Schema) ExecContext ¶
func (s *Schema) ExecContext(ctx context.Context, statement string, arguments ...any) (Result, error)
ExecContext executes a statement and returns its affected-row count.
func (*Schema) InterceptQueries ¶
func (s *Schema) InterceptQueries(interceptor QueryInterceptor) *Subscription
InterceptQueries registers an interceptor for this schema. Interceptors are schema-local: registering one on public does not affect a named schema.
InterceptQueries panics when interceptor is nil, matching the usual Go registration contract for callbacks that cannot be useful when nil.
func (*Schema) ManyContext ¶
func (s *Schema) ManyContext(ctx context.Context, statement string, arguments ...any) ([]Row, error)
ManyContext executes a query and returns every row.
func (*Schema) NoneContext ¶
NoneContext executes a statement that is not expected to return rows.
func (*Schema) OneContext ¶
OneContext executes a query that must return exactly one row.
func (*Schema) Open ¶
Open returns a database/sql pool whose unqualified statements use this schema. Close the pool before closing its DB.
func (*Schema) QueryContext ¶
func (s *Schema) QueryContext(ctx context.Context, statement string, arguments ...any) (Result, error)
QueryContext executes and fully materializes a row-returning statement.
func (*Schema) RegisterFunction ¶
RegisterFunction registers or atomically replaces one scalar function overload in this schema. Fixed overloads are selected before a matching variadic overload.
type Subscription ¶
type Subscription struct {
// contains filtered or unexported fields
}
Subscription controls one query-interceptor registration.
func (*Subscription) Unsubscribe ¶
func (s *Subscription) Unsubscribe()
Unsubscribe removes the interceptor. It is safe to call more than once and concurrently with query execution. An invocation already in progress may finish.
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
Table exposes a small raw-row API for test setup and inspection. SQL remains the primary interface; Table is useful when a fixture is clearer as Go data.
func (*Table) Find ¶
Find returns rows whose columns equal every non-nil template entry. A nil template value matches SQL NULL.
func (*Table) FindContext ¶
FindContext returns rows whose columns equal every template entry.
func (*Table) Insert ¶
Insert inserts a raw row and returns the stored row after defaults and generated values have been applied.
func (*Table) InsertContext ¶
InsertContext inserts a raw row and returns the stored row after defaults and generated values have been applied.