This commit fixes the following golint warnings: pkg/bus/bus.go:64:9: if block ends with a return statement, so drop this else and outdent its block pkg/bus/bus.go:84:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:137:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:177:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:183:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:199:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:208:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/components/dynmap/dynmap.go:236:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:242:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:257:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:263:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:278:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:284:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:299:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:331:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:350:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:356:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:366:12: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:390:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:396:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:405:12: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:427:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:433:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:442:12: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:459:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:465:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:474:12: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:491:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:497:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:506:12: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:523:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:529:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:538:12: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:555:9: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:561:10: if block ends with a return statement, so drop this else and outdent its block pkg/components/dynmap/dynmap.go:570:12: if block ends with a return statement, so drop this else and outdent its block pkg/login/ldap.go:55:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/login/ldap_test.go:372:10: if block ends with a return statement, so drop this else and outdent its block pkg/middleware/middleware_test.go:213:12: if block ends with a return statement, so drop this else and outdent its block pkg/plugins/dashboard_importer.go:153:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/plugins/dashboards_updater.go:39:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/plugins/dashboards_updater.go:121:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/plugins/plugins.go:210:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/plugins/plugins.go:235:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/eval_context.go:111:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/notifier.go:92:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/notifier.go:98:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/notifier.go:122:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/rule.go:108:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/rule.go:118:10: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/rule.go:121:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/alerting/notifiers/telegram.go:94:10: if block ends with a return statement, so drop this else and outdent its block pkg/services/sqlstore/annotation.go:34:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/sqlstore/annotation.go:99:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/sqlstore/dashboard_test.go:107:13: if block ends with a return statement, so drop this else and outdent its block pkg/services/sqlstore/plugin_setting.go:78:10: if block ends with a return statement, so drop this else and outdent its block pkg/services/sqlstore/preferences.go:91:10: if block ends with a return statement, so drop this else and outdent its block pkg/services/sqlstore/user.go:50:10: if block ends with a return statement, so drop this else and outdent its block pkg/services/sqlstore/migrator/migrator.go:106:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/services/sqlstore/migrator/postgres_dialect.go:48:10: if block ends with a return statement, so drop this else and outdent its block pkg/tsdb/time_range.go:59:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/tsdb/time_range.go:67:9: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary) pkg/tsdb/cloudwatch/metric_find_query.go:225:9: if block ends with a return statement, so drop this else and outdent its block pkg/util/filepath.go:68:11: if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary)
165 lines
3.1 KiB
Go
165 lines
3.1 KiB
Go
package migrator
|
|
|
|
import (
|
|
"time"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
"github.com/go-xorm/xorm"
|
|
"github.com/grafana/grafana/pkg/log"
|
|
_ "github.com/lib/pq"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
type Migrator struct {
|
|
x *xorm.Engine
|
|
dialect Dialect
|
|
migrations []Migration
|
|
Logger log.Logger
|
|
}
|
|
|
|
type MigrationLog struct {
|
|
Id int64
|
|
MigrationId string
|
|
Sql string
|
|
Success bool
|
|
Error string
|
|
Timestamp time.Time
|
|
}
|
|
|
|
func NewMigrator(engine *xorm.Engine) *Migrator {
|
|
mg := &Migrator{}
|
|
mg.x = engine
|
|
mg.Logger = log.New("migrator")
|
|
mg.migrations = make([]Migration, 0)
|
|
mg.dialect = NewDialect(mg.x.DriverName())
|
|
return mg
|
|
}
|
|
|
|
func (mg *Migrator) MigrationsCount() int {
|
|
return len(mg.migrations)
|
|
}
|
|
|
|
func (mg *Migrator) AddMigration(id string, m Migration) {
|
|
m.SetId(id)
|
|
mg.migrations = append(mg.migrations, m)
|
|
}
|
|
|
|
func (mg *Migrator) GetMigrationLog() (map[string]MigrationLog, error) {
|
|
logMap := make(map[string]MigrationLog)
|
|
logItems := make([]MigrationLog, 0)
|
|
|
|
exists, err := mg.x.IsTableExist(new(MigrationLog))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !exists {
|
|
return logMap, nil
|
|
}
|
|
|
|
if err = mg.x.Find(&logItems); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for _, logItem := range logItems {
|
|
if !logItem.Success {
|
|
continue
|
|
}
|
|
logMap[logItem.MigrationId] = logItem
|
|
}
|
|
|
|
return logMap, nil
|
|
}
|
|
|
|
func (mg *Migrator) Start() error {
|
|
mg.Logger.Info("Starting DB migration")
|
|
|
|
logMap, err := mg.GetMigrationLog()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, m := range mg.migrations {
|
|
_, exists := logMap[m.Id()]
|
|
if exists {
|
|
mg.Logger.Debug("Skipping migration: Already executed", "id", m.Id())
|
|
continue
|
|
}
|
|
|
|
sql := m.Sql(mg.dialect)
|
|
|
|
record := MigrationLog{
|
|
MigrationId: m.Id(),
|
|
Sql: sql,
|
|
Timestamp: time.Now(),
|
|
}
|
|
|
|
mg.Logger.Debug("Executing", "sql", sql)
|
|
|
|
err := mg.inTransaction(func(sess *xorm.Session) error {
|
|
err := mg.exec(m, sess)
|
|
if err != nil {
|
|
mg.Logger.Error("Exec failed", "error", err, "sql", sql)
|
|
record.Error = err.Error()
|
|
sess.Insert(&record)
|
|
return err
|
|
}
|
|
record.Success = true
|
|
sess.Insert(&record)
|
|
return nil
|
|
})
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
|
|
mg.Logger.Info("Executing migration", "id", m.Id())
|
|
|
|
condition := m.GetCondition()
|
|
if condition != nil {
|
|
sql, args := condition.Sql(mg.dialect)
|
|
results, err := sess.SQL(sql).Query(args...)
|
|
if err != nil || len(results) == 0 {
|
|
mg.Logger.Info("Skipping migration condition not fulfilled", "id", m.Id())
|
|
return sess.Rollback()
|
|
}
|
|
}
|
|
|
|
_, err := sess.Exec(m.Sql(mg.dialect))
|
|
if err != nil {
|
|
mg.Logger.Error("Executing migration failed", "id", m.Id(), "error", err)
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type dbTransactionFunc func(sess *xorm.Session) error
|
|
|
|
func (mg *Migrator) inTransaction(callback dbTransactionFunc) error {
|
|
var err error
|
|
|
|
sess := mg.x.NewSession()
|
|
defer sess.Close()
|
|
|
|
if err = sess.Begin(); err != nil {
|
|
return err
|
|
}
|
|
|
|
err = callback(sess)
|
|
|
|
if err != nil {
|
|
sess.Rollback()
|
|
return err
|
|
} else if err = sess.Commit(); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|