sql

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2024 License: MPL-2.0 Imports: 34 Imported by: 0

Documentation

Overview

Package sql implements persistent storage using the postgres database.

Index

Constants

View Source
const (
	InsertAction = "INSERT"
	UpdateAction = "UPDATE"
	DeleteAction = "DELETE"
)
View Source
const TestDatabaseURL = "OTF_TEST_DATABASE_URL"

Variables

View Source
var ErrSubscriptionTerminated = errors.New("broker terminated the subscription")

ErrSubscriptionTerminated is for use by subscribers to indicate that their subscription has been terminated by the broker.

Functions

func Bool

func Bool(b bool) pgtype.Bool

Bool converts a go-boolean into a postgres non-null boolean

func BoolPtr

func BoolPtr(s *bool) pgtype.Bool

BoolPtr converts a go-boolean pointer into a postgres nullable boolean

func Error

func Error(err error) error

func Inet

func Inet(address net.IP) net.IPNet

Inet is a shim that converts a net.IP into a new.IPNet.

func Int4

func Int4(s int) pgtype.Int4

Int4 converts a go-int into a postgres non-null int4

func Int4Ptr

func Int4Ptr(s *int) pgtype.Int4

Int4Ptr converts a go-int pointer into a postgres nullable int4

func Int8

func Int8(s int) pgtype.Int8

Int8 converts a go-int into a postgres non-null int8

func Int8Ptr

func Int8Ptr(s *int) pgtype.Int8

Int8Ptr converts a go-int pointer into a postgres nullable int8

func NewTestContainer added in v0.10.0

func NewTestContainer(t TestingT) *postgres.PostgresContainer

NewTestContainer returns a new test container.

func NewTestDB

func NewTestDB(t TestingT) string

NewTestDB creates a logical database in postgres for a test and returns a connection string for connecting to the database. The database is dropped upon test completion.

func NoRowsInResultError

func NoRowsInResultError(err error) bool

func NullString

func NullString() pgtype.Text

NullString returns a postgres null string

func Query added in v0.10.0

func Query[T any](ctx context.Context, pool *Pool, fn func(context.Context, pggen.Querier) (T, error)) (T, error)

Query obtains a connection for the pool, executes the given function, and returns the connection to the pool.

func String

func String(s string) pgtype.Text

String converts a go-string into a postgres non-null string

func StringPtr

func StringPtr(s *string) pgtype.Text

StringPtr converts a go-string pointer into a postgres nullable string

func Timestamptz

func Timestamptz(t time.Time) pgtype.Timestamptz

Timestamptz converts a go-time into a postgres non-null timestamptz

func TimestamptzPtr

func TimestamptzPtr(t *time.Time) pgtype.Timestamptz

TimestamptzPtr converts a go-time pointer into a postgres nullable timestamptz

func Tx added in v0.10.0

func Tx[T any](ctx context.Context, pool *Pool, fn func(context.Context, pggen.Querier) (T, error)) (T, error)

Tx obtains a transaction from the pool, executes the given fn, and then commits the transaction.

func UUID

func UUID(s uuid.UUID) pgtype.UUID

UUID converts a google-go-uuid into a postgres non-null UUID

Types

type Action

type Action string

Action is the action that was carried out on a database table

type ForwardFunc

type ForwardFunc func(ctx context.Context, id string, action Action)

ForwardFunc handles forwarding the id and action onto subscribers.

type Listener

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

Listener listens for postgres events

func NewListener

func NewListener(logger *slog.Logger, db pool) *Listener

func (*Listener) RegisterFunc

func (b *Listener) RegisterFunc(table string, getter ForwardFunc)

RegisterFunc registers a function that is capable of converting database events for the given table into an OTF event.

func (*Listener) Start

func (b *Listener) Start(ctx context.Context) error

Start the pubsub daemon; listen to notifications from postgres and forward to local pubsub broker. The listening channel is closed once the broker has started listening; from this point onwards published messages will be forwarded.

func (*Listener) Started

func (b *Listener) Started() <-chan struct{}

type Options

type Options struct {
	Logger     *slog.Logger
	ConnString string
}

Options for constructing a DB

type Pool added in v0.10.0

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

Pool provides access to the postgres db as well as queries generated from SQL

func New

func New(ctx context.Context, opts Options) (*Pool, error)

New constructs a new DB connection pool, and migrates the schema to the latest version.

func NewTestContainerPool added in v0.10.0

func NewTestContainerPool(t TestingT) (*postgres.PostgresContainer, *Pool)

NewTestContainerPool returns a new Pool that is connected to the returned PostgresContainer.

func TestContainerReset added in v0.10.0

func TestContainerReset(t TestingT, pg *postgres.PostgresContainer) *Pool

TestContainerReset resets the test container and provides a new pool that connects to it.

func (*Pool) Acquire added in v0.10.0

func (p *Pool) Acquire(ctx context.Context) (*pgxpool.Conn, error)

Acquire returns a new connection from the pool.

func (*Pool) Close added in v0.10.0

func (p *Pool) Close()

Close releases the pool.

func (*Pool) Exec added in v0.10.0

func (p *Pool) Exec(ctx context.Context, sql string, arguments ...any) (pgconn.CommandTag, error)

Exec executres the given sql.

func (*Pool) Lock added in v0.10.0

func (p *Pool) Lock(ctx context.Context, table string, fn func(context.Context, pggen.Querier) error) error

func (*Pool) Query added in v0.10.0

func (p *Pool) Query(ctx context.Context, callback func(context.Context, pggen.Querier) error) error

Query obtains a connection for the pool, executes the given function, and returns the connection to the pool.

func (*Pool) Tx added in v0.10.0

func (p *Pool) Tx(ctx context.Context, callback func(context.Context, pggen.Querier) error) error

Tx provides the caller with a callback in which all operations are conducted within a transaction.

func (*Pool) WaitAndLock added in v0.10.0

func (db *Pool) WaitAndLock(ctx context.Context, id int64, fn func(context.Context) error) (err error)

WaitAndLock obtains an exclusive session-level advisory lock. If another session holds the lock with the given id then it'll wait until the other session releases the lock. The given fn is called once the lock is obtained and when the fn finishes the lock is released.

type TestingT added in v0.10.0

type TestingT interface {
	require.TestingT
	assert.TestingT
	Helper()
	Name() string
	Skip(...any)
	Cleanup(f func())
}

TestingT is used instead of directly relying on testing.T to prevent the package from being bundled in a normal release build of tofutf.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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