mig

package module
v0.0.0-...-887cf7e Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2014 License: MIT Imports: 2 Imported by: 0

README

mig

GoDoc

Go SQL migration package

Usage

import "github.com/jagregory/mig"

mig.Define(`
	create table foo ( id integer );
`)

mig.DefineVersion(2, `
	create table bar ( id integer );
`)

mig.Migrate(db)

The Define and DefineVersion functions are used to define a migration and the SQL that will be executed for it. The Migrate function actually executes the migrations. An error will be returned from Migrate if anything bad happens.

How it works

Mig will create a db_version table in your database with a version int column. Each migration will be executed in the order they're defined in unless there's a corresponding row in the db_version table. After each successful migration a row will be inserted in db_version for that migration.

Documentation

Overview

Package mig is a simple SQL migration library. You use it to embed snippets of SQL into your application which will be run on startup (or whenever you tell them to). Mig is primarily used for migrating schemas between versions of applications.

Mig is small and deliberately simple. There are no rollbacks in Mig, and it's expected that the application running Mig will have schema altering permissions on the target database.

If you want to restrict your application's rights to a database, it's best you run Mig from a different process or under a different user.

Index

Constants

This section is empty.

Variables

View Source
var Quiet = false

Quiet will supress status output on STDOUT if true

Functions

func Define

func Define(script string)

Define will define and register a migration for running the next time Mig is run. A version is automatically chosen based on the sequence of previous calls to Define (aka version=len(migrations)+1).

mig.Define(`
  create table foo ( id integer );
`)

func DefineVersion

func DefineVersion(version int, script string)

DefineVersion will define and register a migration for running the next time Mig is run with a specific version number.

mig.DefineVersion(2, `
  alter table foo add column bar varchar(10);
`)

func Migrate

func Migrate(db *sql.DB) error

Migrate will execute the defined migrations against the sql.DB. For each migration Mig will look in a db_version table to see if it has already been run. If there's a row in db_version for a migration it wont be run again, otherwise the migration will be run and a row stored in db_version.

If this is the first time Mig has been run, Migrate will first create a db_version table and store a zero row.

Types

type MigrationError

type MigrationError struct {
	Cause   error
	Version int
}

MigrationError is returned when an error occurs running one of the migrations. If the version is 0, the initial db_version table creation failed.

func (MigrationError) Error

func (e MigrationError) Error() string

Jump to

Keyboard shortcuts

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