migrations.go 4.7 KB

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