migrations.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. }
  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. }
  31. panic("db type not supported")
  32. }
  33. func (m *RawSqlMigration) Sqlite(sql string) *RawSqlMigration {
  34. m.sqlite = sql
  35. return m
  36. }
  37. func (m *RawSqlMigration) Mysql(sql string) *RawSqlMigration {
  38. m.mysql = sql
  39. return m
  40. }
  41. type AddColumnMigration struct {
  42. MigrationBase
  43. tableName string
  44. column *Column
  45. }
  46. func (m *AddColumnMigration) Table(tableName string) *AddColumnMigration {
  47. m.tableName = tableName
  48. return m
  49. }
  50. func (m *AddColumnMigration) Column(col *Column) *AddColumnMigration {
  51. m.column = col
  52. return m
  53. }
  54. func (m *AddColumnMigration) Sql(dialect Dialect) string {
  55. return dialect.AddColumnSql(m.tableName, m.column)
  56. }
  57. type AddIndexMigration struct {
  58. MigrationBase
  59. tableName string
  60. index Index
  61. }
  62. func (m *AddIndexMigration) Name(name string) *AddIndexMigration {
  63. m.index.Name = name
  64. return m
  65. }
  66. func (m *AddIndexMigration) Table(tableName string) *AddIndexMigration {
  67. m.tableName = tableName
  68. return m
  69. }
  70. func (m *AddIndexMigration) Unique() *AddIndexMigration {
  71. m.index.Type = UniqueIndex
  72. return m
  73. }
  74. func (m *AddIndexMigration) Columns(columns ...string) *AddIndexMigration {
  75. m.index.Cols = columns
  76. return m
  77. }
  78. func (m *AddIndexMigration) Sql(dialect Dialect) string {
  79. if m.index.Name == "" {
  80. m.index.Name = fmt.Sprintf("%s", strings.Join(m.index.Cols, "_"))
  81. }
  82. return dialect.CreateIndexSql(m.tableName, &m.index)
  83. }
  84. type DropIndexMigration struct {
  85. MigrationBase
  86. tableName string
  87. index Index
  88. }
  89. func (m *DropIndexMigration) Name(name string) *DropIndexMigration {
  90. m.index.Name = name
  91. return m
  92. }
  93. func (m *DropIndexMigration) Table(tableName string) *DropIndexMigration {
  94. m.tableName = tableName
  95. return m
  96. }
  97. func (m *DropIndexMigration) Unique() *DropIndexMigration {
  98. m.index.Type = UniqueIndex
  99. return m
  100. }
  101. func (m *DropIndexMigration) Columns(columns ...string) *DropIndexMigration {
  102. m.index.Cols = columns
  103. return m
  104. }
  105. func (m *DropIndexMigration) Sql(dialect Dialect) string {
  106. if m.index.Name == "" {
  107. m.index.Name = fmt.Sprintf("%s", strings.Join(m.index.Cols, "_"))
  108. }
  109. return dialect.DropIndexSql(m.tableName, &m.index)
  110. }
  111. type AddTableMigration struct {
  112. MigrationBase
  113. table Table
  114. }
  115. func (m *AddTableMigration) Sql(d Dialect) string {
  116. return d.CreateTableSql(&m.table)
  117. }
  118. func (m *AddTableMigration) Name(name string) *AddTableMigration {
  119. m.table.Name = name
  120. return m
  121. }
  122. func (m *AddTableMigration) WithColumns(columns ...*Column) *AddTableMigration {
  123. for _, col := range columns {
  124. m.table.Columns = append(m.table.Columns, col)
  125. if col.IsPrimaryKey {
  126. m.table.PrimaryKeys = append(m.table.PrimaryKeys, col.Name)
  127. }
  128. }
  129. return m
  130. }
  131. func (m *AddTableMigration) WithColumn(col *Column) *AddTableMigration {
  132. m.table.Columns = append(m.table.Columns, col)
  133. if col.IsPrimaryKey {
  134. m.table.PrimaryKeys = append(m.table.PrimaryKeys, col.Name)
  135. }
  136. return m
  137. }
  138. type DropTableMigration struct {
  139. MigrationBase
  140. tableName string
  141. }
  142. func (m *DropTableMigration) Table(tableName string) *DropTableMigration {
  143. m.tableName = tableName
  144. return m
  145. }
  146. func (m *DropTableMigration) Sql(d Dialect) string {
  147. return d.DropTable(m.tableName)
  148. }
  149. type RenameTableMigration struct {
  150. MigrationBase
  151. oldName string
  152. newName string
  153. }
  154. func (m *RenameTableMigration) IfTableExists(tableName string) *RenameTableMigration {
  155. m.Condition = &IfTableExistsCondition{TableName: tableName}
  156. return m
  157. }
  158. func (m *RenameTableMigration) Rename(oldName string, newName string) *RenameTableMigration {
  159. m.oldName = oldName
  160. m.newName = newName
  161. return m
  162. }
  163. func (m *RenameTableMigration) Sql(d Dialect) string {
  164. return d.RenameTable(m.oldName, m.newName)
  165. }
  166. type CopyTableDataMigration struct {
  167. MigrationBase
  168. sourceTable string
  169. targetTable string
  170. sourceCols string
  171. targetCols string
  172. }
  173. func (m *CopyTableDataMigration) Source(tableName string, cols string) *CopyTableDataMigration {
  174. m.sourceTable = tableName
  175. m.sourceCols = cols
  176. return m
  177. }
  178. func (m *CopyTableDataMigration) Target(tableName string, cols string) *CopyTableDataMigration {
  179. m.targetTable = tableName
  180. m.targetCols = cols
  181. return m
  182. }
  183. func (m *CopyTableDataMigration) IfTableExists(tableName string) *CopyTableDataMigration {
  184. m.Condition = &IfTableExistsCondition{TableName: tableName}
  185. return m
  186. }
  187. func (m *CopyTableDataMigration) Sql(d Dialect) string {
  188. return d.CopyTableData(m.sourceTable, m.targetTable, m.sourceCols, m.targetCols)
  189. }