Documentation
¶
Overview ¶
Package database is a generated GoMock package.
Package database is a generated GoMock package.
Index ¶
- func AddFlags(flags *pflag.FlagSet)
- func TxIntoContext(ctx context.Context, tx Tx) context.Context
- func TxManagerIntoContext(ctx context.Context, tx TxManager) context.Context
- type Listener
- type ListenerBuilder
- func (b *ListenerBuilder) AddPayloadCallback(value ListenerPayloadCallback) *ListenerBuilder
- func (b *ListenerBuilder) AddReadyCallback(value ListenerReadyCallback) *ListenerBuilder
- func (b *ListenerBuilder) Build() (result *Listener, err error)
- func (b *ListenerBuilder) SetChannel(value string) *ListenerBuilder
- func (b *ListenerBuilder) SetLogger(logger *slog.Logger) *ListenerBuilder
- func (b *ListenerBuilder) SetRetryInterval(value time.Duration) *ListenerBuilder
- func (b *ListenerBuilder) SetUrl(value string) *ListenerBuilder
- func (b *ListenerBuilder) SetWaitTimeout(value time.Duration) *ListenerBuilder
- type ListenerPayloadCallback
- type ListenerReadyCallback
- type MockTx
- func (m *MockTx) EXPECT() *MockTxMockRecorder
- func (m *MockTx) Exec(ctx context.Context, query string, args ...any) (pgconn.CommandTag, error)
- func (m *MockTx) Query(ctx context.Context, query string, args ...any) (pgx.Rows, error)
- func (m *MockTx) QueryRow(ctx context.Context, query string, args ...any) pgx.Row
- func (m *MockTx) ReportError(err *error)
- type MockTxManager
- type MockTxManagerMockRecorder
- type MockTxMockRecorder
- type Notifier
- type NotifierBuilder
- type Tool
- type ToolBuilder
- type Tx
- type TxInterceptor
- type TxInterceptorBuilder
- type TxManager
- type TxManagerBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddFlags ¶
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 ¶
TxIntoContext adds the given transaction 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.
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 ¶
ListenerPayloadCallback is a function that will be called by the listener when a notification arrives.
type ListenerReadyCallback ¶
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) ReportError ¶
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 ¶
func (m *MockTxManager) EXPECT() *MockTxManagerMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
type MockTxManagerMockRecorder ¶
type MockTxManagerMockRecorder struct {
// contains filtered or unexported fields
}
MockTxManagerMockRecorder is the mock recorder for MockTxManager.
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.
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.
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.
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.