sqldb

package module
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2025 License: MIT Imports: 37 Imported by: 2

Documentation

Index

Constants

View Source
const (
	// DefaultNumTxRetries is the default number of times we'll retry a
	// transaction if it fails with an error that permits transaction
	// repetition.
	DefaultNumTxRetries = 20

	// DefaultRetryDelay is the default delay between retries. This will be
	// used to generate a random delay between 0 and this value.
	DefaultRetryDelay = time.Millisecond * 50

	// DefaultMaxRetryDelay is the default maximum delay between retries.
	DefaultMaxRetryDelay = time.Second
)
View Source
const (
	PostgresTag = "11"
)
View Source
const Subsystem = "SQLD"

Subsystem defines the logging code for this subsystem.

Variables

View Source
var (
	// TargetLatest is a MigrationTarget that migrates to the latest
	// version available.
	TargetLatest = func(mig *migrate.Migrate) error {
		return mig.Up()
	}

	// TargetVersion is a MigrationTarget that migrates to the given
	// version.
	TargetVersion = func(version uint) MigrationTarget {
		return func(mig *migrate.Migrate) error {
			return mig.Migrate(version)
		}
	}
)
View Source
var (
	// DefaultPostgresFixtureLifetime is the default maximum time a Postgres
	// test fixture is being kept alive. After that time the docker
	// container will be terminated forcefully, even if the tests aren't
	// fully executed yet. So this time needs to be chosen correctly to be
	// longer than the longest expected individual test run time.
	DefaultPostgresFixtureLifetime = 10 * time.Minute
)
View Source
var (
	// DefaultStoreTimeout is the default timeout used for any interaction
	// with the storage/database.
	DefaultStoreTimeout = time.Second * 10
)
View Source
var (
	// ErrRetriesExceeded is returned when a transaction is retried more
	// than the max allowed valued without a success.
	ErrRetriesExceeded = errors.New("db tx retries exceeded")
)

Functions

func ApplyMigrations added in v1.0.7

func ApplyMigrations(ctx context.Context, db *BaseDB,
	migrator MigrationExecutor, migrations []MigrationConfig) error

ApplyMigrations applies the provided migrations to the database in sequence. It ensures migrations are executed in the correct order, applying both custom migration functions and SQL migrations as needed.

func DisableLog

func DisableLog()

DisableLog disables all library log output. Logging output is disabled by default until UseLogger is called.

func ExecuteSQLTransactionWithRetry added in v1.0.1

func ExecuteSQLTransactionWithRetry(ctx context.Context, makeTx MakeTx,
	rollbackTx RollbackTx, txBody TxBody, onBackoff OnBackoff,
	numRetries int) error

ExecuteSQLTransactionWithRetry is a helper function that executes a transaction with retry logic. It will retry the transaction if it fails with a serialization error. The function will return an error if the transaction fails with a non-retryable error, the context is cancelled or the number of retries is exceeded.

func IsSerializationError

func IsSerializationError(err error) bool

IsSerializationError returns true if the given error is a serialization error.

func MapSQLError

func MapSQLError(err error) error

MapSQLError attempts to interpret a given error as a database agnostic SQL error.

func SQLInt32

func SQLInt32[T constraints.Integer](num T) sql.NullInt32

SQLInt32 turns a numerical integer type into the NullInt32 that sql/sqlc uses when an integer field can be permitted to be NULL.

We use this constraints.Integer constraint here which maps to all signed and unsigned integer types.

func SQLInt64

func SQLInt64[T constraints.Integer](num T) sql.NullInt64

SQLInt64 turns a numerical integer type into the NullInt64 that sql/sqlc uses when an integer field can be permitted to be NULL.

We use this constraints.Integer constraint here which maps to all signed and unsigned integer types.

func SQLStr

func SQLStr(s string) sql.NullString

SQLStr turns a string into the NullString that sql/sqlc uses when a string can be permitted to be NULL.

func SQLTime

func SQLTime(t time.Time) sql.NullTime

SQLTime turns a time.Time into the NullTime that sql/sqlc uses when a time can be permitted to be NULL.

func UseLogger

