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
- func Refresh() error
- func Register(name string, m Migrationer) error
- func Reset() error
- func Rollback(name string) error
- func Upgrade(lasttime int64) error
- type Column
- func (c *Column) Remove()
- func (c *Column) SetAuto(inc bool) *Column
- func (c *Column) SetDataType(dataType string) *Column
- func (c *Column) SetDefault(def string) *Column
- func (c *Column) SetNullable(null bool) *Column
- func (c *Column) SetPrimary(m *Migration) *Column
- func (c *Column) SetUnsigned(unsign bool) *Column
- type Foreign
- type Index
- type Migration
- func (m *Migration) AddColumns(columns ...*Column) *Migration
- func (m *Migration) AddForeign(foreign *Foreign) *Migration
- func (m *Migration) AddIndex(index *Index) *Migration
- func (m *Migration) AddPrimary(primary *Column) *Migration
- func (m *Migration) AddUnique(unique *Unique) *Migration
- func (m *Migration) AlterTable(tablename string)
- func (m *Migration) CreateTable(tablename, engine, charset string, p ...func())
- func (m *Migration) Down()
- func (m *Migration) Exec(name, status string) error
- func (m *Migration) ForeignCol(colname, foreigncol, foreigntable string) (foreign *Foreign)
- func (m *Migration) GetCreated() int64
- func (m *Migration) GetSQL() (sql string)
- func (m *Migration) Migrate(migrationType string)
- func (m *Migration) NewCol(name string) *Column
- func (m *Migration) PriCol(name string) *Column
- func (m *Migration) RenameColumn(from, to string) *RenameColumn
- func (m *Migration) Reset()
- func (m *Migration) SQL(sql string)
- func (m *Migration) UniCol(uni, name string) *Column
- func (m *Migration) Up()
- type Migrationer
- type RenameColumn
- type Unique
Constants ¶
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 Register ¶
func Register(name string, m Migrationer) error
Register register the Migration in the map
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
SetAuto enables auto_increment of column (can be used once)
func (*Column) SetDataType ¶ added in v1.9.0
SetDataType sets the dataType of the column
func (*Column) SetDefault ¶ added in v1.9.0
SetDefault sets the default value, prepend with "DEFAULT "
func (*Column) SetNullable ¶ added in v1.9.0
SetNullable sets the column to be null
func (*Column) SetPrimary ¶ added in v1.9.0
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
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
SetOnDelete sets the on delete of foreign
func (*Foreign) SetOnUpdate ¶ added in v1.9.0
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
AddColumns adds columns to m struct
func (*Migration) AddForeign ¶ added in v1.9.0
AddForeign adds the column to foreign in m struct
func (*Migration) AddPrimary ¶ added in v1.9.0
AddPrimary adds the column to primary in m struct
func (*Migration) AlterTable ¶ added in v1.9.0
AlterTable set the ModifyType to alter
func (*Migration) CreateTable ¶ added in v1.9.0
CreateTable creates the table on system
func (*Migration) Down ¶
func (m *Migration) Down()
Down implement in the Inheritance struct for down
func (*Migration) ForeignCol ¶ added in v1.9.0
ForeignCol creates a new foreign column and returns the instance of column
func (*Migration) GetCreated ¶
GetCreated get the unixtime from the Created
func (*Migration) NewCol ¶ added in v1.9.0
NewCol creates a new standard column and attaches it to m struct
func (*Migration) PriCol ¶ added in v1.9.0
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
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
Unique struct defines a single unique key combination
func (*Unique) AddColumnsToUnique ¶ added in v1.9.0
AddColumnsToUnique adds the columns to Unique Struct