migrations.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 NewAddIndexMigration(table Table, index *Index) *AddIndexMigration {
  63. return &AddIndexMigration{tableName: table.Name, index: index}
  64. }
  65. func (m *AddIndexMigration) Table(tableName string) *AddIndexMigration {
  66. m.tableName = tableName
  67. return m
  68. }
  69. func (m *AddIndexMigration) Sql(dialect Dialect) string {
  70. return dialect.CreateIndexSql(m.tableName, m.index)
  71. }
  72. type DropIndexMigration struct {
  73. MigrationBase
  74. tableName string
  75. index *Index
  76. }
  77. func NewDropIndexMigration(table Table, index *Index) *DropIndexMigration {
  78. return &DropIndexMigration{tableName: table.Name, index: index}
  79. }
  80. func (m *DropIndexMigration) Sql(dialect Dialect) string {
  81. if m.index.Name == "" {
  82. m.index.Name = fmt.Sprintf("%s", strings.Join(m.index.Cols, "_"))
  83. }
  84. return dialect.DropIndexSql(m.tableName, m.index)
  85. }
  86. type AddTableMigration struct {
  87. MigrationBase
  88. table Table
  89. }
  90. func NewAddTableMigration(table Table) *AddTableMigration {
  91. for _, col := range table.Columns {
  92. if col.IsPrimaryKey {
  93. table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
  94. }
  95. }
  96. return &AddTableMigration{table: table}
  97. }
  98. func (m *AddTableMigration) Sql(d Dialect) string {
  99. return d.CreateTableSql(&m.table)
  100. }
  101. type DropTableMigration struct {
  102. MigrationBase
  103. tableName string
  104. }
  105. func NewDropTableMigration(tableName string) *DropTableMigration {
  106. return &DropTableMigration{tableName: tableName}
  107. }
  108. func (m *DropTableMigration) Sql(d Dialect) string {
  109. return d.DropTable(m.tableName)
  110. }
  111. type RenameTableMigration struct {
  112. MigrationBase
  113. oldName string
  114. newName string
  115. }
  116. func NewRenameTableMigration(oldName string, newName string) *RenameTableMigration {
  117. return &RenameTableMigration{oldName: oldName, newName: newName}
  118. }
  119. func (m *RenameTableMigration) IfTableExists(tableName string) *RenameTableMigration {
  120. m.Condition = &IfTableExistsCondition{TableName: tableName}
  121. return m
  122. }
  123. func (m *RenameTableMigration) Rename(oldName string, newName string) *RenameTableMigration {
  124. m.oldName = oldName
  125. m.newName = newName
  126. return m
  127. }
  128. func (m *RenameTableMigration) Sql(d Dialect) string {
  129. return d.RenameTable(m.oldName, m.newName)
  130. }
  131. type CopyTableDataMigration struct {
  132. MigrationBase
  133. sourceTable string
  134. targetTable string
  135. sourceCols []string
  136. targetCols []string
  137. colMap map[string]string
  138. }
  139. func NewCopyTableDataMigration(targetTable string, sourceTable string, colMap map[string]string) *CopyTableDataMigration {
  140. m := &CopyTableDataMigration{sourceTable: sourceTable, targetTable: targetTable}
  141. for key, value := range colMap {
  142. m.targetCols = append(m.targetCols, key)
  143. m.sourceCols = append(m.sourceCols, value)
  144. }
  145. return m
  146. }
  147. func (m *CopyTableDataMigration) IfTableExists(tableName string) *CopyTableDataMigration {
  148. m.Condition = &IfTableExistsCondition{TableName: tableName}
  149. return m
  150. }
  151. func (m *CopyTableDataMigration) Sql(d Dialect) string {
  152. return d.CopyTableData(m.sourceTable, m.targetTable, m.sourceCols, m.targetCols)
  153. }