stdent

package
v0.0.148 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package stdent provides re-usable code for using Ent.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AttemptFromContext added in v0.0.50

func AttemptFromContext(ctx context.Context) int

AttemptFromContext returns which execution attempt it is. Panics if this information is not present.

func ContextWithAttempts added in v0.0.50

func ContextWithAttempts(ctx context.Context, v int) context.Context

ContextWithAttempts stores which execution attempt it is.

func ContextWithTx added in v0.0.50

func ContextWithTx[T Tx](ctx context.Context, tx T) context.Context

ContextWithTx returns a context with the Tx in it.

func NoTestForMaxQueryPlanCosts added in v0.0.91

func NoTestForMaxQueryPlanCosts(ctx context.Context) bool

NoTestForMaxQueryPlanCosts returns whether the cost check is disabled.

func Transact0 added in v0.0.50

func Transact0[T Tx](
	ctx context.Context,
	txr *Transactor[T],
	fnc func(ctx context.Context, tx T) error,
) (err error)

Transact0 runs Transact1 but without a value to return.

func Transact1 added in v0.0.50

func Transact1[T Tx, U any](
	ctx context.Context,
	txr *Transactor[T],
	fnc func(ctx context.Context, tx T) (U, error),
) (res U, err error)

Transact1 runs fnc in a transaction T derived from the provided Ent client while returning one value of type U. The implementation is taken from the official docs: https://entgo.io/docs/transactions#best-practices. If the context already has a transaction, it runs it in that one.

func TxFromContext added in v0.0.50

func TxFromContext[T Tx](ctx context.Context) T

TxFromContext will get a transaction of type T from the context or panic.

func WithNoTestForMaxQueryPlanCosts added in v0.0.91

func WithNoTestForMaxQueryPlanCosts(ctx context.Context) context.Context

WithNoTestForMaxQueryPlanCosts allow disabling the plan cost check.

Types

type Client added in v0.0.50

type Client[T Tx] interface {
	BeginTx(ctx context.Context, opts *entsql.TxOptions) (T, error)
}

Client defines an Ent client that begins transactions of type T.

type Driver added in v0.0.91

type Driver struct {
	entdialect.Driver
	// contains filtered or unexported fields
}

Driver is an opionated Ent driver that wraps a base driver but only allows interactions with the database to be done through a transaction with specific isolation properties and hooking any sql being executed.

func NewDriver added in v0.0.91

func NewDriver(
	base entdialect.Driver,
	opts ...DriverOption,
) *Driver

NewDriver inits the driver.

func (Driver) BeginTx added in v0.0.91

func (d Driver) BeginTx(ctx context.Context, opts *sql.TxOptions) (entdialect.Tx, error)

BeginTx calls the base driver's method if it's supported and calls our hook.

func (Driver) Exec added in v0.0.91

func (d Driver) Exec(_ context.Context, _ string, _, _ any) error

Exec executes a query that does not return records. For example, in SQL, INSERT or UPDATE. It scans the result into the pointer v. For SQL drivers, it is dialect/sql.Result.

func (Driver) Query added in v0.0.91

func (d Driver) Query(_ context.Context, _ string, _, _ any) error

Query executes a query that returns rows, typically a SELECT in SQL. It scans the result into the pointer v. For SQL drivers, it is *dialect/sql.Rows.

func (Driver) Tx added in v0.0.91

func (d Driver) Tx(ctx context.Context) (entdialect.Tx, error)

Tx will begin a transaction with linearizable isolation level.

type DriverOption added in v0.0.91

type DriverOption func(*Driver)

func BeginHook added in v0.0.91

func BeginHook(v func(
	ctx context.Context, sql *strings.Builder, tx entdialect.ExecQuerier) (*strings.Builder, error),
) DriverOption

BeginHook may be called right when the transaction has been setup. This allows injecting custom settings into transaction. For example to facilitate role switching and Row-level security. This can either be performed by extending the sql statement that is already being performed (perferred for simple operations). Or using the transaction concretely.

func DiscourageSequentialScans added in v0.0.91

func DiscourageSequentialScans() DriverOption

DiscourageSequentialScans will dis-incentivize the query planner to use sequential scans for all transactions. This is mainly useful with the TestForMaxQueryPlanCost option to assert that queries under testing are missing an index.

func TestForMaxQueryPlanCosts added in v0.0.91

func TestForMaxQueryPlanCosts(maxCost float64) DriverOption

TestForMaxQueryPlanCosts will enable EXPLAIN on every query that is executed with the driver and fail when the cost of the resulting query is above the maximum. Together with the enable_seqscan=OFF it can help test of infefficient queries do to missing indexes.

func TxExecQueryLoggingLevel added in v0.0.91

func TxExecQueryLoggingLevel(v zapcore.Level) DriverOption

TxExecQueryLoggingLevel configures the level at which transaction's exec and query sql logs are send to the logger.

type Option added in v0.0.50

type Option func(opts *options)

func IsolationLevel added in v0.0.50

func IsolationLevel(v sql.IsolationLevel) Option

IsolationLevel specifies the isolation level for new transactions.

func ReadOnly added in v0.0.50

func ReadOnly(v bool) Option

ReadOnly sets the transaction to be read-only.

func SerializationFailureCodes added in v0.0.50

func SerializationFailureCodes(v ...string) Option

SerializationFailureCodes configures which PostgreSQL error codes should be considered serialization failures.

func SerializationFailureMaxRetries added in v0.0.50

func SerializationFailureMaxRetries(v int) Option

SerializationFailureMaxRetries configures the maximum number of retries in case the transacted code encounters a serialization failure.

type Transactor added in v0.0.50

type Transactor[T Tx] struct {
	// contains filtered or unexported fields
}

func New added in v0.0.50

func New[T Tx](client Client[T], opts ...Option) *Transactor[T]

type Tx added in v0.0.50

type Tx interface {
	Commit() error
	Rollback() error
}

Tx describes the constraints for an Ent transaction.

type WTx added in v0.0.91

type WTx struct {
	entdialect.Tx
	MaxQueryPlanCosts float64
	// contains filtered or unexported fields
}

WTx wraps a Ent transaction to provide us with the ability to hook any sql before it's being executed. In our case we ant to fail tests when the to-be-executed query plan has a cost that is too high.

func (WTx) Exec added in v0.0.91

func (tx WTx) Exec(ctx context.Context, query string, args, v any) error

Exec executes a query that does not return records. For example, in SQL, INSERT or UPDATE. It scans the result into the pointer v. For SQL drivers, it is dialect/sql.Result.

func (WTx) ExecContext added in v0.0.115

func (tx WTx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

ExecContext implements a way to execute raw sql.

func (WTx) Query added in v0.0.91

func (tx WTx) Query(ctx context.Context, query string, args, v any) error

Query executes a query that returns rows, typically a SELECT in SQL. It scans the result into the pointer v. For SQL drivers, it is *dialect/sql.Rows.

func (WTx) QueryContext added in v0.0.115

func (tx WTx) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryContext implements a way to execute raw sql.

func (WTx) StandardTx added in v0.0.116

func (tx WTx) StandardTx() *sql.Tx

StandardTx returns the sql.Tx instance that this Ent transaction holds. This is useful for code that depends on that interface. It panics if the Ent transaction could not be converted to a *sql.Tx.

Directories

Path Synopsis
Package stdenttest is a utility for writing tests on ent transaction.
Package stdenttest is a utility for writing tests on ent transaction.

Jump to

Keyboard shortcuts

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