migrate

package module
v0.0.0-...-63ab506 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2018 License: MIT Imports: 3 Imported by: 0

README

mgo-migrate

GoDoc Build Status codecov Go Report Card

Migrate function of MongoDB driver for Go

How to use

package main

import (
	"github.com/appleboy/mgo-migrate"
	"gopkg.in/mgo.v2"
)

func main() {
	session, err := mgo.Dial("127.0.0.1")
	if err != nil {
		panic(err)
	}

	m := migrate.New(session, "test_db", migrate.DefaultOptions, []*migrate.Migration{{
		ID: "201709201400",
		Migrate: func(s *mgo.Session) error {
			return nil
		},
		Rollback: func(s *mgo.Session) error {
			return nil
		},
	}})

	if err := m.Migrate(); err != nil {
		panic(err)
	}

	if err := m.RollbackLast(); err != nil {
		panic(err)
	}
}

Documentation

Overview

Example
session, err := mgo.Dial("127.0.0.1")
if err != nil {
	panic(err)
}

m := migrate.New(session, "test_db", migrate.DefaultOptions, []*migrate.Migration{{
	ID: "201709201400",
	Migrate: func(s *mgo.Session) error {
		return nil
	},
	Rollback: func(s *mgo.Session) error {
		return nil
	},
}})

if err := m.Migrate(); err != nil {
	panic(err)
}

if err := m.RollbackLast(); err != nil {
	panic(err)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultOptions can be used if you don't want to think about options.
	DefaultOptions = &Options{
		TableName:    "migrations",
		IDColumnName: "id",
	}

	// ErrRollbackImpossible is returned when trying to rollback a migration
	// that has no rollback function.
	ErrRollbackImpossible = errors.New("It's impossible to rollback this migration")

	// ErrNoMigrationDefined is returned when no migration is defined.
	ErrNoMigrationDefined = errors.New("No migration defined")

	// ErrMissingID is returned when the ID od migration is equal to ""
	ErrMissingID = errors.New("Missing ID in migration")

	// ErrNoRunnedMigration is returned when any runned migration was found while
	// running RollbackLast
	ErrNoRunnedMigration = errors.New("Could not find last runned migration")
)

Functions

This section is empty.

Types

type InitSchemaFunc

type InitSchemaFunc func(*mgo.Session) error

InitSchemaFunc is the func signature for initializing the schema.

type Migrate

type Migrate struct {
	// contains filtered or unexported fields
}

Migrate represents a collection of all migrations of a database schema.

func New

func New(session *mgo.Session, database string, options *Options, migrations []*Migration) *Migrate

New returns a new Gormigrate.

func (*Migrate) InitSchema

func (m *Migrate) InitSchema(initSchema InitSchemaFunc)

InitSchema sets a function that is run if no migration is found. The idea is preventing to run all migrations when a new clean database is being migrating. In this function you should create all tables and foreign key necessary to your application.

func (*Migrate) Migrate

func (m *Migrate) Migrate() error

Migrate executes all migrations that did not run yet.

func (*Migrate) RollbackLast

func (m *Migrate) RollbackLast() error

RollbackLast undo the last migration

func (*Migrate) RollbackMigration

func (m *Migrate) RollbackMigration(mig *Migration) error

RollbackMigration undo a migration.

type MigrateFunc

type MigrateFunc func(*mgo.Session) error

MigrateFunc is the func signature for migrating.

type Migration

type Migration struct {
	// ID is the migration identifier. Usually a timestamp like "201601021504".
	ID string
	// Migrate is a function that will br executed while running this migration.
	Migrate MigrateFunc
	// Rollback will be executed on rollback. Can be nil.
	Rollback RollbackFunc
}

Migration represents a database migration (a modification to be made on the database).

type Options

type Options struct {
	// TableName is the migration table.
	TableName string
	// IDColumnName is the name of column where the migration id will be stored.
	IDColumnName string
}

Options define options for all migrations.

type RollbackFunc

type RollbackFunc func(*mgo.Session) error

RollbackFunc is the func signature for rollbacking.

Jump to

Keyboard shortcuts

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