migrator.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package migrations
  2. import (
  3. "time"
  4. _ "github.com/go-sql-driver/mysql"
  5. "github.com/go-xorm/xorm"
  6. _ "github.com/lib/pq"
  7. _ "github.com/mattn/go-sqlite3"
  8. "github.com/torkelo/grafana-pro/pkg/log"
  9. )
  10. type Migrator struct {
  11. LogLevel log.LogLevel
  12. x *xorm.Engine
  13. dialect Dialect
  14. migrations []Migration
  15. }
  16. type MigrationLog struct {
  17. Id int64
  18. MigrationId string
  19. Sql string
  20. Success bool
  21. Error string
  22. Timestamp time.Time
  23. }
  24. func NewMigrator(engine *xorm.Engine) *Migrator {
  25. mg := &Migrator{}
  26. mg.x = engine
  27. mg.LogLevel = log.WARN
  28. mg.migrations = make([]Migration, 0)
  29. switch mg.x.DriverName() {
  30. case MYSQL:
  31. mg.dialect = NewMysqlDialect()
  32. case SQLITE:
  33. mg.dialect = NewSqlite3Dialect()
  34. }
  35. return mg
  36. }
  37. func (mg *Migrator) AddMigration(id string, m Migration) {
  38. m.SetId(id)
  39. mg.migrations = append(mg.migrations, m)
  40. }
  41. func (mg *Migrator) GetMigrationLog() (map[string]MigrationLog, error) {
  42. logMap := make(map[string]MigrationLog)
  43. logItems := make([]MigrationLog, 0)
  44. exists, err := mg.x.IsTableExist(new(MigrationLog))
  45. if err != nil {
  46. return nil, err
  47. }
  48. if !exists {
  49. return logMap, nil
  50. }
  51. if err = mg.x.Find(&logItems); err != nil {
  52. return nil, err
  53. }
  54. for _, logItem := range logItems {
  55. if !logItem.Success {
  56. continue
  57. }
  58. logMap[logItem.MigrationId] = logItem
  59. }
  60. return logMap, nil
  61. }
  62. func (mg *Migrator) Start() error {
  63. if mg.LogLevel <= log.INFO {
  64. log.Info("Migrator:: Starting DB migration")
  65. }
  66. logMap, err := mg.GetMigrationLog()
  67. if err != nil {
  68. return err
  69. }
  70. for _, m := range mg.migrations {
  71. _, exists := logMap[m.Id()]
  72. if exists {
  73. if mg.LogLevel <= log.DEBUG {
  74. log.Debug("Migrator:: Skipping migration: %v, Already executed", m.Id())
  75. }
  76. continue
  77. }
  78. sql := m.Sql(mg.dialect)
  79. record := MigrationLog{
  80. MigrationId: m.Id(),
  81. Sql: sql,
  82. Timestamp: time.Now(),
  83. }
  84. if mg.LogLevel <= log.DEBUG {
  85. log.Debug("Migrator: Executing SQL: \n %v \n", sql)
  86. }
  87. if err := mg.exec(m); err != nil {
  88. record.Error = err.Error()
  89. mg.x.Insert(&record)
  90. return err
  91. } else {
  92. record.Success = true
  93. mg.x.Insert(&record)
  94. }
  95. }
  96. return nil
  97. }
  98. func (mg *Migrator) exec(m Migration) error {
  99. if mg.LogLevel <= log.INFO {
  100. log.Info("Migrator::exec migration id: %v", m.Id())
  101. }
  102. err := mg.inTransaction(func(sess *xorm.Session) error {
  103. _, err := sess.Exec(m.Sql(mg.dialect))
  104. if err != nil {
  105. log.Error(3, "Migrator::exec FAILED migration id: %v, err: %v", m.Id(), err)
  106. return err
  107. }
  108. return nil
  109. })
  110. if err != nil {
  111. return err
  112. }
  113. return nil
  114. }
  115. type dbTransactionFunc func(sess *xorm.Session) error
  116. func (mg *Migrator) inTransaction(callback dbTransactionFunc) error {
  117. var err error
  118. sess := mg.x.NewSession()
  119. defer sess.Close()
  120. if err = sess.Begin(); err != nil {
  121. return err
  122. }
  123. err = callback(sess)
  124. if err != nil {
  125. sess.Rollback()
  126. return err
  127. } else if err = sess.Commit(); err != nil {
  128. return err
  129. }
  130. return nil
  131. }