database

package
v0.0.27 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package database is a generated GoMock package.

Package database is a generated GoMock package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddFlags

func AddFlags(flags *pflag.FlagSet)

AddFlags adds to the given flag set the flags needed to configure the database tool. For example:

database.AddFlags(flags)

Will add the following flags:

--db-url Database connection URL.

func TxIntoContext

func TxIntoContext(ctx context.Context, tx Tx) context.Context

TxIntoContext adds the given transaction into the provided context and returns the new context containing it.

func TxManagerIntoContext

func TxManagerIntoContext(ctx context.Context, tx TxManager) context.Context

TxManagerIntoContext adds the given transaction manager into the provided context and returns the new context containing it.

Types

type Listener

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

Listener knows how to liste for notifications using PostgreSQL's `listen` mechanism.

func (*Listener) Listen

func (l *Listener) Listen(ctx context.Context) error

Listen waits for notifications in the configured channel, decodes the payload and runs the callbacks to process it.

type ListenerBuilder

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

ListenerBuilder contains the data and logic needed to build a listener.

func NewListener

func NewListener() *ListenerBuilder

NewListener uses the information stored in the builder to create a new listener.

func (*ListenerBuilder) AddPayloadCallback

func (b *ListenerBuilder) AddPayloadCallback(value ListenerPayloadCallback) *ListenerBuilder

AddPayloadCallback adds a function that will be called by the listener when a notification arrives. Errors returned by these functions will be logged, but the listener will continue working. At least one of these functions is mandatory.

func (*ListenerBuilder) AddReadyCallback

func (b *ListenerBuilder) AddReadyCallback(value ListenerReadyCallback) *ListenerBuilder

AddReadyCallback adds a function that will be called by the listener when it is actually listening for notifications. Errors returned by these functions will be logged, but the listener will continue working. This is optional.

func (*ListenerBuilder) Build

func (b *ListenerBuilder) Build() (result *Listener, err error)

Build constructs a listener instance using the configured parameters.

func (*ListenerBuilder) SetChannel

func (b *ListenerBuilder) SetChannel(value string) *ListenerBuilder

SetChannel sets the channel name for the listener. This is mandatory.

func (*ListenerBuilder) SetLogger

func (b *ListenerBuilder) SetLogger(logger *slog.Logger) *ListenerBuilder

SetLogger sets the logger for the listener. This is mandatory.

func (*ListenerBuilder) SetRetryInterval

func (b *ListenerBuilder) SetRetryInterval(value time.Duration) *ListenerBuilder

SetRetryInterval sets the time that the listener will wait before trying to open a new connectio and start listening after a failure. This is optional and the default is five seconds.

func (*ListenerBuilder) SetUrl

func (b *ListenerBuilder) SetUrl(value string) *ListenerBuilder

SetUrl sets the database connection URL. This is mandatory.

func (*ListenerBuilder) SetWaitTimeout

func (b *ListenerBuilder) SetWaitTimeout(value time.Duration) *ListenerBuilder

SetWaitTimeout sets the maximum time that the listener will wait for a notification. After that it will close the connection and open it again. This is intended to automatically recover from situations where the server or the connection malfunction and stop sending the notifications. This is optional and the default is five minutes.

type ListenerPayloadCallback

type ListenerPayloadCallback func(ctx context.Context, payload proto.Message) error

ListenerPayloadCallback is a function that will be called by the listener when a notification arrives.

type ListenerReadyCallback

type ListenerReadyCallback func(ctx context.Context) error

ListenerReadyCallback is a function that will be called by the listener when it is actually listening for notifications.

type MockTx

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

MockTx is a mock of Tx interface.

func NewMockTx

func NewMockTx(ctrl *gomock.Controller) *MockTx

NewMockTx creates a new mock instance.

func (*MockTx) EXPECT

func (m *MockTx) EXPECT() *MockTxMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockTx) Exec

func (m *MockTx) Exec(ctx context.Context, query string, args ...any) (pgconn.CommandTag, error)

Exec mocks base method.

func (*MockTx) Query

func (m *MockTx) Query(ctx context.Context, query string, args ...any) (pgx.Rows, error)

Query mocks base method.

func (*MockTx) QueryRow

func (m *MockTx) QueryRow(ctx context.Context, query string, args ...any) pgx.Row

QueryRow mocks base method.

func (*MockTx) ReportError

func (m *MockTx) ReportError(err *error)

ReportError mocks base method.

type MockTxManager

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

MockTxManager is a mock of TxManager interface.

