postgres

package
v3.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2024 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	DBHost                string        // DBHost represents the database host
	DBPort                int           // DBPort is the database port
	DBName                string        // DBName is the database name
	DBUser                string        // DBUser is the database user used to connect
	DBPassword            string        // DBPassword is the database password
	DBSchema              string        // DBSchema represents the database schema
	MaxConnections        int           // MaxConnections represents the number of max connections in the pool
	MinConnections        int           // MinConnections represents the number of minimum connections in the pool
	MaxConnectionLifetime time.Duration // MaxConnectionLifetime represents the duration since creation after which a connection will be automatically closed.
	MaxConnIdleTime       time.Duration // MaxConnIdleTime is the duration after which an idle connection will be automatically closed by the health check.
	HealthCheckPeriod     time.Duration // HeathCheckPeriod is the duration between checks of the health of idle connections.
}

Config defines the postgres configuration This configuration does not take into consideration the SSL mode TODO: enhance with SSL mode

func NewConfig

func NewConfig(host string, port int, user, password, dbName string) *Config

NewConfig creates an instance of Config

type Postgres

type Postgres interface {
	// Connect connects to the underlying database
	Connect(ctx context.Context) error
	// Disconnect closes the underlying opened underlying connection database
	Disconnect(ctx context.Context) error
	// Select fetches a single row from the database and automatically scanned it into the dst.
	// It returns an error in case of failure. When there is no record no errors is return.
	Select(ctx context.Context, dst any, query string, args ...any) error
	// SelectAll fetches a set of rows as defined by the query and scanned those record in the dst.
	// It returns nil when there is no records to fetch.
	SelectAll(ctx context.Context, dst any, query string, args ...any) error
	// Exec executes an SQL statement against the database and returns the appropriate result or an error.
	Exec(ctx context.Context, query string, args ...any) (pgconn.CommandTag, error)
	// BeginTx helps start an SQL transaction. The return transaction object is expected to be used in
	// the subsequent queries following the BeginTx.
	BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, error)
}

Postgres will be implemented by concrete RDBMS store

func New

func New(config *Config) Postgres

New returns a store connecting to the given Postgres database.

type TestContainer

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

TestContainer helps creates a Postgres docker container to run unit tests

func NewTestContainer

func NewTestContainer(dbName, dbUser, dbPassword string) *TestContainer

NewTestContainer create a Postgres test container useful for unit and integration tests This function will exit when there is an error.Call this function inside your SetupTest to create the container before each test.

func (TestContainer) Cleanup

func (c TestContainer) Cleanup()

Cleanup frees the resource by removing a container and linked volumes from docker. Call this function inside your TearDownSuite to clean-up resources after each test

func (TestContainer) GetTestDB

func (c TestContainer) GetTestDB() *TestDB

GetTestDB returns a Postgres TestDB that can be used in the tests to perform some database queries

func (TestContainer) Host

func (c TestContainer) Host() string

Host return the host of the test container

func (TestContainer) Port

func (c TestContainer) Port() int

Port return the port of the test container

func (TestContainer) Schema

func (c TestContainer) Schema() string

Schema return the test schema of the test container

type TestDB

type TestDB struct {
	Postgres
}

TestDB is used in test to perform some database queries

func (TestDB) Count

func (c TestDB) Count(ctx context.Context, tableName string) (int, error)

Count utility function to help count the number of rows in a Postgres table. tableName is in the format: <schemaName.tableName>. e.g: public.users It returns -1 when there is an error

func (TestDB) CreateSchema

func (c TestDB) CreateSchema(ctx context.Context, schemaName string) error

CreateSchema helps create a test schema in a Postgres database

func (TestDB) DropSchema

func (c TestDB) DropSchema(ctx context.Context, schemaName string) error

DropSchema utility function to drop a database schema

func (TestDB) DropTable

func (c TestDB) DropTable(ctx context.Context, tableName string) error

DropTable utility function to drop a database table

func (TestDB) SchemaExists

func (c TestDB) SchemaExists(ctx context.Context, schemaName string) (bool, error)

SchemaExists helps check the existence of a Postgres schema. Very useful when implementing tests

func (TestDB) TableExists

func (c TestDB) TableExists(ctx context.Context, tableName string) error

TableExists utility function to help check the existence of table in Postgres tableName is in the format: <schemaName.tableName>. e.g: public.users

Jump to

Keyboard shortcuts

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