migrations.go 4.3 KB

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