func NewMockTxManager

func NewMockTxManager(ctrl *gomock.Controller) *MockTxManager

NewMockTxManager creates a new mock instance.

func (*MockTxManager) Begin

func (m *MockTxManager) Begin(ctx context.Context) (Tx, error)

Begin mocks base method.

func (*MockTxManager) EXPECT

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockTxManager) End

func (m *MockTxManager) End(ctx context.Context, tx Tx) error

End mocks base method.

type MockTxManagerMockRecorder

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

MockTxManagerMockRecorder is the mock recorder for MockTxManager.

func (*MockTxManagerMockRecorder) Begin

func (mr *MockTxManagerMockRecorder) Begin(ctx any) *gomock.Call

Begin indicates an expected call of Begin.

func (*MockTxManagerMockRecorder) End

func (mr *MockTxManagerMockRecorder) End(ctx, tx any) *gomock.Call

End indicates an expected call of End.

type MockTxMockRecorder

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

MockTxMockRecorder is the mock recorder for MockTx.

func (*MockTxMockRecorder) Exec

func (mr *MockTxMockRecorder) Exec(ctx, query any, args ...any) *gomock.Call

Exec indicates an expected call of Exec.

func (*MockTxMockRecorder) Query

func (mr *MockTxMockRecorder) Query(ctx, query any, args ...any) *gomock.Call

Query indicates an expected call of Query.

func (*MockTxMockRecorder) QueryRow

func (mr *MockTxMockRecorder) QueryRow(ctx, query any, args ...any) *gomock.Call

QueryRow indicates an expected call of QueryRow.

func (*MockTxMockRecorder) ReportError

func (mr *MockTxMockRecorder) ReportError(err any) *gomock.Call

ReportError indicates an expected call of ReportError.

type Notifier

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

Notifier knows how to send notifications using PostgreSQL's NOTIFY command, using protocol buffers messages as payload.

func (*Notifier) Notify

func (n *Notifier) Notify(ctx context.Context, payload proto.Message) (err error)

Notify sends a notification with the given payload using the configured channel. The payload is placed into an Any object, then marshalled using protocol buffers.

Note that this method expects to find a transaction in the context.

type NotifierBuilder

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

NotifierBuilder contains the data and logic needed to build a notifier.

func NewNotifier

func NewNotifier() *NotifierBuilder

NewNotifier uses the information stored in the builder to create a new notifier.

func (*NotifierBuilder) Build

func (b *NotifierBuilder) Build() (result *Notifier, err error)

Build constructs a notifier instance using the configured parameters.

func (*NotifierBuilder) SetChannel

func (b *NotifierBuilder) SetChannel(value string) *NotifierBuilder

SetLogger sets the channel name. This is mandatory.

func (*NotifierBuilder) SetLogger

func (b *NotifierBuilder) SetLogger(logger *slog.Logger) *NotifierBuilder

SetLogger sets the logger for the notifier. This is mandatory.

type Tool

type Tool interface {
	// Wait waits till the database is available.
	Wait(ctx context.Context) error

	// Migrate runs the database migrations.
	Migrate(ctx context.Context) error

	// Pool returns the pool of database connections.
	Pool(ctx context.Context) (result *pgxpool.Pool, err error)

	// URL returns the database connection URL.
	URL() string
}

Tool tries to simplify and centralize database operations that are needed frequently during the startup of a process that uses the database, like creating the database connection string from the command line flags, waiting till the database is up and running, applying the migrations and creationg the database connection pool.

type ToolBuilder

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

func NewTool

func NewTool() *ToolBuilder

func (*ToolBuilder) Build

func (b *ToolBuilder) Build() (result Tool, err error)

func (*ToolBuilder) SetFlags

func (b *ToolBuilder) SetFlags(flags *pflag.FlagSet) *ToolBuilder

SetFlags sets the command line flags that should be used to configure the tool. This is optional.

func (*ToolBuilder) SetLogger

func (b *ToolBuilder) SetLogger(value *slog.Logger) *ToolBuilder

func (*ToolBuilder) SetURL

func (b *ToolBuilder) SetURL(value string) *ToolBuilder

type Tx

