migration

package
v0.0.0-...-ebdf1d9 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2018 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package migration provides an interface for migrating a database backwards and forwards.

You must store the path to the env.json file in the environment variable: JAYCONFIG

Examples:

jay migrate:mysql make "test" # Create new migration
jay migrate:mysql all         # Advance all migrations
jay migrate:mysql reset       # Rollback all migrations
jay migrate:mysql refresh     # Rollback all migrations then advance all migrations
jay migrate:mysql status      # See last 'up' migration
jay migrate:mysql up          # Apply only the next 'up' migration
jay migrate:mysql down        # Apply only the current 'down' migration

jay migrate make "Create user table"
  Creates two new files in the database/migration folder using this format:
    * YYYYMMDD_HHMMSS.nnnnnn_create_user_table.up.sql
    * YYYYMMDD_HHMMSS.nnnnnn_create_user_table.down.sql

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNone is when there are no migrations in the database
	ErrNone = errors.New("No migrations yet.")
	// ErrCurrent is when the database is up-to-date
	ErrCurrent = errors.New("Database current. No changes made.")
	// ErrMissing is when the migration file cannot be found
	ErrMissing = errors.New("Migration not found.")
	// ErrTableNotCreated is when the migration cannot be created
	ErrTableNotCreated = errors.New("Could not create the migration table.")
)

Functions

This section is empty.

Types

type Info

type Info struct {
	// Db is the database information
	Db Interface
	//DateFormat is the date and time format for the migration files
	DateFormat string
	// Folder is the migrations folder
	Folder string
	// List if the life of Up migrations
	List []string

	// Folder is the migrations table
	Table string
	// TemplateUp is the stub used for Up migration files when they are created
	TemplateUp string
	// TemplateDown is the stub used for Down migration files when they are created
	TemplateDown string
	// contains filtered or unexported fields
}

Info holds the information for the migration.

func New

func New(db Interface, table string, folder string) (*Info, error)

New returns an instance of a migration after creating the migration table (if one doesn't exist), retrieving a list of the available migrations, and reading the last migration. You must connect to the database prior to calling this function.

func (*Info) Create

func (info *Info) Create(description string) error

Create writes two new migration files to the folder with timestamps and descriptions.

func (*Info) DownAll

func (info *Info) DownAll() error

DownAll removes all migrations.

func (*Info) DownOne

func (info *Info) DownOne() error

DownOne removes only the last migration.

func (*Info) Output

func (info *Info) Output() string

Output returns the text output of the performed operations.

func (*Info) Position

func (info *Info) Position() int

Position returns the index of the migration. 0 is no migration.

func (*Info) Status

func (info *Info) Status() string

Status returns the last applied migration name without the file extension.

func (*Info) UpAll

func (info *Info) UpAll() error

UpAll applies all migrations that have not been applied.

func (*Info) UpOne

func (info *Info) UpOne() error

UpOne applies only the next migration.

type Interface

type Interface interface {
	// Extension should return an extension without a period or a blank string
	Extension() string
	// TableExist should return an error if the table does not exist
	TableExist() error
	// CreateTable should return an error if the table could not be created
	CreateTable() error
	// Status should return the name of the last migration or return an error
	// or a model.ErrNoResult error if there are no results
	Status() (string, error)
	// Migrate will run the migration and return an error if not successful
	Migrate(query string) error
	// RecordUp should record the name of the file in the database
	RecordUp(name string) error
	// RecordDown should record the name of the file in the database and make
	// any changes to the database like updating the AUTO_INCREMENT value
	RecordDown(name string) error
}

Interface defines all the functions required for a migration.

Directories

Path Synopsis
Package mysql implements MySQL migrations.
Package mysql implements MySQL migrations.

Jump to

Keyboard shortcuts

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