migrations.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. return &AddIndexMigration{tableName: table.Name, index: index}
  88. }
  89. func (m *AddIndexMigration) Table(tableName string) *AddIndexMigration {
  90. m.tableName = tableName
  91. return m
  92. }
  93. func (m *AddIndexMigration) Sql(dialect Dialect) string {
  94. return dialect.CreateIndexSql(m.tableName, m.index)
  95. }
  96. type DropIndexMigration struct {
  97. MigrationBase
  98. tableName string
  99. index *Index
  100. }
  101. func NewDropIndexMigration(table Table, index *Index) *DropIndexMigration {
  102. return &DropIndexMigration{tableName: table.Name, index: index}
  103. }
  104. func (m *DropIndexMigration) Sql(dialect Dialect) string {
  105. if m.index.Name == "" {
  106. m.index.Name = strings.Join(m.index.Cols, "_")
  107. }
  108. return dialect.DropIndexSql(m.tableName, m.index)
  109. }
  110. type AddTableMigration struct {
  111. MigrationBase
  112. table Table
  113. }
  114. func NewAddTableMigration(table Table) *AddTableMigration {
  115. for _, col := range table.Columns {
  116. if col.IsPrimaryKey {
  117. table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
  118. }
  119. }
  120. return &AddTableMigration{table: table}
  121. }
  122. func (m *AddTableMigration) Sql(d Dialect) string {
  123. return d.CreateTableSql(&m.table)
  124. }
  125. type DropTableMigration struct {
  126. MigrationBase
  127. tableName string
  128. }
  129. func NewDropTableMigration(tableName string) *DropTableMigration {
  130. return &DropTableMigration{tableName: tableName}
  131. }
  132. func (m *DropTableMigration) Sql(d Dialect) string {
  133. return d.DropTable(m.tableName)
  134. }
  135. type RenameTableMigration struct {
  136. MigrationBase
  137. oldName string
  138. newName string
  139. }
  140. func NewRenameTableMigration(oldName string, newName string) *RenameTableMigration {
  141. return &RenameTableMigration{oldName: oldName, newName: newName}
  142. }
  143. func (m *RenameTableMigration) IfTableExists(tableName string) *RenameTableMigration {
  144. m.Condition = &IfTableExistsCondition{TableName: tableName}
  145. return m
  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) IfTableExists(tableName string) *CopyTableDataMigration {
  172. m.Condition = &IfTableExistsCondition{TableName: tableName}
  173. return m
  174. }
  175. func (m *CopyTableDataMigration) Sql(d Dialect) string {
  176. return d.CopyTableData(m.sourceTable, m.targetTable, m.sourceCols, m.targetCols)
  177. }
  178. type TableCharsetMigration struct {
  179. MigrationBase
  180. tableName string
  181. columns []*Column
  182. }
  183. func NewTableCharsetMigration(tableName string, columns []*Column) *TableCharsetMigration {
  184. return &TableCharsetMigration{tableName: tableName, columns: columns}
  185. }
  186. func (m *TableCharsetMigration) Sql(d Dialect) string {
  187. return d.UpdateTableSql(m.tableName, m.columns)
  188. }