postgres

package
v0.0.0-...-a6a2b61 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect

func Connect(ctx context.Context, databaseURL string) (*sql.DB, error)

Connect to a PostgreSQL server and connection check.

func ConnectionString

func ConnectionString(host string, port uint16, name, sslMode, user, pass string) string

ConnectionString builds the connection string from the individual components.

func CreateDatabaseIfNotExists

func CreateDatabaseIfNotExists(ctx context.Context, databaseURL string) (bool, error)

CreateDatabaseIfNotExists creates a PostgrSQL database if it doesn't already exist.

func DatabaseExists

func DatabaseExists(ctx context.Context, databaseURL string) (bool, error)

DatabaseExists checks if the given database on a PostgreSQL server does exist.

func DropDatabaseIfExists

func DropDatabaseIfExists(ctx context.Context, databaseURL string) (bool, error)

DropDatabaseIfExists drops a PostgrSQL database if it exist.

func IsDBNotExists

func IsDBNotExists(err error) bool

IsDBNotExists returns true if the given error indicates error code 3D000: database "..." does not exist.

func IsDup

func IsDup(err error) bool

IsDup returns true if the given error indicates that a duplicate record has been found (23505 unique_violation).

func IsDupDB

func IsDupDB(err error) bool

IsDupDB returns true if the given error indicates the database already exists. This is typically returned from the `CREATE DATABASE dbname` command if `dbname` already exists (42P04 database "..." already exists).

func IsDupUser

func IsDupUser(err error) bool

IsDupUser returns true if the given error indicates the user/role already exists. This is typically returned from the `CREATE ROLE user ...` command if `user` already exists (42710 role "..." already exists).

func IsForeignKeyViolation

func IsForeignKeyViolation(err error) bool

IsForeignKeyViolation returns true if the given error indicates a violation of a foreign key constraint (23503 foreign_key_violation).

func IsNotFound

func IsNotFound(err error) bool

IsNotFound returns true if the given error indicates that a record could not be found.

func IsPSQLError

func IsPSQLError(err error, code string) bool

IsPSQLError returns true if the given error is from PostgreSQL and has the given error code.

See https://www.postgresql.org/docs/13/errcodes-appendix.html for a list of all PostgreSQL error codes.

func IsPerm

func IsPerm(err error) bool

IsPerm returns true if the given error indicates a permission issue (42501 insufficient_privilege).

func WithDatabaseName

func WithDatabaseName(databaseName string) startConfigFunc

func WithInMemory

func WithInMemory(inMemory bool) startConfigFunc

func WithIsTemplate

func WithIsTemplate(isTemplate bool) startConfigFunc

func WithPostStart

func WithPostStart(funcs ...postStartFunc) startConfigFunc

WithPostStart adds a post-startup operation to the container. This can be used to install extensions, create tables, seed data etc.

func WithTimeout

func WithTimeout(timeout time.Duration) startConfigFunc

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

func Start

func Start(tb testing.TB, options ...startConfigFunc) *Container

Start a PostgreSQL container.

func (*Container) Close

func (c *Container) Close() error

func (*Container) ConnConfig

func (c *Container) ConnConfig() *pgx.ConnConfig

func (*Container) DB

func (c *Container) DB() *sql.DB

func (*Container) StartFromTemplate

func (c *Container) StartFromTemplate(tb testing.TB) (*sql.DB, *pgx.ConnConfig, func() error)

type ContainerCache

type ContainerCache struct {
	// contains filtered or unexported fields
}

ContainerCache is a thread-safe cache for Postgres containers.

func NewContainerCache

func NewContainerCache() *ContainerCache

NewContainerCache returns a new ContainerCache.

func (*ContainerCache) Close

func (p *ContainerCache) Close() error

Close stops all containers in the cache.

func (*ContainerCache) GetOrCreate

func (p *ContainerCache) GetOrCreate(id string, createFunc func() *Container) *Container

GetOrCreate starts a new container if none is running, otherwise returns the pooled container.

Jump to

Keyboard shortcuts

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