type Tx interface {
	// Query executes a SQL query against the database using the provided context, query string, and optional
	// arguments. It returns the resulting rows or an error if the query fails.
	Query(ctx context.Context, query string, args ...any) (result pgx.Rows, err error)

	// QueryRow executes a SQL query that is expected to return a single row. It uses the provided context, query
	// string, and optional arguments. The returned row can be used to scan the result into variables.
	QueryRow(ctx context.Context, query string, args ...any) pgx.Row

	// Exec executes a SQL statement against the database using the provided context, query string, and optional
	// arguments. It returns the result of the execution or an error if the execution fails.
	Exec(ctx context.Context, query string, args ...any) (tag pgconn.CommandTag, err error)

	// ReportError adds a error that the transaction manager will use to determine if the transaction should be
	// commited or rolled back. The default behaviour is that if there are no errors reported, or if they are all
	// nil then the transaction will be commited. Otherwise it will be rolled back.
	//
	// The recommended way to use this is with the defer mechanism:
	//
	//	func myWork(ctx context.Context) (err error) {
	//		// Get the transaction and remember to report the errors:
	//		tx, err := database.TxFromContext(ctx)
	//		if err != nil {
	//			return
	//		}
	//		defer tx.ReportError(&err)
	//
	//		// Do the actual work, which will update the returned 'err'.
	//
	//		return
	//	}
	//
	// Note that value passed is a pointer to a error. This is needed to make possible the recommended usage above,
	// because if the error was passed by value then it most cases it will be nil, because it will be evaluated at
	// the time of execution of the 'defer' statement, not at the time of execution of deferred ReportError
	// function.
	//
	// It this method is called multiple times for the same transaction the reported errors will be accumulated.
	ReportError(err *error)
}

Tx is a database transaction automatically started and automatically commited or rolled back.

func TxFromContext

func TxFromContext(ctx context.Context) (tx Tx, err error)

TxFromContext retrieves the transaction from the provided context.

type TxInterceptor

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

TxInterceptor contains the data needed by the interceptor.

func (*TxInterceptor) UnaryServer

func (i *TxInterceptor) UnaryServer(ctx context.Context, request any, info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler) (response any, err error)

UnaryServer is the unary server interceptor function.

type TxInterceptorBuilder

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

TxInterceptorBuilder contains the data and logic needed to build an interceptor that begins and ends transactions automatically. Don't create instances of this type directly, use the NewTxInterceptor function instead.

func NewTxInterceptor

func NewTxInterceptor() *TxInterceptorBuilder

NewTxInterceptor creates a builder that can then be used to configure and create a transactions interceptor.

func (*TxInterceptorBuilder) Build

func (b *TxInterceptorBuilder) Build() (result *TxInterceptor, err error)

Build uses the data stored in the builder to create and configure a new interceptor.

func (*TxInterceptorBuilder) SetLogger

func (b *TxInterceptorBuilder) SetLogger(value *slog.Logger) *TxInterceptorBuilder

SetLogger sets the logger that will be used to write to the log. This is mandatory.

func (*TxInterceptorBuilder) SetManager

func (b *TxInterceptorBuilder) SetManager(value TxManager) *TxInterceptorBuilder

SetManager sets the transaction manager that will be used to begin and end transactions. This is mandatory.

type TxManager

type TxManager interface {
	// Begin starts a new transaction.
	Begin(ctx context.Context) (Tx, error)

	// End finishes a transaction. It will be commited or rolled back according to the errors that have been
	// reported during its execution. See the ReportError of the Tx interface for details. Note that this only
	// supports transactions created with the Begin method of the same transaction manager.
	End(ctx context.Context, tx Tx) error
}

TxManager is a database transaction manager. It knows how to start, commit and rollback transactions.

func TxManagerFromContext

func TxManagerFromContext(ctx context.Context) (tm TxManager, err error)

TxManagerFromContext retrieves the transaction manager from the provided context.

type TxManagerBuilder

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

TxManagerBuilder is a builder responsible for constructing database transaction managers. Don't create instances of this type directly, use the NewTxManager function instead.

func NewTxManager

func NewTxManager() *TxManagerBuilder

NewTxManager creates a builder that can then be used to initializa a new transaction manager.

func (*TxManagerBuilder) Build

func (b *TxManagerBuilder) Build() (result TxManager, err error)

Build uses the information stored in the builder to create a new transaction manager.

func (*TxManagerBuilder) SetLogger

func (b *TxManagerBuilder) SetLogger(value *slog.Logger) *TxManagerBuilder

SetLogger sets the logger that the transaction manager will use to write to the log. This is mandatory.

func (*TxManagerBuilder) SetPool

func (b *TxManagerBuilder) SetPool(value *pgxpool.Pool) *TxManagerBuilder

SetPool sets the database connection pool that the transaction manager will use to create transactions. This is mandatory.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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