entdb

package
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package entdb extends the ent db library and satisfies matt's needs for consistency

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDriverLackingBeginTx is returned when the driver does not support BeginTx
	ErrDriverLackingBeginTx = errors.New("driver does not support BeginTx")
	// ErrShuttingDown is returned when operations are attempted during shutdown
	ErrShuttingDown = errors.New("database shutting down")
)

Functions

func BeginShutdown

func BeginShutdown()

BeginShutdown marks the system as shutting down. It is safe to call multiple times

func BlockHistoryInterceptor

func BlockHistoryInterceptor() historygenerated.Interceptor

BlockHistoryInterceptor returns an ent.Interceptor that prevents history queries once shutdown starts

func BlockHistoryInterceptorWithFlag

func BlockHistoryInterceptorWithFlag(f *ShutdownFlag) historygenerated.Interceptor

BlockHistoryInterceptorWithFlag returns an interceptor tied to the provided shutdown flag it works similarly to BlockHistoryInterceptor, but is used for intercepting history queries it checks the shutdown flag before allowing the query to proceed if the flag is set, it returns ErrShuttingDown and blocks the query if the flag is NOT set, it allows the query to proceed this is useful for preventing new queries from being executed while the system is shutting down

func BlockHook

func BlockHook() ent.Hook

BlockHook returns an ent.Hook that prevents mutations after shutdown begins

func BlockHookWithFlag

func BlockHookWithFlag(f *ShutdownFlag) ent.Hook

BlockHookWithFlag returns an ent.Hook tied to the provided shutdown flag it's added as a global hook to the ent.Client, so it's attached to all mutations the hook wraps the next mutator in the chain; before allowing the mutation to proceed, it checks the shutdown flag, and returns an error and blocks the mutation if the flag is NOT set, it just calls the next mutator in the chain This function is intended to ensure data consistency and avoid partial writes or race conditions as the application is shutting down

func BlockInterceptor

func BlockInterceptor() ent.Interceptor

BlockInterceptor returns an ent.Interceptor that prevents queries once shutdown starts

func BlockInterceptorWithFlag

func BlockInterceptorWithFlag(f *ShutdownFlag) ent.Interceptor

BlockInterceptorWithFlag returns an interceptor tied to the provided shutdown flag it works similarly to BlockHook, but is used for intercepting queries it checks the shutdown flag before allowing the query to proceed if the flag is set, it returns ErrShuttingDown and blocks the query if the flag is NOT set, it allows the query to proceed this is useful for preventing new queries from being executed while the system is shutting down

func EnablePostgresExtensions

func EnablePostgresExtensions(db *sql.DB) error

EnablePostgresExtensions enables the postgres extensions needed when running migrations

func EnablePostgresOption

func EnablePostgresOption(db *sql.DB) schema.MigrateOption

EnablePostgresOption returns a schema.MigrateOption that will enable the Postgres extension if needed for running atlas migrations

func GracefulClose

func GracefulClose(ctx context.Context, c *ent.Client, interval time.Duration) error

GracefulClose waits for in-flight queries to finish before closing the database connections

func GracefulCloseWithFlag

func GracefulCloseWithFlag(ctx context.Context, c *ent.Client, interval time.Duration, f *ShutdownFlag) error

GracefulCloseWithFlag behaves like GracefulClose but uses the provided shutdown flag

func IsShuttingDown

func IsShuttingDown() bool

IsShuttingDown reports whether GracefulClose was invoked

func New

func New(ctx context.Context, c entx.Config, jobOpts []riverqueue.Option, clientOpts []Option, opts ...ent.Option) (*ent.Client, error)

New returns a ent client with a primary and secondary, if configured, write database

func NewHistory

func NewHistory(c entx.Config, opts ...historygenerated.Option) (*historygenerated.Client, error)

NewHistory returns a enthistory client with a primary database

func NewTestClient

func NewTestClient(ctx context.Context, ctr *testutils.TestFixture, jobOpts []riverqueue.Option, clientOpts []Option, entOpts []ent.Option) (*ent.Client, error)

NewTestClient creates an entdb client that can be used for TEST purposes ONLY. clientOpts allows passing entdb options like WithModules; pass nil if not needed.

func NewTestFixture

func NewTestFixture() *testutils.TestFixture

NewTestFixture creates a test container for testing purposes

func NewTestHistoryClient

func NewTestHistoryClient(ctx context.Context, ctr *testutils.TestFixture) (*historygenerated.Client, error)

NewTestHistoryClient creates a ent history client that can be used for TEST purposes ONLY

func ResetShutdown

func ResetShutdown()

ResetShutdown clears the shutdown flag. It is intended for tests

func SQLDB

func SQLDB(c dbClientWithDriver) *stdsql.DB

SQLDB unwraps the ent driver and returns the underlying *sql.DB

Types

type Option

type Option func(*ent.Client)

options for creating the ent client

func WithMetricsHook

func WithMetricsHook() Option

WithMetricsHook adds the metrics hook to the ent client

func WithModules

func WithModules() Option

WithModules adds the modules interceptor to the ent client

type ShutdownFlag

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

ShutdownFlag tracks whether a shutdown is in progress

func (*ShutdownFlag) Begin

func (s *ShutdownFlag) Begin()

Begin sets the shutdown flag to true, indicating that a shutdown is in progress

func (*ShutdownFlag) IsSet

func (s *ShutdownFlag) IsSet() bool

IsSet checks if the shutdown flag is set to true

func (*ShutdownFlag) Reset

func (s *ShutdownFlag) Reset()

Reset clears the shutdown flag, indicating that a shutdown is no longer in progress

Jump to

Keyboard shortcuts

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