gomigration

package module
v0.0.0-...-40d2547 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2015 License: MIT Imports: 4 Imported by: 0

README

GoDoc

gomigration

Package gomigration provides some basic functionality to run migrations.

How to Install

go get github.com/mier85/gomigration

Example for a batch migration

package main

import (
	"database/sql"
	_ "github.com/go-sql-driver/mysql"
	"github.com/gocraft/dbr"
	"github.com/mier85/gomigration"
)

func main() {
	migrations := make([]Migration, 0)
	migrations = append(migrations, Migration{
		Name: "initial",
		Up: func(transaction *dbr.Tx) (rE error) {
			_, rE = transaction.Exec(`CREATE TABLE ` + "`word`" + `(
 id INT NOT NULL AUTO_INCREMENT,
 name VARCHAR(255),
 PRIMARY KEY (id)
 );`)
			return
		},
		Down: func(transaction *dbr.Tx) (rE error) {
			_, rE = transaction.Exec("DROP TABLE `word`")
			return
		},
	})
	db, err := sql.Open("mysql", "user:password!@tcp(host:port)/dbname")
	if nil != err {
		panic(err)
	}
	connection := dbr.NewConnection(db, nil)
	mM := NewMigrationManager(connection)
	mM.MigrationRunner(migrations)
}

Documentation

Overview

Package gomigration provides some basic functionality to run migrations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Migrate

type Migrate func(*dbr.Tx) error

type Migration

type Migration struct {
	Name     string
	Up, Down Migrate
}

type MigrationManager

type MigrationManager struct {
	Connection *dbr.Connection
	// contains filtered or unexported fields
}

func NewMigrationManager

func NewMigrationManager(c *dbr.Connection) MigrationManager

NewMigrationManager returns a default MigrationManager and initializes it.

func NewMigrationManagerExplicitTableName

func NewMigrationManagerExplicitTableName(c *dbr.Connection, tableName string) MigrationManager

NewMigrationManagerExplicitTableName returns a new MigrationManager with a named migration-meta-data table and initializes it.

func (MigrationManager) CheckIfExecuted

func (mM MigrationManager) CheckIfExecuted(session *dbr.Session, migration Migration) bool

CheckIfExecuted checks if an migration ran before and returns true if yes and otherwise false.

func (MigrationManager) CheckIfSane

func (mM MigrationManager) CheckIfSane(migrations []Migration) error

CheckIfSane checks if the list of migrations has any name twice and stops on first error or returns nil.

func (MigrationManager) Init

func (mM MigrationManager) Init()

Init initializes the necessary DbTable for the migrations and panics if not successful.

func (MigrationManager) MarkAsExecuted

func (mM MigrationManager) MarkAsExecuted(transaction *dbr.Tx, migration Migration) (rErr error)

MarkAsExecuted marks that a single Migration was applied.

func (MigrationManager) MarkAsNotExecuted

func (mM MigrationManager) MarkAsNotExecuted(transaction *dbr.Tx, migration Migration) (rErr error)

MarkAsNotExecuted deletes the entry of an migration that was previously applied.

func (MigrationManager) MigrationRunner

func (mM MigrationManager) MigrationRunner(migrations []Migration)

MigrationRunner applies all migrations that have not yet been executed.

func (MigrationManager) RunSingleMigrationDown

func (mM MigrationManager) RunSingleMigrationDown(session *dbr.Session, migration Migration) error

RunSingleMigrationDown undos a migration if it was already applied, otherwise throws an error.

func (MigrationManager) RunSingleMigrationUp

func (mM MigrationManager) RunSingleMigrationUp(session *dbr.Session, migration Migration) error

RunSingleMigrationUp applies a single migration if it was not yet executed.

Jump to

Keyboard shortcuts

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