pgtestdb

package module
v0.0.0-...-20c5a8e Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 9 Imported by: 6

README

Documentation

Index

Constants

View Source
const (
	// TestUser is the username for connecting to each test database.
	TestUser = "pgtdbuser"
	// TestPassword is the password for connecting to each test database.
	TestPassword = "pgtdbpass"
)

Variables

This section is empty.

Functions

func New

func New(t testing.TB, conf Config, migrator Migrator) *sql.DB

New connects to a postgres server and creates and connects to a fresh database instance. This database is prepared and migrated by the given migrator, by get-or-creating a template database and then cloning it. This is a concurrency-safe primitive. If there is an error creating the database, the test will be immediately failed with `t.Fatal()`.

If this method succeeds, it will `t.Log()` the connection string to the created database, so that if your test fails, you can connect to the database manually and see what happened.

If this method succeeds and your test succeeds, the database will be removed as part of the test cleanup process.

`testing.TB` is the common testing interface implemented by `*testing.T`, `*testing.B`, and `*testing.F`, so you can use pgtestdb to get a database for tests, benchmarks, and fuzzes.

Types

type Config

type Config struct {
	DriverName string // the name of a driver to use when calling sql.Open() to connect to a database
	Host       string // the host of the database, "localhost"
	Port       string // the port of the database, "5433"
	User       string // the user to connect as, "postgres"
	Password   string // the password to connect with, "password"
	Database   string // the database to connect to, "postgres"
	Options    string // URL-formatted additional options to pass in the connection string, "sslmode=disable&something=value"
}

Config contains the details needed to connect to a postgres server/database.

func Custom

func Custom(t testing.TB, conf Config, migrator Migrator) *Config

Custom is like New but after creating the new database instance, it closes any connections and returns the configuration details of that database so that you can connect to it explicitly, potentially via a different SQL interface.

func (Config) Connect

func (c Config) Connect() (*sql.DB, error)

Connect calls `sql.Open()“ and connects to the database.

func (Config) URL

func (c Config) URL() string

URL returns a postgres connection string in the format "postgres://user:password@host:port/database?options=..."

type Migrator

type Migrator interface {
	// Hash should return a unique identifier derived from the state of the database
	// after it has been fully migrated. For instance, it may return a hash of all
	// of the migration names and contents.
	//
	// pgtestdb will use the returned Hash to identify a template database. If a
	// Migrator returns a Hash that has already been used to create a template
	// database, it is assumed that the database need not be recreated since it
	// would result in the same schema and data.
	Hash() (string, error)

	// Prepare should perform any plugin or extension installations necessary to
	// make the database ready for the migrations. For instance, you may want to
	// enable certain extensions like `trigram` or `pgcrypto`, or creating or
	// altering certain roles and permissions.
	// Prepare will be given a *sql.DB connected to the template database.
	Prepare(context.Context, *sql.DB, Config) error

	// Migrate is a function that actually performs the schema and data
	// migrations to provision a template database. The connection given to this
	// function is to an entirely new, empty, database. Migrate will be called
	// only once, when the template database is being created.
	Migrate(context.Context, *sql.DB, Config) error

	// Verify is called each time you ask for a new test database instance. It
	// should be cheaper than the call to Migrate(), and should return nil iff
	// the database is in the correct state. An example implementation would be
	// to check that all the migrations have been marked as applied, and
	// otherwise return an error.
	Verify(context.Context, *sql.DB, Config) error
}

A Migrator is necessary to provision and verify the database that will be used as as template for each test.

type NoopMigrator

type NoopMigrator struct{}

NoopMigrator fulfills the Migrator interface but does absolutely nothing. You can use this to get empty databases in your tests, or if you are trying out testdb and aren't sure which migrator to use yet.

For more documentation on migrators, see https://github.com/kodergarten/pgtestdb#testdbmigrator

func (NoopMigrator) Hash

func (NoopMigrator) Hash() (string, error)

func (NoopMigrator) Migrate

func (NoopMigrator) Migrate(_ context.Context, _ *sql.DB, _ Config) error

func (NoopMigrator) Prepare

func (NoopMigrator) Prepare(_ context.Context, _ *sql.DB, _ Config) error

func (NoopMigrator) Verify

func (NoopMigrator) Verify(_ context.Context, _ *sql.DB, _ Config) error

Directories

Path Synopsis
internal
multierr
multierr is a reimplementation of https://github.com/uber-go/multierr/ with a much simpler API and zero dependencies.
multierr is a reimplementation of https://github.com/uber-go/multierr/ with a much simpler API and zero dependencies.
once
once contains helpers for constructing type-safe, concurrency-safe values that are only ever initialized once, and can potentially return an error.
once contains helpers for constructing type-safe, concurrency-safe values that are only ever initialized once, and can potentially return an error.
sessionlock
sessionlock package provides support for application level distributed locks via advisory locks in PostgreSQL.
sessionlock package provides support for application level distributed locks via advisory locks in PostgreSQL.
withdb
withdb is a simplified way of creating test databases, used to test the internal packages that pgtestdb depends on.
withdb is a simplified way of creating test databases, used to test the internal packages that pgtestdb depends on.
migrators
atlasmigrator Module
goosemigrator Module
pgmigrator Module
sqlmigrator Module

Jump to

Keyboard shortcuts

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