postgres

package
v1.0.1-0...-336119c Latest Latest
Warning

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

Go to latest
Published: Jun 17, 2023 License: MIT Imports: 16 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
	MaxOpenConnections    int           // MaxOpenConnections represents the number of open connections in the pool
	MaxIdleConnections    int           // MaxIdleConnections represents the number of idle connections in the pool
	ConnectionMaxLifetime time.Duration // ConnectionMaxLifetime represents the connection max life time
}

Config is the postgres database configuration

type DBConn

type DBConn 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 interface{}, query string, args ...interface{}) 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 interface{}, query string, args ...interface{}) error
	// Exec executes an SQL statement against the database and returns the appropriate result or an error.
	Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, 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 *sql.TxOptions) (*sql.Tx, error)
}

DBConn will be implemented by concrete RDBMS storage

type Postgres

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

Postgres helps interact with the Postgres database

func New

func New(config *Config) *Postgres

New returns a storage connecting to the given Postgres database.

func (*Postgres) BeginTx

func (p *Postgres) BeginTx(ctx context.Context, txOptions *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a new database transaction

func (*Postgres) Connect

func (p *Postgres) Connect(ctx context.Context) error

Connect will connect to our Postgres database

func (*Postgres) Disconnect

func (p *Postgres) Disconnect(ctx context.Context) error

Disconnect the database connection.

func (*Postgres) Exec

func (p *Postgres) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

Exec executes a sql query without returning rows against the database

func (*Postgres) Select

func (p *Postgres) Select(ctx context.Context, dst interface{}, query string, args ...interface{}) error

Select fetches only one row

func (*Postgres) SelectAll

func (p *Postgres) SelectAll(ctx context.Context, dst interface{}, query string, args ...interface{}) error

SelectAll fetches rows

type SchemaUtils

type SchemaUtils struct {
	DBConn
	// contains filtered or unexported fields
}

SchemaUtils helps interact with the database and perform some postgres DDL task

func NewSchemaUtils

func NewSchemaUtils(db DBConn) *SchemaUtils

NewSchemaUtils returns an instance of SchemaUtils

func (SchemaUtils) CreateSchema

func (s SchemaUtils) CreateSchema(ctx context.Context, schemaName string) error

CreateSchema helps create a test schema in a Postgres database

func (SchemaUtils) DropSchema

func (s SchemaUtils) DropSchema(ctx context.Context, schemaName string) error

DropSchema utility function to drop a database schema

func (SchemaUtils) DropTable

func (s SchemaUtils) DropTable(ctx context.Context, tableName string) error

DropTable utility function to drop a database table

func (SchemaUtils) SchemaExists

func (s SchemaUtils) SchemaExists(ctx context.Context, schemaName string) (bool, error)

SchemaExists helps check the existence of a Postgres schema.

func (SchemaUtils) TableExists

func (s SchemaUtils) TableExists(ctx context.Context, schema, tableName string) (bool, error)

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

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 {
	DBConn
}

TestDB is used in test to perform some database queries

Jump to

Keyboard shortcuts

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