migrations.go 5.2 KB

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