sql

package
v1.14.3 Latest Latest
Warning

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

Go to latest
Published: Aug 30, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Migrate

func Migrate(ctx context.Context, db DatabaseConn, opts MigrationOptions) (err error)

Migrate performs database migrations.

Types

type DatabaseConn

type DatabaseConn interface {
	Begin(context.Context) (databaseConnTx, error)
	QueryRow(context.Context, string, ...any) databaseConnRow
	Exec(context.Context, string, ...any) (int64, error)
	IsNoRowsError(err error) bool
}

DatabaseConn is the interface matched by all adapters.

func AdaptDatabaseSQLConn

func AdaptDatabaseSQLConn(db DatabaseSQLConn) DatabaseConn

AdaptDatabaseSQLConn returns a databaseConn based on a database/sql connection.

Note: when using transactions with database/sql, the context bassed to Begin impacts the entire transaction. Canceling the context automatically rolls back the transaction.

func AdaptPgxConn

func AdaptPgxConn(db PgxConn) DatabaseConn

AdaptPgxConn returns a databaseConn based on a pgx connection.

Note: when using transactions with pgx, the context bassed to Begin impacts the creation of the transaction only.

type DatabaseSQLAdapter

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

DatabaseSQLAdapter is an adapter for database/sql connections.

func (*DatabaseSQLAdapter) Begin

func (sqla *DatabaseSQLAdapter) Begin(ctx context.Context) (databaseConnTx, error)

func (*DatabaseSQLAdapter) Exec

func (sqla *DatabaseSQLAdapter) Exec(ctx context.Context, query string, args ...any) (int64, error)

func (*DatabaseSQLAdapter) IsNoRowsError

func (sqla *DatabaseSQLAdapter) IsNoRowsError(err error) bool

func (*DatabaseSQLAdapter) QueryRow

func (sqla *DatabaseSQLAdapter) QueryRow(ctx context.Context, query string, args ...any) databaseConnRow

type DatabaseSQLConn

type DatabaseSQLConn interface {
	BeginTx(context.Context, *sql.TxOptions) (*sql.Tx, error)
	QueryContext(context.Context, string, ...any) (*sql.Rows, error)
	QueryRowContext(context.Context, string, ...any) *sql.Row
	ExecContext(context.Context, string, ...any) (sql.Result, error)
}

DatabaseSQLConn is the interface for connections that use database/sql.

type GCOptions

type GCOptions struct {
	Logger logger.Logger

	// Query that must atomically update the "last cleanup time" in the metadata table, but only if the garbage collector hasn't run already.
	// The caller will check the nuber of affected rows. If zero, it assumes that the GC has ran too recently, and will not proceed to delete expired records.
	// The query receives one parameter that is the last cleanup interval, in milliseconds.
	// The function must return both the query and the argument.
	UpdateLastCleanupQuery func(arg any) (string, any)

	// Query that performs the cleanup of all expired rows.
	DeleteExpiredValuesQuery string

	// Interval to perfm the cleanup.
	CleanupInterval time.Duration

	// Database connection.
	// Must be adapted using AdaptDatabaseSQLConn or AdaptPgxConn.
	DB DatabaseConn
}

type GarbageCollector

type GarbageCollector interface {
	CleanupExpired() error
	io.Closer
}

func ScheduleGarbageCollector

func ScheduleGarbageCollector(opts GCOptions) (GarbageCollector, error)

type MigrationFn

type MigrationFn = func(ctx context.Context) error

type MigrationOptions

type MigrationOptions struct {
	// Logger
	Logger logger.Logger

	// List of migrations to execute.
	// Each item is a function that receives a context and the database connection, and can execute queries.
	Migrations []MigrationFn

	// EnsureMetadataTable ensures that the metadata table exists.
	EnsureMetadataTable func(ctx context.Context) error

	// GetVersionQuery is the query to execute to load the latest migration version.
	GetVersionQuery string

	// UpdateVersionQuery is a function that returns the query to update the migration version, and the arg.
	UpdateVersionQuery func(version string) (string, any)
}

MigrationOptions contains options for the Migrate function.

type MigrationTeardownFn

type MigrationTeardownFn = func() error

type PgxAdapter

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

PgxAdapter is an adapter for pgx connections.

func (*PgxAdapter) Begin

func (pga *PgxAdapter) Begin(ctx context.Context) (databaseConnTx, error)

func (*PgxAdapter) Exec

func (pga *PgxAdapter) Exec(ctx context.Context, query string, args ...any) (int64, error)

func (*PgxAdapter) IsNoRowsError

func (pga *PgxAdapter) IsNoRowsError(err error) bool

func (*PgxAdapter) QueryRow

func (pga *PgxAdapter) QueryRow(ctx context.Context, query string, args ...any) databaseConnRow

type PgxConn

type PgxConn interface {
	Begin(context.Context) (pgx.Tx, error)
	Query(context.Context, string, ...any) (pgx.Rows, error)
	QueryRow(context.Context, string, ...any) pgx.Row
	Exec(context.Context, string, ...any) (pgconn.CommandTag, error)
}

PgxConn is the interface for connections that use pgx.

Directories

Path Synopsis
migrations

Jump to

Keyboard shortcuts

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