psqldocker

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: MIT Imports: 9 Imported by: 0

README

adrianbrad psqldocker

🚢 psqldocker GitHub release

powered by ory/dockertest.

GitHub go.mod Go version of a Go module GoDoc reference example

CodeFactor Go Report Card codecov

lint-test grype codeql gitleaks


Go package providing lifecycle management for PostgreSQL Docker instances.

Here is an article expanding on the usage of this package.

Leverage Docker to run unit and integration tests against a real PostgreSQL database.

Usage
package foo_test

import (
	"testing"

	"github.com/adrianbrad/psqldocker"
)

func TestXxx(t *testing.T) {
    const schema = "CREATE TABLE users(user_id UUID PRIMARY KEY);"
	
    c, err := psqldocker.NewContainer(
        "user",
        "password",
        "dbName",
        psqldocker.WithContainerName("test"), 
        // initialize with a schema
        psqldocker.WithSql(schema),
        // you can add other options here
    )
    if err != nil {
        t.Fatalf("cannot start new psql container: %s\n", err)
    }
	
    t.Cleanup(func() {
        err = c.Close()
        if err != nil {
            t.Logf("err while closing conainter: %w", err)
        }
    })
	
    t.Run("Subtest", func(t *testing.T) {
        // execute your test logic here 
    })
}

In a TestMain function
package foo_test

import (
	"log"
	"testing"

	"github.com/adrianbrad/psqldocker"
)

func TestMain(m *testing.M) {
    const schema = "CREATE TABLE users(user_id UUID PRIMARY KEY);"

    c, err := psqldocker.NewContainer(
        "user",
        "password",
        "dbName",
        psqldocker.WithContainerName("test"), 
        // initialize with a schema
        psqldocker.WithSql(schema),
        // you can add other options here
    )
    if err != nil {
        log.Fatalf("cannot start new psql container: %s\n", err)
    }
	
    defer func() {
        err = c.Close()
        if err != nil {
            log.Printf("err while closing conainter: %w", err)
        }
    }() 
	
    m.Run()
}

Documentation

Overview

Package psqldocker provides functions to start, stop and configure a PostgreSQL docker container. The purpose of this package is mainly for testing.

The docker container is created programatically using the github.com/ory/dockertest package.

Index

Constants

This section is empty.

Variables

View Source
var ErrWithPoolAndWithPoolEndpoint = errors.New(
	"with pool and with pool endpoint are mutually exclusive",
)

ErrWithPoolAndWithPoolEndpoint is returned when both WithPool and WithPoolEndpoint options are given to the NewContainer constructor.

Functions

This section is empty.

Types

type Container

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

Container represents a Docker container running a PostgreSQL image.

func NewContainer

func NewContainer(
	user,
	password,
	dbName string,
	opts ...Option,
) (*Container, error)

NewContainer starts a new psql database in a docker container.

func (*Container) Close

func (c *Container) Close() error

Close removes the Docker container.

func (*Container) Port

func (c *Container) Port() string

Port returns the container host port mapped to the database running inside it.

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option configures an BTC Node Docker.

func WithContainerName

func WithContainerName(name string) Option

WithContainerName configures the PSQL Container Name, if empty, a random one will be picked.

func WithDBPort

func WithDBPort(name string) Option

WithDBPort sets database port running in the container, default 5432.

func WithExpiration added in v1.1.0

func WithExpiration(seconds uint) Option

WithExpiration terminates the container after a period has passed.

func WithImageTag

func WithImageTag(tag string) Option

WithImageTag configures the PSQL Container image tag, default: alpine.

func WithPingRetryTimeout added in v1.1.0

func WithPingRetryTimeout(seconds uint) Option

WithPingRetryTimeout sets the timeout in seconds for the ping retry function.

func WithPool

func WithPool(pool *dockertest.Pool) Option

WithPool sets the docker container getPool. ! This is mutually exclusive with WithPoolEndpoint, and an error will be thrown if both are used.

func WithPoolEndpoint added in v1.1.0

func WithPoolEndpoint(endpoint string) Option

WithPoolEndpoint sets the docker container pool endpoint. ! This is mutually exclusive with WithPool, and an error will be thrown if both are used.

func WithSQL added in v1.1.5

func WithSQL(sql string) Option

WithSQL specifies a sqls file, to initiate the db with.

func WithTimescaleDB added in v1.2.0

func WithTimescaleDB() Option

WithTimescaleDB allows using the TimescaleDB repository and images. This option also updates the image tag to 'latest-pg15' as the default alpine is invalid.

Jump to

Keyboard shortcuts

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