migrate

package
v0.0.0-...-cf2fa2e Latest Latest
Warning

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

Go to latest
Published: Jan 11, 2024 License: AGPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package migrate provides a dead simple Go package for performing sql migrations using database/sql.

Index

Constants

View Source
const DefaultTable = "schema_migrations"

DefaultTable The default table to store what migrations have been run.

Variables

This section is empty.

Functions

func Exec

func Exec(db *sql.DB, dir MigrationDirection, migrations ...Migration) error

Exec is a convenience method that runs the migrations against the default table.

func NewPostgresLocker

func NewPostgresLocker(db *sql.DB) sync.Locker

NewPostgresLocker returns a new sync.Locker that obtains locks with pg_advisory_lock.

func Queries

func Queries(queries []string) func(*sql.Tx) error

Queries returns a func(tx *sql.Tx) error function that performs the given sql queries in multiple Exec calls.

Types

type ByID

type ByID []Migration

ByID implements the sort.Interface for sorting migrations by ID.

func (ByID) Len

func (m ByID) Len() int

func (ByID) Less

func (m ByID) Less(i, j int) bool

func (ByID) Swap

func (m ByID) Swap(i, j int)

type Migration

type Migration struct {
	// ID is a unique, numeric, identifier for this migration.
	ID int

	// Up is a function that gets called when this migration should go up.
	Up func(tx *sql.Tx) error

	// Down is a function that gets called when this migration should go
	// down.
	Down func(tx *sql.Tx) error
}

Migration represents a sql migration that can be migrated up or down.

type MigrationDirection

type MigrationDirection int
const (
	Up MigrationDirection = iota
	Down
)

type MigrationError

type MigrationError struct {
	Migration

	// The underlying error.
	Err error
}

MigrationError is an error that gets returned when an individual migration fails.

func (*MigrationError) Error

func (e *MigrationError) Error() string

Error implements the error interface.

type Migrator

type Migrator struct {
	// Table is the table to store what migrations have been run. The zero
	// value is DefaultTable.
	Table string

	// Locker is a sync.Locker to use to ensure that only 1 process is
	// running migrations.
	sync.Locker

	// The TransactionMode to use. The zero value is IndividualTransactions,
	// which runs each migration in it's own transaction.
	TransactionMode TransactionMode
	// contains filtered or unexported fields
}

Migrator performs migrations.

func NewMigrator

func NewMigrator(db *sql.DB) *Migrator

NewMigrator returns a new Migrator instance that will use the sql.DB to perform the migrations.

func NewPostgresMigrator

func NewPostgresMigrator(db *sql.DB) *Migrator

NewPostgresMigrator returns a new Migrator instance that uses the underlying sql.DB connection to a postgres database to perform migrations. It will use Postgres advisory locks to ensure that only 1 migration is run at a time.

func (*Migrator) Exec

func (m *Migrator) Exec(dir MigrationDirection, migrations ...Migration) error

Exec runs the migrations in the given direction.

type TransactionMode

type TransactionMode int
const (
	// IndividualTransactions In this mode, each migration is run in it's own isolated transaction.
	// If a migration fails, only that migration will be rolled back.
	IndividualTransactions TransactionMode = iota

	// SingleTransaction In this mode, all migrations are run inside a single transaction. If
	// one migration fails, all migrations are rolled back.
	SingleTransaction
)

Jump to

Keyboard shortcuts

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