beego: Index | Files

package migration

import ""

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`)


Package Files

ddl.go doc.go migration.go


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

const the data format for the bee generate migration datatype

func Refresh Uses

func Refresh() error

Refresh first Reset, then Upgrade

func Register Uses

func Register(name string, m Migrationer) error

Register register the Migration in the map

func Reset Uses

func Reset() error

Reset reset all migration run all migration's down function

func Rollback Uses

func Rollback(name string) error

Rollback rollback the migration by the name

func Upgrade Uses

func Upgrade(lasttime int64) error

Upgrade upgrade the migration from lasttime

type Column Uses

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 Uses

func (c *Column) Remove()

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

func (*Column) SetAuto Uses

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

SetAuto enables auto_increment of column (can be used once)

func (*Column) SetDataType Uses

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

SetDataType sets the dataType of the column

func (*Column) SetDefault Uses

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

SetDefault sets the default value, prepend with "DEFAULT "

func (*Column) SetNullable Uses

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

SetNullable sets the column to be null

func (*Column) SetPrimary Uses

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 Uses

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

SetUnsigned sets the column to be unsigned int

type Foreign Uses

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

Foreign struct defines a single foreign relationship

func (*Foreign) SetOnDelete Uses

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

SetOnDelete sets the on delete of foreign

func (*Foreign) SetOnUpdate Uses

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

SetOnUpdate sets the on update of foreign

type Index Uses

type Index struct {
    Name string

Index struct defines the structure of Index Columns

type Migration Uses

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 Uses

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

AddColumns adds columns to m struct

func (*Migration) AddForeign Uses

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

AddForeign adds the column to foreign in m struct

func (*Migration) AddIndex Uses

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

AddIndex adds the column to index in m struct

func (*Migration) AddPrimary Uses

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

AddPrimary adds the column to primary in m struct

func (*Migration) AddUnique Uses

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

AddUnique adds the column to unique in m struct

func (*Migration) AlterTable Uses

func (m *Migration) AlterTable(tablename string)

AlterTable set the ModifyType to alter

func (*Migration) CreateTable Uses

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

CreateTable creates the table on system

func (*Migration) Down Uses

func (m *Migration) Down()

Down implement in the Inheritance struct for down

func (*Migration) Exec Uses

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

Exec execute the sql already add in the sql

func (*Migration) ForeignCol Uses

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 Uses

func (m *Migration) GetCreated() int64

GetCreated get the unixtime from the Created

func (*Migration) GetSQL Uses

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

GetSQL returns the generated sql depending on ModifyType

func (*Migration) Migrate Uses

func (m *Migration) Migrate(migrationType string)

Migrate adds the SQL to the execution list

func (*Migration) NewCol Uses

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

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

func (*Migration) PriCol Uses

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

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

func (*Migration) RenameColumn Uses

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

RenameColumn allows renaming of columns

func (*Migration) Reset Uses

func (m *Migration) Reset()

Reset the sqls

func (*Migration) SQL Uses

func (m *Migration) SQL(sql string)

SQL add sql want to execute

func (*Migration) UniCol Uses

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 Uses

func (m *Migration) Up()

Up implement in the Inheritance struct for upgrade

type Migrationer Uses

type Migrationer interface {
    Exec(name, status string) error
    GetCreated() int64

Migrationer is an interface for all Migration struct

type RenameColumn Uses

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

RenameColumn struct allows renaming of columns

func (*RenameColumn) SetOldDataType Uses

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

SetOldDataType allows reverting to previous datatype on reverse ms

func (*RenameColumn) SetOldDefault Uses

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

SetOldDefault allows reverting to previous default on reverse ms

func (*RenameColumn) SetOldNullable Uses

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

SetOldNullable allows reverting to previous nullable on reverse ms

func (*RenameColumn) SetOldUnsigned Uses

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

SetOldUnsigned allows reverting to previous unsgined on reverse ms

type Unique Uses

type Unique struct {
    Definition string
    Columns    []*Column

Unique struct defines a single unique key combination

func (*Unique) AddColumnsToUnique Uses

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

AddColumnsToUnique adds the columns to Unique Struct

Package migration imports 7 packages (graph) and is imported by 1 packages. Updated 2020-11-07. Refresh now. Tools for package owners.