migrator.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package migrator
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "reflect"
  6. "strings"
  7. "github.com/jinzhu/gorm"
  8. "github.com/jinzhu/gorm/clause"
  9. "github.com/jinzhu/gorm/schema"
  10. )
  11. // Migrator m struct
  12. type Migrator struct {
  13. Config
  14. }
  15. // Config schema config
  16. type Config struct {
  17. DB *gorm.DB
  18. gorm.Dialector
  19. }
  20. func (m Migrator) RunWithValue(value interface{}, fc func(*gorm.Statement) error) error {
  21. stmt := m.DB.Statement
  22. if stmt == nil {
  23. stmt = &gorm.Statement{DB: m.DB}
  24. }
  25. if err := stmt.Parse(value); err != nil {
  26. return err
  27. }
  28. return fc(stmt)
  29. }
  30. func (m Migrator) DataTypeOf(field *schema.Field) string {
  31. if field.DBDataType != "" {
  32. return field.DBDataType
  33. }
  34. return m.Dialector.DataTypeOf(field)
  35. }
  36. // AutoMigrate
  37. func (m Migrator) AutoMigrate(values ...interface{}) error {
  38. // TODO smart migrate data type
  39. for _, value := range values {
  40. if !m.DB.Migrator().HasTable(value) {
  41. if err := m.DB.Migrator().CreateTable(value); err != nil {
  42. return err
  43. }
  44. } else {
  45. if err := m.RunWithValue(value, func(stmt *gorm.Statement) error {
  46. for _, field := range stmt.Schema.FieldsByDBName {
  47. if !m.DB.Migrator().HasColumn(value, field.DBName) {
  48. if err := m.DB.Migrator().AddColumn(value, field.DBName); err != nil {
  49. return err
  50. }
  51. }
  52. }
  53. for _, rel := range stmt.Schema.Relationships.Relations {
  54. if constraint := rel.ParseConstraint(); constraint != nil {
  55. if !m.DB.Migrator().HasConstraint(value, constraint.Name) {
  56. if err := m.DB.Migrator().CreateConstraint(value, constraint.Name); err != nil {
  57. return err
  58. }
  59. }
  60. }
  61. for _, chk := range stmt.Schema.ParseCheckConstraints() {
  62. if !m.DB.Migrator().HasConstraint(value, chk.Name) {
  63. if err := m.DB.Migrator().CreateConstraint(value, chk.Name); err != nil {
  64. return err
  65. }
  66. }
  67. }
  68. // create join table
  69. joinValue := reflect.New(rel.JoinTable.ModelType).Interface()
  70. if !m.DB.Migrator().HasTable(joinValue) {
  71. defer m.DB.Migrator().CreateTable(joinValue)
  72. }
  73. }
  74. return nil
  75. }); err != nil {
  76. return err
  77. }
  78. }
  79. }
  80. return nil
  81. }
  82. func (m Migrator) CreateTable(values ...interface{}) error {
  83. for _, value := range values {
  84. if err := m.RunWithValue(value, func(stmt *gorm.Statement) error {
  85. var (
  86. createTableSQL = "CREATE TABLE ? ("
  87. values = []interface{}{clause.Table{Name: stmt.Table}}
  88. hasPrimaryKeyInDataType bool
  89. )
  90. for _, dbName := range stmt.Schema.DBNames {
  91. field := stmt.Schema.FieldsByDBName[dbName]
  92. createTableSQL += fmt.Sprintf("? ?")
  93. hasPrimaryKeyInDataType = hasPrimaryKeyInDataType || strings.Contains(strings.ToUpper(field.DBDataType), "PRIMARY KEY")
  94. values = append(values, clause.Column{Name: dbName}, clause.Expr{SQL: m.DataTypeOf(field)})
  95. if field.AutoIncrement {
  96. createTableSQL += " AUTO_INCREMENT"
  97. }
  98. if field.NotNull {
  99. createTableSQL += " NOT NULL"
  100. }
  101. if field.Unique {
  102. createTableSQL += " UNIQUE"
  103. }
  104. if field.DefaultValue != "" {
  105. createTableSQL += " DEFAULT ?"
  106. values = append(values, clause.Expr{SQL: field.DefaultValue})
  107. }
  108. createTableSQL += ","
  109. }
  110. if !hasPrimaryKeyInDataType {
  111. createTableSQL += "PRIMARY KEY ?,"
  112. primaryKeys := []interface{}{}
  113. for _, field := range stmt.Schema.PrimaryFields {
  114. primaryKeys = append(primaryKeys, clause.Column{Name: field.DBName})
  115. }
  116. values = append(values, primaryKeys)
  117. }
  118. for _, idx := range stmt.Schema.ParseIndexes() {
  119. createTableSQL += "INDEX ? ?,"
  120. values = append(values, clause.Expr{SQL: idx.Name}, m.DB.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt))
  121. }
  122. for _, rel := range stmt.Schema.Relationships.Relations {
  123. if constraint := rel.ParseConstraint(); constraint != nil {
  124. sql, vars := buildConstraint(constraint)
  125. createTableSQL += sql + ","
  126. values = append(values, vars...)
  127. }
  128. // create join table
  129. joinValue := reflect.New(rel.JoinTable.ModelType).Interface()
  130. if !m.DB.Migrator().HasTable(joinValue) {
  131. defer m.DB.Migrator().CreateTable(joinValue)
  132. }
  133. }
  134. for _, chk := range stmt.Schema.ParseCheckConstraints() {
  135. createTableSQL += "CONSTRAINT ? CHECK ?,"
  136. values = append(values, clause.Column{Name: chk.Name}, clause.Expr{SQL: chk.Constraint})
  137. }
  138. createTableSQL = strings.TrimSuffix(createTableSQL, ",")
  139. createTableSQL += ")"
  140. return m.DB.Exec(createTableSQL, values...).Error
  141. }); err != nil {
  142. return err
  143. }
  144. }
  145. return nil
  146. }
  147. func (m Migrator) DropTable(values ...interface{}) error {
  148. for _, value := range values {
  149. if err := m.RunWithValue(value, func(stmt *gorm.Statement) error {
  150. return m.DB.Exec("DROP TABLE ?", clause.Table{Name: stmt.Table}).Error
  151. }); err != nil {
  152. return err
  153. }
  154. }
  155. return nil
  156. }
  157. func (m Migrator) HasTable(value interface{}) bool {
  158. var count int64
  159. m.RunWithValue(value, func(stmt *gorm.Statement) error {
  160. currentDatabase := m.DB.Migrator().CurrentDatabase()
  161. return m.DB.Raw("SELECT count(*) FROM information_schema.tables WHERE table_schema = ? AND table_name = ? AND table_type = ?", currentDatabase, stmt.Table, "BASE TABLE").Row().Scan(&count)
  162. })
  163. return count > 0
  164. }
  165. func (m Migrator) RenameTable(oldName, newName string) error {
  166. return m.DB.Exec("RENAME TABLE ? TO ?", oldName, newName).Error
  167. }
  168. func (m Migrator) AddColumn(value interface{}, field string) error {
  169. return m.RunWithValue(value, func(stmt *gorm.Statement) error {
  170. if field := stmt.Schema.LookUpField(field); field != nil {
  171. return m.DB.Exec(
  172. "ALTER TABLE ? ADD ? ?",
  173. clause.Table{Name: stmt.Table}, clause.Column{Name: field.DBName}, clause.Expr{SQL: m.DataTypeOf(field)},
  174. ).Error
  175. }
  176. return fmt.Errorf("failed to look up field with name: %s", field)
  177. })
  178. }
  179. func (m Migrator) DropColumn(value interface{}, field string) error {
  180. return m.RunWithValue(value, func(stmt *gorm.Statement) error {
  181. if field := stmt.Schema.LookUpField(field); field != nil {
  182. return m.DB.Exec(
  183. "ALTER TABLE ? DROP COLUMN ?", clause.Table{Name: stmt.Table}, clause.Column{Name: field.DBName},
  184. ).Error
  185. }
  186. return fmt.Errorf("failed to look up field with name: %s", field)
  187. })
  188. }
  189. func (m Migrator) AlterColumn(value interface{}, field string) error {
  190. return m.RunWithValue(value, func(stmt *gorm.Statement) error {
  191. if field := stmt.Schema.LookUpField(field); field != nil {
  192. return m.DB.Exec(
  193. "ALTER TABLE ? ALTER COLUMN ? TYPE ?",
  194. clause.Table{Name: stmt.Table}, clause.Column{Name: field.DBName}, clause.Expr{SQL: m.DataTypeOf(field)},
  195. ).Error
  196. }
  197. return fmt.Errorf("failed to look up field with name: %s", field)
  198. })
  199. }
  200. func (m Migrator) HasColumn(value interface{}, field string) bool {
  201. var count int64
  202. m.RunWithValue(value, func(stmt *gorm.Statement) error {
  203. currentDatabase := m.DB.Migrator().CurrentDatabase()
  204. name := field
  205. if field := stmt.Schema.LookUpField(field); field != nil {
  206. name = field.DBName
  207. }
  208. return m.DB.Raw(
  209. "SELECT count(*) FROM INFORMATION_SCHEMA.columns WHERE table_schema = ? AND table_name = ? AND column_name = ?",
  210. currentDatabase, stmt.Table, name,
  211. ).Row().Scan(&count)
  212. })
  213. return count > 0
  214. }
  215. func (m Migrator) RenameColumn(value interface{}, oldName, field string) error {
  216. return m.RunWithValue(value, func(stmt *gorm.Statement) error {
  217. if field := stmt.Schema.LookUpField(field); field != nil {
  218. oldName = m.DB.NamingStrategy.ColumnName(stmt.Table, oldName)
  219. return m.DB.Exec(
  220. "ALTER TABLE ? RENAME COLUMN ? TO ?",
  221. clause.Table{Name: stmt.Table}, clause.Column{Name: oldName}, clause.Column{Name: field.DBName},
  222. ).Error
  223. }
  224. return fmt.Errorf("failed to look up field with name: %s", field)
  225. })
  226. }
  227. func (m Migrator) ColumnTypes(value interface{}) ([]*sql.ColumnType, error) {
  228. return nil, gorm.ErrNotImplemented
  229. }
  230. func (m Migrator) CreateView(name string, option gorm.ViewOption) error {
  231. return gorm.ErrNotImplemented
  232. }
  233. func (m Migrator) DropView(name string) error {
  234. return gorm.ErrNotImplemented
  235. }
  236. func buildConstraint(constraint *schema.Constraint) (sql string, results []interface{}) {
  237. sql = "CONSTRAINT ? FOREIGN KEY ? REFERENCES ??"
  238. if constraint.OnDelete != "" {
  239. sql += " ON DELETE " + constraint.OnDelete
  240. }
  241. if constraint.OnUpdate != "" {
  242. sql += " ON UPDATE " + constraint.OnUpdate
  243. }
  244. var foreignKeys, references []interface{}
  245. for _, field := range constraint.ForeignKeys {
  246. foreignKeys = append(foreignKeys, clause.Column{Name: field.DBName})
  247. }
  248. for _, field := range constraint.References {
  249. references = append(references, clause.Column{Name: field.DBName})
  250. }
  251. results = append(results, constraint.Name, foreignKeys, clause.Table{Name: constraint.ReferenceSchema.Table}, references)
  252. return
  253. }
  254. func (m Migrator) CreateConstraint(value interface{}, name string) error {
  255. return m.RunWithValue(value, func(stmt *gorm.Statement) error {
  256. checkConstraints := stmt.Schema.ParseCheckConstraints()
  257. if chk, ok := checkConstraints[name]; ok {
  258. return m.DB.Exec(
  259. "ALTER TABLE ? ADD CONSTRAINT ? CHECK ?",
  260. clause.Table{Name: stmt.Table}, clause.Column{Name: chk.Name}, clause.Expr{SQL: chk.Constraint},
  261. ).Error
  262. }
  263. for _, rel := range stmt.Schema.Relationships.Relations {
  264. if constraint := rel.ParseConstraint(); constraint != nil && constraint.Name == name {
  265. sql, values := buildConstraint(constraint)
  266. return m.DB.Exec("ALTER TABLE ? ADD "+sql, append([]interface{}{clause.Table{Name: stmt.Table}}, values...)...).Error
  267. }
  268. }
  269. err := fmt.Errorf("failed to create constraint with name %v", name)
  270. if field := stmt.Schema.LookUpField(name); field != nil {
  271. for _, cc := range checkConstraints {
  272. if err = m.CreateIndex(value, cc.Name); err != nil {
  273. return err
  274. }
  275. }
  276. for _, rel := range stmt.Schema.Relationships.Relations {
  277. if constraint := rel.ParseConstraint(); constraint != nil && constraint.Field == field {
  278. if err = m.CreateIndex(value, constraint.Name); err != nil {
  279. return err
  280. }
  281. }
  282. }
  283. }
  284. return err
  285. })
  286. }
  287. func (m Migrator) DropConstraint(value interface{}, name string) error {
  288. return m.RunWithValue(value, func(stmt *gorm.Statement) error {
  289. return m.DB.Exec(
  290. "ALTER TABLE ? DROP CONSTRAINT ?",
  291. clause.Table{Name: stmt.Table}, clause.Column{Name: name},
  292. ).Error
  293. })
  294. }
  295. func (m Migrator) HasConstraint(value interface{}, name string) bool {
  296. var count int64
  297. m.RunWithValue(value, func(stmt *gorm.Statement) error {
  298. currentDatabase := m.DB.Migrator().CurrentDatabase()
  299. return m.DB.Raw(
  300. "SELECT count(*) FROM INFORMATION_SCHEMA.referential_constraints WHERE constraint_schema = ? AND table_name = ? AND constraint_name = ?",
  301. currentDatabase, stmt.Table, name,
  302. ).Row().Scan(&count)
  303. })
  304. return count > 0
  305. }
  306. func (m Migrator) BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{}) {
  307. for _, opt := range opts {
  308. str := stmt.Quote(opt.DBName)
  309. if opt.Expression != "" {
  310. str = opt.Expression
  311. } else if opt.Length > 0 {
  312. str += fmt.Sprintf("(%d)", opt.Length)
  313. }
  314. if opt.Collate != "" {
  315. str += " COLLATE " + opt.Collate
  316. }
  317. if opt.Sort != "" {
  318. str += " " + opt.Sort
  319. }
  320. results = append(results, clause.Expr{SQL: str})
  321. }
  322. return
  323. }
  324. type BuildIndexOptionsInterface interface {
  325. BuildIndexOptions([]schema.IndexOption, *gorm.Statement) []interface{}
  326. }
  327. func (m Migrator) CreateIndex(value interface{}, name string) error {
  328. return m.RunWithValue(value, func(stmt *gorm.Statement) error {
  329. err := fmt.Errorf("failed to create index with name %v", name)
  330. indexes := stmt.Schema.ParseIndexes()
  331. if idx, ok := indexes[name]; ok {
  332. opts := m.DB.Migrator().(BuildIndexOptionsInterface).BuildIndexOptions(idx.Fields, stmt)
  333. values := []interface{}{clause.Column{Name: idx.Name}, clause.Table{Name: stmt.Table}, opts}
  334. createIndexSQL := "CREATE "
  335. if idx.Class != "" {
  336. createIndexSQL += idx.Class + " "
  337. }
  338. createIndexSQL += "INDEX ? ON ??"
  339. if idx.Comment != "" {
  340. values = append(values, idx.Comment)
  341. createIndexSQL += " COMMENT ?"
  342. }
  343. if idx.Type != "" {
  344. createIndexSQL += " USING " + idx.Type
  345. }
  346. return m.DB.Exec(createIndexSQL, values...).Error
  347. } else if field := stmt.Schema.LookUpField(name); field != nil {
  348. for _, idx := range indexes {
  349. for _, idxOpt := range idx.Fields {
  350. if idxOpt.Field == field {
  351. if err = m.CreateIndex(value, idx.Name); err != nil {
  352. return err
  353. }
  354. }
  355. }
  356. }
  357. }
  358. return err
  359. })
  360. }
  361. func (m Migrator) DropIndex(value interface{}, name string) error {
  362. return m.RunWithValue(value, func(stmt *gorm.Statement) error {
  363. return m.DB.Exec("DROP INDEX ? ON ?", clause.Column{Name: name}, clause.Table{Name: stmt.Table}).Error
  364. })
  365. }
  366. func (m Migrator) HasIndex(value interface{}, name string) bool {
  367. var count int64
  368. m.RunWithValue(value, func(stmt *gorm.Statement) error {
  369. currentDatabase := m.DB.Migrator().CurrentDatabase()
  370. return m.DB.Raw(
  371. "SELECT count(*) FROM information_schema.statistics WHERE table_schema = ? AND table_name = ? AND index_name = ?",
  372. currentDatabase, stmt.Table, name,
  373. ).Row().Scan(&count)
  374. })
  375. return count > 0
  376. }
  377. func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error {
  378. return m.RunWithValue(value, func(stmt *gorm.Statement) error {
  379. return m.DB.Exec(
  380. "ALTER TABLE ? RENAME INDEX ? TO ?",
  381. clause.Table{Name: stmt.Table}, clause.Column{Name: oldName}, clause.Column{Name: newName},
  382. ).Error
  383. })
  384. }
  385. func (m Migrator) CurrentDatabase() (name string) {
  386. m.DB.Raw("SELECT DATABASE()").Row().Scan(&name)
  387. return
  388. }