From 7c2ec075a4fb45ca4dd25e7ae7585417d9c99b53 Mon Sep 17 00:00:00 2001 From: Gnanakeethan Balasubramaniam Date: Thu, 13 Jul 2017 21:03:30 +0530 Subject: [PATCH] Update: fixing some methods and adding documentation Signed-off-by: Gnanakeethan Balasubramaniam --- migration/ddl.go | 13 ++++++++++++ migration/doc.go | 48 ++++++++++++++++++++++++++++++++++++++++++ migration/migration.go | 16 +++++++++++++- 3 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 migration/doc.go diff --git a/migration/ddl.go b/migration/ddl.go index 3b405498..9d4bfc11 100644 --- a/migration/ddl.go +++ b/migration/ddl.go @@ -63,6 +63,19 @@ type RenameColumn struct { 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 func (m *Migration) NewCol(name string) *Column { col := &Column{Name: name} diff --git a/migration/doc.go b/migration/doc.go new file mode 100644 index 00000000..51050ed2 --- /dev/null +++ b/migration/doc.go @@ -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") + + + + + + + +*/ diff --git a/migration/migration.go b/migration/migration.go index d1387ba0..ef42c986 100644 --- a/migration/migration.go +++ b/migration/migration.go @@ -52,6 +52,7 @@ type Migrationer interface { GetCreated() int64 } +//Migration defines the migrations by either SQL or DDL type Migration struct { sqls []string Created string @@ -82,11 +83,24 @@ func init() { // Up implement in the Inheritance struct for upgrade 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 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