database

package
v0.0.0-...-7f00d58 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2021 License: GPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const BootstrapSchema = `` /* 241-byte string literal not displayed */

BootstrapSchema is the schema of the migrations table.

Changing this is a major change, since "normal" updates won't work after it, so special upgrade steps will be needed.

Variables

This section is empty.

Functions

func MaybeCommit

func MaybeCommit(conn Connection) error

func MaybeRollback

func MaybeRollback(conn Connection) error

func MigrateSchema

func MigrateSchema(logger logger.Logger, conn Connection, providers ...MigrationsProvider) error

func MigrationVersion

func MigrationVersion(conn Connection, name string) (int, error)

func SaveMigrationVersion

func SaveMigrationVersion(conn Connection, name string, version int) error

Types

type Check

type Check func(logger logger.Logger, conn Connection) error

type Checks

type Checks []Check

type ConfigurableConnection

type ConfigurableConnection interface {
	Connection

	SetConnMaxLifetime(d time.Duration)
	SetMaxIdleConns(n int)
	SetMaxOpenConns(n int)
}

func Connect

func Connect(dbUrl string) (ConfigurableConnection, error)

Connect creates a database connection to a PostgreSQL database.

type Connection

type Connection interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
}

Connection represents a database connection.

func MaybeBegin

func MaybeBegin(conn Connection) (Connection, error)

func NewLoggerDB

func NewLoggerDB(logger logger.Logger, db Connection) Connection

NewLoggerDB wraps a database connection with a logger.

func TestConnect

func TestConnect(dbUrl string) (Connection, func())

TestConnect creates a connection to a PostgreSQL database for testing.

This will set up a temporary schema, and delete it after the test is run.

The correct usage of this function:

conn, cleanup := database.TestConnect(os.Getenv("DATABASE_URL"))
t.Cleanup(cleanup)

type Constraint

type Constraint struct {
	Name string
	Type ConstraintType
}

func LoadConstraints

func LoadConstraints(conn Connection, relname, prefix string) ([]Constraint, error)

type ConstraintType

type ConstraintType string
const (
	ConstraintTypePrimary   ConstraintType = "p"
	ConstraintTypeUnique    ConstraintType = "u"
	ConstraintTypeCheck     ConstraintType = "c"
	ConstraintTypeForeign   ConstraintType = "f"
	ConstraintTypeExclusion ConstraintType = "x"

	ConstraintTypeUniqueIndex ConstraintType = "iu"
)

func (ConstraintType) DropDefinition

func (c ConstraintType) DropDefinition(name string) string

type Migration

type Migration func(logger logger.Logger, conn Connection) error

type MigrationCheckProvider

type MigrationCheckProvider interface {
	MigrationsProvider
	Checks() Checks
}

func DecorateMigrationsWithChecks

func DecorateMigrationsWithChecks(mp MigrationsProvider, checks ...Check) MigrationCheckProvider

type Migrations

type Migrations []Migration

func (Migrations) UpgradeFrom

func (g Migrations) UpgradeFrom(last int, logger logger.Logger, conn Connection) (int, error)

type MigrationsProvider

type MigrationsProvider interface {
	Name() string
	Migrations() Migrations
}

func DefineMigrations

func DefineMigrations(name string, gens ...Migration) MigrationsProvider

type Transaction

type Transaction interface {
	Connection
	Commit() error
	Rollback() error
}

Transaction represents a database connection with an active transaction.

type TransactionFactory

type TransactionFactory interface {
	Begin() (Transaction, error)
}

TransactionFactory can initiate a transaction.

Jump to

Keyboard shortcuts

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