database

package
v0.2.4 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterMigration

func RegisterMigration(m Migration)

func RegisteredIDs

func RegisteredIDs() []string

func WithTx

func WithTx(ctx context.Context, conn *Connection, fn func(pgx.Tx) error) (err error)

WithTx runs fn inside a database transaction obtained from the connection pool. The transaction is committed when fn returns nil; otherwise it is rolled back. A panic inside fn is propagated after a best-effort rollback so the caller's defer chain stays intact.

Repositories use WithTx to compose multiple writes into a single atomic unit:

err := database.WithTx(ctx, conn, func(tx pgx.Tx) error {
    if _, err := tx.Exec(ctx, "INSERT ..."); err != nil { return err }
    return tx.QueryRow(ctx, "...").Scan(&id)
})

Types

type Connection

type Connection struct {
	Pool *pgxpool.Pool
}

func NewConnection

func NewConnection(ctx context.Context, cfg *config.DatabaseConfig) (*Connection, error)

func NewConnectionProvider

func NewConnectionProvider(ctx context.Context, cfg *config.DatabaseConfig) (*Connection, error)

func (*Connection) Close

func (c *Connection) Close()

func (*Connection) HealthCheck

func (c *Connection) HealthCheck(ctx context.Context) error

type Migration

type Migration struct {
	ID   string
	Name string
	Up   func(ctx context.Context, tx pgx.Tx) error
	Down func(ctx context.Context, tx pgx.Tx) error
}

Migration is a versioned schema change. ID must start with a Unix timestamp prefix.

type MigrationsManager

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

MigrationsManager applies registered migrations against a pgx pool.

func NewMigrationsManager

func NewMigrationsManager(pool *pgxpool.Pool) *MigrationsManager

func NewMigrationsManagerProvider

func NewMigrationsManagerProvider(conn *Connection) *MigrationsManager

func (*MigrationsManager) ApplyPending

func (m *MigrationsManager) ApplyPending(ctx context.Context) error

Directories

Path Synopsis
Package migrations registers in-code schema migrations via init().
Package migrations registers in-code schema migrations via init().

Jump to

Keyboard shortcuts

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