func UseLogger(logger btclog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using btclog.

Types

type BaseDB

type BaseDB struct {
	*sql.DB

	*sqlc.Queries
}

BaseDB is the base database struct that each implementation can embed to gain some common functionality.

func (*BaseDB) BeginTx

func (s *BaseDB) BeginTx(ctx context.Context, opts TxOptions) (*sql.Tx, error)

BeginTx wraps the normal sql specific BeginTx method with the TxOptions interface. This interface is then mapped to the concrete sql tx options struct.

type BatchedQuerier

type BatchedQuerier interface {
	// Querier is the underlying query source, this is in place so we can
	// pass a BatchedQuerier implementation directly into objects that
	// create a batched version of the normal methods they need.
	sqlc.Querier

	// BeginTx creates a new database transaction given the set of
	// transaction options.
	BeginTx(ctx context.Context, options TxOptions) (*sql.Tx, error)
}

BatchedQuerier is a generic interface that allows callers to create a new database transaction based on an abstract type that implements the TxOptions interface.

type BatchedTx

type BatchedTx[Q any] interface {
	// ExecTx will execute the passed txBody, operating upon generic
	// parameter Q (usually a storage interface) in a single transaction.
	//
	// The set of TxOptions are passed in order to allow the caller to
	// specify if a transaction should be read-only and optionally what
	// type of concurrency control should be used.
	ExecTx(ctx context.Context, txOptions TxOptions,
		txBody func(Q) error, reset func()) error
}

BatchedTx is a generic interface that represents the ability to execute several operations to a given storage interface in a single atomic transaction. Typically, Q here will be some subset of the main sqlc.Querier interface allowing it to only depend on the routines it needs to implement any additional business logic.

type DB added in v1.0.7

type DB interface {
	// GetBaseDB returns the underlying BaseDB instance.
	GetBaseDB() *BaseDB

	// ApplyAllMigrations applies all migrations to the database including
	// both sqlc and custom in-code migrations.
	ApplyAllMigrations(ctx context.Context,
		customMigrations []MigrationConfig) error
}

DB is an interface that represents a generic SQL database. It provides methods to apply migrations and access the underlying database connection.

type ErrSQLUniqueConstraintViolation

type ErrSQLUniqueConstraintViolation struct {
	DBError error
}

ErrSQLUniqueConstraintViolation is an error type which represents a database agnostic SQL unique constraint violation.

func (ErrSQLUniqueConstraintViolation) Error

type ErrSerializationError

type ErrSerializationError struct {
	DBError error
}

ErrSerializationError is an error type which represents a database agnostic error that a transaction couldn't be serialized with other concurrent db transactions.

func (ErrSerializationError) Error

func (e ErrSerializationError) Error() string

Error returns the error message.

func (ErrSerializationError) Unwrap

func (e ErrSerializationError) Unwrap() error

Unwrap returns the wrapped error.

type MakeTx added in v1.0.1

type MakeTx func() (Tx, error)

MakeTx is a function that creates a new transaction. It returns a Tx and an error if the transaction cannot be created. This is used to abstract the creation of a transaction from the actual transaction logic in order to be able to reuse the transaction retry logic in other packages.

type MigrationConfig added in v1.0.7

type MigrationConfig struct {
	// Name is the name of the migration.
	Name string

	// Version represents the "global" database version for this migration.
	// Unlike the schema version tracked by golang-migrate, it encompasses
	// all migrations, including those managed by golang-migrate as well
	// as custom in-code migrations.
	Version int

	// SchemaVersion represents the schema version tracked by golang-migrate
	// at which the migration is applied.
	SchemaVersion int

	// MigrationFn is the function executed for custom migrations at the
	// specified version. It is used to handle migrations that cannot be
	// performed through SQL alone. If set to nil, no custom migration is
	// applied.
	MigrationFn func(tx *sqlc.Queries) error
}

MigrationConfig is a configuration struct that describes SQL migrations. Each migration is associated with a specific schema version and a global database version. Migrations are applied in the order of their global database version. If a migration includes a non-nil MigrationFn, it is executed after the SQL schema has been migrated to the corresponding schema version.

func GetMigrations added in v1.0.7

func GetMigrations() []MigrationConfig

GetMigrations returns a copy of the migration configuration.

type MigrationExecutor added in v1.0.7

type MigrationExecutor interface {
	// ExecuteMigrations runs database migrations up to the specified target
	// version or all migrations if no target is specified. A migration may
	// include a schema change, a custom migration function, or both.
	// Developers must ensure that migrations are defined in the correct
	// order. Migration details are stored in the global variable
	// migrationConfig.
	ExecuteMigrations(target MigrationTarget) error
}

MigrationExecutor is an interface that abstracts the migration functionality.

type MigrationTarget added in v1.0.3

type MigrationTarget func(mig *migrate.Migrate) error

MigrationTarget is a functional option that can be passed to applyMigrations to specify a target version to migrate to.

type MigrationTxOptions added in v1.0.7

type MigrationTxOptions struct {
}

MigrationTxOptions is the implementation of the TxOptions interface for migration transactions.

func (*MigrationTxOptions) ReadOnly added in v1.0.7

func (m *MigrationTxOptions) ReadOnly() bool

ReadOnly returns false to indicate that migration transactions are not read only.

type OnBackoff added in v1.0.1

type OnBackoff func(retry int, delay time.Duration)

OnBackoff is a function that is called when a transaction is retried due to a serialization error. The function is called with the retry attempt number and the delay before the next retry.

type PostgresConfig

type PostgresConfig struct {
	Dsn            string        `long:"dsn" description:"Database connection string."`
	Timeout        time.Duration `long:"timeout" description:"Database connection timeout. Set to zero to disable."`
	MaxConnections int           `long:"maxconnections" description:"The maximum number of open connections to the database. Set to zero for unlimited."`
	SkipMigrations bool          `long:"skipmigrations" description:"Skip applying migrations on startup."`
}

PostgresConfig holds the postgres database configuration.

func (*PostgresConfig) Validate

func (p *PostgresConfig) Validate() error

type PostgresStore

type PostgresStore struct {
	*BaseDB
	// contains filtered or unexported fields
}

PostgresStore is a database store implementation that uses a Postgres backend.

func NewPostgresStore

func NewPostgresStore(cfg *PostgresConfig) (*PostgresStore, error)

NewPostgresStore creates a new store that is backed by a Postgres database backend.

func NewTestPostgresDB

func NewTestPostgresDB(t *testing.T, fixture *TestPgFixture) *PostgresStore

NewTestPostgresDB is a helper function that creates a Postgres database for testing using the given fixture.

func NewTestPostgresDBWithVersion added in v1.0.3

func NewTestPostgresDBWithVersion(t *testing.T, fixture *TestPgFixture,
	version uint) *PostgresStore

NewTestPostgresDBWithVersion is a helper function that creates a Postgres database for testing and migrates it to the given version.

func (*PostgresStore) ApplyAllMigrations added in v1.0.7

func (s *PostgresStore) ApplyAllMigrations(ctx context.Context,
	migrations []MigrationConfig) error

ApplyAllMigrations applies both the SQLC and custom in-code migrations to the Postgres database.

func (*PostgresStore) ExecuteMigrations added in v1.0.3

func (s *PostgresStore) ExecuteMigrations(target MigrationTarget) error

ExecuteMigrations runs migrations for the Postgres database, depending on the target given, either all migrations or up to a given version.

func (*PostgresStore) GetBaseDB added in v1.0.7

func (s *PostgresStore) GetBaseDB() *BaseDB

GetBaseDB returns the underlying BaseDB instance for the Postgres store. It is a trivial helper method to comply with the sqldb.DB interface.

type QueryCreator

type QueryCreator[Q any] func(*sql.Tx) Q

QueryCreator is a generic function that's used to create a Querier, which is a type of interface that implements storage related methods from a database transaction. This will be used to instantiate an object callers can use to apply multiple modifications to an object interface in a single atomic transaction.

type RollbackTx added in v1.0.1

type RollbackTx func(tx Tx) error

RollbackTx is a function that is called when a transaction needs to be rolled back due to a serialization error. By using this intermediate function, we can avoid having to return rollback errors that are not actionable by the caller.

type SqliteConfig

type SqliteConfig struct {
	Timeout        time.Duration `long:"timeout" description:"The time after which a database query should be timed out."`
	BusyTimeout    time.Duration `` /* 126-byte string literal not displayed */
	MaxConnections int           `long:"maxconnections" description:"The maximum number of open connections to the database. Set to zero for unlimited."`
	PragmaOptions  []string      `` /* 219-byte string literal not displayed */
	SkipMigrations bool          `long:"skipmigrations" description:"Skip applying migrations on startup."`
}

SqliteConfig holds all the config arguments needed to interact with our sqlite DB.

type SqliteStore

type SqliteStore struct {
	*BaseDB
	// contains filtered or unexported fields
}

SqliteStore is a database store implementation that uses a sqlite backend.

func NewSqliteStore

func NewSqliteStore(cfg *SqliteConfig, dbPath string) (*SqliteStore, error)

NewSqliteStore attempts to open a new sqlite database based on the passed config.

func NewTestSqliteDB

func NewTestSqliteDB(t *testing.T) *SqliteStore

NewTestSqliteDB is a helper function that creates an SQLite database for testing.

func NewTestSqliteDBWithVersion added in v1.0.3

func NewTestSqliteDBWithVersion(t *testing.T, version uint) *SqliteStore

NewTestSqliteDBWithVersion is a helper function that creates an SQLite database for testing and migrates it to the given version.

func (*SqliteStore) ApplyAllMigrations added in v1.0.7

func (s *SqliteStore) ApplyAllMigrations(ctx context.Context,
	migrations []MigrationConfig) error

ApplyAllMigrations applies both the SQLC and custom in-code migrations to the SQLite database.

func (*SqliteStore) ExecuteMigrations added in v1.0.3

func (s *SqliteStore) ExecuteMigrations(target MigrationTarget) error

ExecuteMigrations runs migrations for the sqlite database, depending on the target given, either all migrations or up to a given version.

func (*SqliteStore) GetBaseDB added in v1.0.7

func (s *SqliteStore) GetBaseDB() *BaseDB

GetBaseDB returns the underlying BaseDB instance for the SQLite store. It is a trivial helper method to comply with the sqldb.DB interface.

type TestPgFixture

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

TestPgFixture is a test fixture that starts a Postgres 11 instance in a docker container.

func NewTestPgFixture

func NewTestPgFixture(t *testing.T, expiry time.Duration) *TestPgFixture

NewTestPgFixture constructs a new TestPgFixture starting up a docker container running Postgres 11. The started container will expire in after the passed duration.

func (*TestPgFixture) GetConfig

func (f *TestPgFixture) GetConfig(dbName string) *PostgresConfig

GetConfig returns the full config of the Postgres node.

func (*TestPgFixture) TearDown

func (f *TestPgFixture) TearDown(t *testing.T)

TearDown stops the underlying docker container.

type TransactionExecutor

type TransactionExecutor[Query any] struct {
	BatchedQuerier
	// contains filtered or unexported fields
}

TransactionExecutor is a generic struct that abstracts away from the type of query a type needs to run under a database transaction, and also the set of options for that transaction. The QueryCreator is used to create a query given a database transaction created by the BatchedQuerier.

func NewTransactionExecutor

func NewTransactionExecutor[Querier any](db BatchedQuerier,
	createQuery QueryCreator[Querier],
	opts ...TxExecutorOption) *TransactionExecutor[Querier]

NewTransactionExecutor creates a new instance of a TransactionExecutor given a Querier query object and a concrete type for the type of transactions the Querier understands.

func (*TransactionExecutor[Q]) ExecTx

func (t *TransactionExecutor[Q]) ExecTx(ctx context.Context,
	txOptions TxOptions, txBody func(Q) error, reset func()) error

ExecTx is a wrapper for txBody to abstract the creation and commit of a db transaction. The db transaction is embedded in a `*Queries` that txBody needs to use when executing each one of the queries that need to be applied atomically. This can be used by other storage interfaces to parameterize the type of query and options run, in order to have access to batched operations related to a storage object.

type Tx

type Tx interface {
	// Commit commits the database transaction, an error should be returned
	// if the commit isn't possible.
	Commit() error

	// Rollback rolls back an incomplete database transaction.
	// Transactions that were able to be committed can still call this as a
	// noop.
	Rollback() error
}

Tx represents a database transaction that can be committed or rolled back.

type TxBody added in v1.0.1

type TxBody func(tx Tx) error

TxBody represents the function type for transactions. It returns an error to indicate success or failure.

type TxExecutorOption

type TxExecutorOption func(*txExecutorOptions)

TxExecutorOption is a functional option that allows us to pass in optional argument when creating the executor.

func WithTxRetries

func WithTxRetries(numRetries int) TxExecutorOption

WithTxRetries is a functional option that allows us to specify the number of times a transaction should be retried if it fails with a repeatable error.

func WithTxRetryDelay

func WithTxRetryDelay(delay time.Duration) TxExecutorOption

WithTxRetryDelay is a functional option that allows us to specify the delay to wait before a transaction is retried.

type TxOptions

type TxOptions interface {
	// ReadOnly returns true if the transaction should be read only.
	ReadOnly() bool
}

TxOptions represents a set of options one can use to control what type of database transaction is created. Transaction can be either read or write.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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