migration

package
v1.9.2 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2017 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package migration enables you to generate migrations back and forth. It generates both migrations.

//Creates a table m.CreateTable("tablename","InnoDB","utf8");

//Alter a table m.AlterTable("tablename")

Standard Column Methods * SetDataType * SetNullable * SetDefault * SetUnsigned (use only on integer types unless produces error)

//Sets a primary column, multiple calls allowed, standard column methods available m.PriCol("id").SetAuto(true).SetNullable(false).SetDataType("INT(10)").SetUnsigned(true)

//UniCol Can be used multiple times, allows standard Column methods. Use same "index" string to add to same index m.UniCol("index","column")

//Standard Column Initialisation, can call .Remove() after NewCol("") on alter to remove m.NewCol("name").SetDataType("VARCHAR(255) COLLATE utf8_unicode_ci").SetNullable(false) m.NewCol("value").SetDataType("DOUBLE(8,2)").SetNullable(false)

//Rename Columns , only use with Alter table, doesn't works with Create, prefix standard column methods with "Old" to //create a true reversible migration eg: SetOldDataType("DOUBLE(12,3)") m.RenameColumn("from","to")...

//Foreign Columns, single columns are only supported, SetOnDelete & SetOnUpdate are available, call appropriately. //Supports standard column methods, automatic reverse. m.ForeignCol("local_col","foreign_col","foreign_table")

Package migration is used for migration

The table structure is as follow:

