migrations.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package migrator
  2. import (
  3. "strings"
  4. )
  5. type MigrationBase struct {
  6. id string
  7. Condition MigrationCondition
  8. }
  9. func (m *MigrationBase) Id() string {
  10. return m.id
  11. }
  12. func (m *MigrationBase) SetId(id string) {
  13. m.id = id
  14. }
  15. func (m *MigrationBase) GetCondition() MigrationCondition {
  16. return m.Condition
  17. }
  18. type RawSqlMigration struct {
  19. MigrationBase
  20. sql map[string]string
  21. }
  22. func NewRawSqlMigration(sql string) *RawSqlMigration {
  23. m := &RawSqlMigration{}
  24. if sql != "" {
  25. m.Default(sql)
  26. }
  27. return m
  28. }
  29. func (m *RawSqlMigration) Sql(dialect Dialect) string {
  30. if m.sql != nil {
  31. if val := m.sql[dialect.DriverName()]; val != "" {
  32. return val
  33. }
  34. if val := m.sql["default"]; val != "" {
  35. return val
  36. }
  37. }
  38. return dialect.NoOpSql()
  39. }
  40. func (m *RawSqlMigration) Set(dialect string, sql string) *RawSqlMigration {
  41. if m.sql == nil {
  42. m.sql = make(map[string]string)
  43. }
  44. m.sql[dialect] = sql
  45. return m
  46. }
  47. func (m *RawSqlMigration) Default(sql string) *RawSqlMigration {
  48. return m.Set("default", sql)
  49. }
  50. func (m *RawSqlMigration) Sqlite(sql string) *RawSqlMigration {
  51. return m.Set(SQLITE, sql)
  52. }
  53. func (m *RawSqlMigration) Mysql(sql string) *RawSqlMigration {
  54. return m.Set(MYSQL, sql)
  55. }
  56. func (m *RawSqlMigration) Postgres(sql string) *RawSqlMigration {
  57. return m.Set(POSTGRES, sql)
  58. }
  59. func (m *RawSqlMigration) Mssql(sql string) *RawSqlMigration {
  60. return m.Set(MSSQL, sql)
  61. }
  62. type AddColumnMigration struct {
  63. MigrationBase
  64. tableName string
  65. column *Column
  66. }
  67. func NewAddColumnMigration(table Table, col *Column) *AddColumnMigration {
  68. return &AddColumnMigration{tableName: table.Name, column: col}
  69. }
  70. func (m *AddColumnMigration) Table(tableName string) *AddColumnMigration {
  71. m.tableName = tableName
  72. return m
  73. }
  74. func (m *AddColumnMigration) Column(col *Column) *AddColumnMigration {
  75. m.column = col
  76. return m
  77. }
  78. func (m *AddColumnMigration) Sql(dialect Dialect) string {
  79. return dialect.AddColumnSql(m.tableName, m.column)
  80. }
  81. type AddIndexMigration struct {
  82. MigrationBase
  83. tableName string
  84. index *Index
  85. }
  86. func NewAddIndexMigration(table Table, index *Index) *AddIndexMigration {
  87. m := &AddIndexMigration{tableName: table.Name, index: index}
  88. m.Condition = &IfIndexNotExistsCondition{TableName: table.Name, IndexName: index.XName(table.Name)}
  89. return m
  90. }
  91. func (m *AddIndexMigration) Table(tableName string) *AddIndexMigration {
  92. m.tableName = tableName
  93. return m
  94. }
  95. func (m *AddIndexMigration) Sql(dialect Dialect) string {
  96. return dialect.CreateIndexSql(m.tableName, m.index)
  97. }
  98. type DropIndexMigration struct {
  99. MigrationBase
  100. tableName string
  101. index *Index
  102. }
  103. func NewDropIndexMigration(table Table, index *Index) *DropIndexMigration {
  104. m := &DropIndexMigration{tableName: table.Name, index: index}
  105. m.Condition = &IfIndexExistsCondition{TableName: table.Name, IndexName: index.XName(table.Name)}
  106. return m
  107. }
  108. func (m *DropIndexMigration) Sql(dialect Dialect) string {
  109. if m.index.Name == "" {
  110. m.index.Name = strings.Join(m.index.Cols, "_")
  111. }
  112. return dialect.DropIndexSql(m.tableName, m.index)
  113. }
  114. type AddTableMigration struct {
  115. MigrationBase
  116. table Table
  117. }
  118. func NewAddTableMigration(table Table) *AddTableMigration {
  119. for _, col := range table.Columns {
  120. if col.IsPrimaryKey {
  121. table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
  122. }
  123. }
  124. return &AddTableMigration{table: table}
  125. }
  126. func (m *AddTableMigration) Sql(d Dialect) string {
  127. return d.CreateTableSql(&m.table)
  128. }
  129. type DropTableMigration struct {
  130. MigrationBase
  131. tableName string
  132. }
  133. func NewDropTableMigration(tableName string) *DropTableMigration {
  134. return &DropTableMigration{tableName: tableName}
  135. }
  136. func (m *DropTableMigration) Sql(d Dialect) string {
  137. return d.DropTable(m.tableName)
  138. }
  139. type RenameTableMigration struct {
  140. MigrationBase
  141. oldName string
  142. newName string
  143. }
  144. func NewRenameTableMigration(oldName string, newName string) *RenameTableMigration {
  145. return &RenameTableMigration{oldName: oldName, newName: newName}
  146. }
  147. func (m *RenameTableMigration) Rename(oldName string, newName string) *RenameTableMigration {
  148. m.oldName = oldName
  149. m.newName = newName
  150. return m
  151. }
  152. func (m *RenameTableMigration) Sql(d Dialect) string {
  153. return d.RenameTable(m.oldName, m.newName)
  154. }
  155. type CopyTableDataMigration struct {
  156. MigrationBase
  157. sourceTable string
  158. targetTable string
  159. sourceCols []string
  160. targetCols []string
  161. //colMap map[string]string
  162. }
  163. func NewCopyTableDataMigration(targetTable string, sourceTable string, colMap map[string]string) *CopyTableDataMigration {
  164. m := &CopyTableDataMigration{sourceTable: sourceTable, targetTable: targetTable}
  165. for key, value := range colMap {
  166. m.targetCols = append(m.targetCols, key)
  167. m.sourceCols = append(m.sourceCols, value)
  168. }
  169. return m
  170. }
  171. func (m *CopyTableDataMigration) Sql(d Dialect) string {
  172. return d.CopyTableData(m.sourceTable, m.targetTable, m.sourceCols, m.targetCols)
  173. }
  174. type TableCharsetMigration struct {
  175. MigrationBase
  176. tableName string
  177. columns []*Column
  178. }
  179. func NewTableCharsetMigration(tableName string, columns []*Column) *TableCharsetMigration {
  180. return &TableCharsetMigration{tableName: tableName, columns: columns}
  181. }
  182. func (m *TableCharsetMigration) Sql(d Dialect) string {
  183. return d.UpdateTableSql(m.tableName, m.columns)
  184. }