1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 22:11:29 +00:00

Update: fixing some methods and adding documentation

Signed-off-by: Gnanakeethan Balasubramaniam <gnanakeethan@gmail.com>
This commit is contained in:
Gnanakeethan Balasubramaniam 2017-07-13 21:03:30 +05:30
parent e888fee4e0
commit 7c2ec075a4
No known key found for this signature in database
GPG Key ID: 24924006ABA72F31
3 changed files with 76 additions and 1 deletions

View File

@ -63,6 +63,19 @@ type RenameColumn struct {
Column Column
} }
// CreateTable creates the table on system
func (m *Migration) CreateTable(tablename, engine, charset string, p ...func()) {
m.TableName = tablename
m.Engine = engine
m.Charset = charset
m.ModifyType = "create"
}
func (m *Migration) AlterTable(tablename string) {
m.TableName = tablename
m.ModifyType = "alter"
}
// NewCol creates a new standard column and attaches it to m struct // NewCol creates a new standard column and attaches it to m struct
func (m *Migration) NewCol(name string) *Column { func (m *Migration) NewCol(name string) *Column {
col := &Column{Name: name} col := &Column{Name: name}

48
migration/doc.go Normal file
View File

@ -0,0 +1,48 @@
package migration
/* 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")
*/

View File

@ -52,6 +52,7 @@ type Migrationer interface {
GetCreated() int64 GetCreated() int64
} }
//Migration defines the migrations by either SQL or DDL
type Migration struct { type Migration struct {
sqls []string sqls []string
Created string Created string
@ -82,11 +83,24 @@ func init() {
// Up implement in the Inheritance struct for upgrade // Up implement in the Inheritance struct for upgrade
func (m *Migration) Up() { func (m *Migration) Up() {
switch m.ModifyType {
case "reverse":
m.ModifyType = "alter"
case "delete":
m.ModifyType = "create"
}
m.sqls = append(m.sqls, m.GetSQL())
} }
// Down implement in the Inheritance struct for down // Down implement in the Inheritance struct for down
func (m *Migration) Down() { func (m *Migration) Down() {
switch m.ModifyType {
case "alter":
m.ModifyType = "reverse"
case "create":
m.ModifyType = "delete"
}
m.sqls = append(m.sqls, m.GetSQL())
} }
//Migrate adds the SQL to the execution list //Migrate adds the SQL to the execution list