Update: fixing some methods and adding documentation

Signed-off-by: 's avatarGnanakeethan Balasubramaniam <gnanakeethan@gmail.com>
parent e888fee4
...@@ -63,6 +63,19 @@ type RenameColumn struct { ...@@ -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}
......
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")
*/
...@@ -52,6 +52,7 @@ type Migrationer interface { ...@@ -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() { ...@@ -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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment