migrations.go 4.8 KB

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