pkgsql

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: Apache-2.0 Imports: 19 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidMigrationFormat = errors.New("not a valid MigrationFormat")

Functions

func EndTx

func EndTx(tx Tx, err error) error

EndTx ends the Tx and rollbacks it if an err is not nil, otherwise the function commits the transaction

Types

type AtomicStore

type AtomicStore[T any] struct {
	// contains filtered or unexported fields
}

func NewAtomicStore

func NewAtomicStore[T any](db Database, newStoreFunc NewStoreFunc[T]) *AtomicStore[T]

NewAtomicStore returns new AtomicStore[T].

func (*AtomicStore[T]) Exec

func (s *AtomicStore[T]) Exec(ctx context.Context, op pkgstore.AtomicOperation[T]) (err error)

Exec runs an atomic operation.

type Beginner

type Beginner interface {
	BeginTx(ctx context.Context, txOptions *sql.TxOptions) (*sql.Tx, error)
}

Beginner begins a transaction.

type Committer

type Committer interface {
	Commit() error
}

Committer commits a transaction.

type Database

type Database interface {
	Beginner
	TableOperator
}

Database represents a database connection.

type Execer

type Execer interface {
	ExecContext(ctx context.Context, sql string, arguments ...any) (sql.Result, error)
}

Execer executes a query without returning sql rows.

type MigrationFormat

type MigrationFormat int8

MigrationFormat is a format used to create the migration files.

  • MigrationFileStyleGoMigrate uses the format: {version}_{title}.up.{extension} {version}_{title}.down.{extension}

  • MigrationFileStyleFlyway uses the format: {V}{version}__{migration_name}.sql – for Up migrations. {U}{version}__{migration_name}.sql – for down migrations.

ENUM( flyway gomigrate )

const (
	// MigrationFormatFlyway is a MigrationFormat of type Flyway.
	MigrationFormatFlyway MigrationFormat = iota
	// MigrationFormatGomigrate is a MigrationFormat of type Gomigrate.
	MigrationFormatGomigrate
)

func ParseMigrationFormat

func ParseMigrationFormat(name string) (MigrationFormat, error)

ParseMigrationFormat attempts to convert a string to a MigrationFormat.

func (MigrationFormat) IsValid

func (x MigrationFormat) IsValid() bool

IsValid provides a quick way to determine if the typed value is part of the allowed enumerated values

func (MigrationFormat) MarshalText

func (x MigrationFormat) MarshalText() ([]byte, error)

MarshalText implements the text marshaller method.

func (*MigrationFormat) Scan

func (x *MigrationFormat) Scan(value interface{}) (err error)

Scan implements the Scanner interface.

func (MigrationFormat) String

func (x MigrationFormat) String() string

String implements the Stringer interface.

func (*MigrationFormat) UnmarshalText

func (x *MigrationFormat) UnmarshalText(text []byte) error

UnmarshalText implements the text unmarshaller method.

func (MigrationFormat) Value

func (x MigrationFormat) Value() (driver.Value, error)

Value implements the driver Valuer interface.

type Migrator

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

Migrator is Postgres database schem migrator.

func NewMigrator

func NewMigrator(cfg MigratorConfig) (*Migrator, error)

NewMigrator returns a new Migrator.

func (*Migrator) MigrateDown

func (m *Migrator) MigrateDown() error

MigrateDown applies down migrations.

func (*Migrator) MigrateUp

func (m *Migrator) MigrateUp() error

MigrateUp applies up migrations.

type MigratorConfig

type MigratorConfig struct {
	// MigrationsDir is a path to the migrations directory. If MigrationsFs is nil, the Migrator will use it directly.
	// If MigrationsFs is not nil, the path in MigrationsDir is used to strip the prefix.
	//
	// Example:
	//
	//     //go:embed migrations/*
	//     var MigrationsFS embed.FS
	//
	// Here, 'migrations' is a prefix that should be stripped from the path.
	// This allows the iofs.Driver to read its content correctly.
	MigrationsDir string
	MigrationsFs  fs.FS
	DSN           string
	Format        MigrationFormat
}

type NewStoreFunc

type NewStoreFunc[T any] func(db Database, to TableOperator) T

type Querier

type Querier interface {
	QueryContext(ctx context.Context, sql string, args ...any) (*sql.Rows, error)
}

Querier runs a single SQL query.

type Rollbacker

type Rollbacker interface {
	Rollback() error
}

Rollbacker rollbacks a transaction.

type TableOperator

type TableOperator interface {
	Execer
	Querier
}

TableOperator can run Exec and Query operations on database.

type Transactor

type Transactor interface {
	Committer
	Rollbacker
}

Transactor commits or rollbacks a transaction.

type Tx

type Tx interface {
	Transactor
	TableOperator
}

Tx is an interface for SQL transaction.

Jump to

Keyboard shortcuts

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