CREATE TABLE `migrations` (
	`id_migration` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'surrogate key',
	`name` varchar(255) DEFAULT NULL COMMENT 'migration name, unique',
	`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'date migrated or rolled back',
	`statements` longtext COMMENT 'SQL statements for this migration',
	`rollback_statements` longtext,
	`status` enum('update','rollback') DEFAULT NULL COMMENT 'update indicates it is a normal migration while rollback means this migration is rolled back',
	PRIMARY KEY (`id_migration`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Index

Constants

View Source
const (
	DateFormat   = "20060102_150405"
	DBDateFormat = "2006-01-02 15:04:05"
)

const the data format for the bee generate migration datatype

Variables

This section is empty.

Functions

func Refresh

func Refresh() error

Refresh first Reset, then Upgrade

func Register

func Register(name string, m Migrationer) error

Register register the Migration in the map

func Reset

func Reset() error

Reset reset all migration run all migration's down function

func Rollback

func Rollback(name string) error

Rollback rollback the migration by the name

func Upgrade

func Upgrade(lasttime int64) error

Upgrade upgrade the migration from lasttime

Types

type Column added in v1.4.2

type Column struct {
	Name     string
	Inc      string
	Null     string
	Default  string
	Unsign   string
	DataType string

	Modify bool
	// contains filtered or unexported fields
}

Column struct defines a single column of a table

func (*Column) Remove added in v1.9.0

func (c *Column) Remove()

Remove marks the columns to be removed. it allows reverse m to create the column.

func (*Column) SetAuto added in v1.9.0

func (c *Column) SetAuto(inc bool) *Column

SetAuto enables auto_increment of column (can be used once)

func (*Column) SetDataType added in v1.9.0

func (c *Column) SetDataType(dataType string) *Column

SetDataType sets the dataType of the column

func (*Column) SetDefault added in v1.9.0

func (c *Column) SetDefault(def string) *Column

SetDefault sets the default value, prepend with "DEFAULT "

func (*Column) SetNullable added in v1.9.0

func (c *Column) SetNullable(null bool) *Column

SetNullable sets the column to be null

func (*Column) SetPrimary added in v1.9.0

func (c *Column) SetPrimary(m *Migration) *Column

SetPrimary adds the columns to the primary key (can only be used any number of times in only one m)

func (*Column) SetUnsigned added in v1.9.0

func (c *Column) SetUnsigned(unsign bool) *Column

SetUnsigned sets the column to be unsigned int

type Foreign added in v1.9.0

type Foreign struct {
	ForeignTable  string
	ForeignColumn string
	OnDelete      string
	OnUpdate      string
	Column
}

Foreign struct defines a single foreign relationship

func (*Foreign) SetOnDelete added in v1.9.0

func (foreign *Foreign) SetOnDelete(del string) *Foreign

SetOnDelete sets the on delete of foreign

func (*Foreign) SetOnUpdate added in v1.9.0

func (foreign *Foreign) SetOnUpdate(update string) *Foreign

SetOnUpdate sets the on update of foreign

type Index added in v1.9.0

type Index struct {
	Name string
}

Index struct defines the structure of Index Columns

type Migration

type Migration struct {
	Created        string
	TableName      string
	Engine         string
	Charset        string
	ModifyType     string
	Columns        []*Column
	Indexes        []*Index
	Primary        []*Column
	Uniques        []*Unique
	Foreigns       []*Foreign
	Renames        []*RenameColumn
	RemoveColumns  []*Column
	RemoveIndexes  []*Index
	RemoveUniques  []*Unique
	RemoveForeigns []*Foreign
	// contains filtered or unexported fields
}

Migration defines the migrations by either SQL or DDL

func (*Migration) AddColumns added in v1.9.0

func (m *Migration) AddColumns(columns ...*Column) *Migration

AddColumns adds columns to m struct

func (*Migration) AddForeign added in v1.9.0

func (m *Migration) AddForeign(foreign *Foreign) *Migration

AddForeign adds the column to foreign in m struct

func (*Migration) AddIndex added in v1.9.0

func (m *Migration) AddIndex(index *Index) *Migration

AddIndex adds the column to index in m struct

func (*Migration) AddPrimary added in v1.9.0

func (m *Migration) AddPrimary(primary *Column) *Migration

AddPrimary adds the column to primary in m struct

func (*Migration) AddUnique added in v1.9.0

func (m *Migration) AddUnique(unique *Unique) *Migration

AddUnique adds the column to unique in m struct

func (*Migration) AlterTable added in v1.9.0

func (m *Migration) AlterTable(tablename string)

AlterTable set the ModifyType to alter

func (*Migration) CreateTable added in v1.9.0

func (m *Migration) CreateTable(tablename, engine, charset string, p ...func())

CreateTable creates the table on system

func (*Migration) Down

func (m *Migration) Down()

Down implement in the Inheritance struct for down

func (*Migration) Exec

func (m *Migration) Exec(name, status string) error

Exec execute the sql already add in the sql

func (*Migration) ForeignCol added in v1.9.0

func (m *Migration) ForeignCol(colname, foreigncol, foreigntable string) (foreign *Foreign)

ForeignCol creates a new foreign column and returns the instance of column

func (*Migration) GetCreated

func (m *Migration) GetCreated() int64

GetCreated get the unixtime from the Created

func (*Migration) GetSQL added in v1.9.0

func (m *Migration) GetSQL() (sql string)

GetSQL returns the generated sql depending on ModifyType

func (*Migration) Migrate added in v1.9.0

func (m *Migration) Migrate(migrationType string)

Migrate adds the SQL to the execution list

func (*Migration) NewCol added in v1.9.0

func (m *Migration) NewCol(name string) *Column

NewCol creates a new standard column and attaches it to m struct

func (*Migration) PriCol added in v1.9.0

func (m *Migration) PriCol(name string) *Column

PriCol creates a new primary column and attaches it to m struct

func (*Migration) RenameColumn added in v1.9.0

func (m *Migration) RenameColumn(from, to string) *RenameColumn

RenameColumn allows renaming of columns

func (*Migration) Reset

func (m *Migration) Reset()

Reset the sqls

func (*Migration) SQL added in v1.6.0

func (m *Migration) SQL(sql string)

SQL add sql want to execute

func (*Migration) UniCol added in v1.9.0

func (m *Migration) UniCol(uni, name string) *Column

UniCol creates / appends columns to specified unique key and attaches it to m struct

func (*Migration) Up

func (m *Migration) Up()

Up implement in the Inheritance struct for upgrade

type Migrationer

type Migrationer interface {
	Up()
	Down()
	Reset()
	Exec(name, status string) error
	GetCreated() int64
}

Migrationer is an interface for all Migration struct

type RenameColumn added in v1.9.0

type RenameColumn struct {
	OldName     string
	OldNull     string
	OldDefault  string
	OldUnsign   string
	OldDataType string
	NewName     string
	Column
}

RenameColumn struct allows renaming of columns

func (*RenameColumn) SetOldDataType added in v1.9.0

func (c *RenameColumn) SetOldDataType(dataType string) *RenameColumn

SetOldDataType allows reverting to previous datatype on reverse ms

func (*RenameColumn) SetOldDefault added in v1.9.0

func (c *RenameColumn) SetOldDefault(def string) *RenameColumn

SetOldDefault allows reverting to previous default on reverse ms

func (*RenameColumn) SetOldNullable added in v1.9.0

func (c *RenameColumn) SetOldNullable(null bool) *RenameColumn

SetOldNullable allows reverting to previous nullable on reverse ms

func (*RenameColumn) SetOldUnsigned added in v1.9.0

func (c *RenameColumn) SetOldUnsigned(unsign bool) *RenameColumn

SetOldUnsigned allows reverting to previous unsgined on reverse ms

type Unique added in v1.9.0

type Unique struct {
	Definition string
	Columns    []*Column
}

Unique struct defines a single unique key combination

func (*Unique) AddColumnsToUnique added in v1.9.0

func (unique *Unique) AddColumnsToUnique(columns ...*Column) *Unique

AddColumnsToUnique adds the columns to Unique Struct

Jump to

Keyboard shortcuts

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