hawk

package module
v0.0.0-...-7f207da Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2024 License: BSD-2-Clause Imports: 8 Imported by: 0

README

Hawk

Hawk is a lightweight migration library for Go.

Documentation

Overview

Package hawk provides a lightweight migration library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialect

type Dialect interface {
	// CreateMigrationsTable sets up the database for use by Hawk.
	//
	// Dialects are free to determine how the migration state is
	// represented in the database, so long as the Dialect interface
	// can be satisfied.
	CreateMigrationsTable(db *sql.DB) error

	// GetVersion gets the number of the currently applied migration
	// from the database.
	//
	// If there are no currently applied migrations, this should return
	// -1 as the migration number and a nil error.
	GetVersion(db *sql.DB) (int64, error)

	// SetVersion sets the number of the currently applied migration in
	// the database.
	SetVersion(tx *sql.Tx, version int64) error
}

A Dialect extracts SQL-specific implementation details, so that Hawk can support multiple SQL implementations.

func Sqlite3

func Sqlite3() Dialect

Sqlite3 provides a Dialect for SQLite3.

type Engine

type Engine struct {
	DB      *sql.DB
	Dialect Dialect
	// contains filtered or unexported fields
}

func NewEngine

func NewEngine(db *sql.DB, dialect Dialect) *Engine

func (*Engine) AddMigration

func (e *Engine) AddMigration(num int64, name string, migration Migration)

AddMigration adds a migration to the engine.

Migrations are run in their numerical order (smallest to largest), with the name purely being used in the MigrationReference.

func (*Engine) Down

func (e *Engine) Down(version int64) (Result, error)

Down un-applies the migration of the given migration and any applied migrations since then.

func (*Engine) GetVersion

func (e *Engine) GetVersion() (int64, error)

GetVersion gets the number of the currently applied migration from the database.

If there are no currently applied migrations, this will return -1 as the migration number and a nil error.

func (*Engine) ReadMigrationsFromDirectory

func (e *Engine) ReadMigrationsFromDirectory(path string) error

ReadMigrationsFromDirectory reads migrations from a directory that uses golang-migrate's format.

func (*Engine) Rollback

func (e *Engine) Rollback() (Result, error)

Rollback reverts the last applied migration.

func (*Engine) Up

func (e *Engine) Up() (Result, error)

Up applies all un-applied migrations to the database.

type Migration

type Migration interface {
	// Up applies to the database.
	Up(tx *sql.Tx) error

	// Down un-applies to the database.
	Down(tx *sql.Tx) error
}

A Migration is an SQL transaction that can be both applied (up) and un-applied (down) to a database.

func MigrationFunc

func MigrationFunc(up func(tx *sql.Tx) error, down func(tx *sql.Tx) error) Migration

MigrationFunc is an adapter to allow the use of ordinary functions as a Migration.

type MigrationReference

type MigrationReference struct {
	Num  int64
	Name string
}

MigrationReference refers to a registered migration.

type Result

type Result struct {
	Version MigrationReference
	Skipped []MigrationReference
	Applied []MigrationReference
}

Result keeps a record of what migrations have been applied or skipped when running migrations.

Jump to

Keyboard shortcuts

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