| 
									
										
										
										
											2015-01-20 21:15:48 +08:00
										 |  |  | package migrator | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	_ "github.com/go-sql-driver/mysql" | 
					
						
							| 
									
										
										
										
											2019-05-13 14:45:54 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							| 
									
										
										
										
											2019-10-22 20:08:18 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/util/errutil" | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 	_ "github.com/lib/pq" | 
					
						
							|  |  |  | 	_ "github.com/mattn/go-sqlite3" | 
					
						
							| 
									
										
										
										
											2020-04-01 21:57:21 +08:00
										 |  |  | 	"xorm.io/xorm" | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Migrator struct { | 
					
						
							|  |  |  | 	x          *xorm.Engine | 
					
						
							| 
									
										
										
										
											2018-08-21 19:30:39 +08:00
										 |  |  | 	Dialect    Dialect | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 	migrations []Migration | 
					
						
							| 
									
										
										
										
											2016-06-07 05:06:44 +08:00
										 |  |  | 	Logger     log.Logger | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-20 15:50:08 +08:00
										 |  |  | type MigrationLog struct { | 
					
						
							|  |  |  | 	Id          int64 | 
					
						
							|  |  |  | 	MigrationId string | 
					
						
							|  |  |  | 	Sql         string | 
					
						
							|  |  |  | 	Success     bool | 
					
						
							|  |  |  | 	Error       string | 
					
						
							|  |  |  | 	Timestamp   time.Time | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | func NewMigrator(engine *xorm.Engine) *Migrator { | 
					
						
							|  |  |  | 	mg := &Migrator{} | 
					
						
							|  |  |  | 	mg.x = engine | 
					
						
							| 
									
										
										
										
											2016-06-07 18:11:41 +08:00
										 |  |  | 	mg.Logger = log.New("migrator") | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 	mg.migrations = make([]Migration, 0) | 
					
						
							| 
									
										
										
										
											2018-08-21 19:30:39 +08:00
										 |  |  | 	mg.Dialect = NewDialect(mg.x) | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 	return mg | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 22:30:12 +08:00
										 |  |  | func (mg *Migrator) MigrationsCount() int { | 
					
						
							|  |  |  | 	return len(mg.migrations) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 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) { | 
					
						
							| 
									
										
										
										
											2015-01-19 17:44:16 +08:00
										 |  |  | 	logMap := make(map[string]MigrationLog) | 
					
						
							|  |  |  | 	logItems := make([]MigrationLog, 0) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 	exists, err := mg.x.IsTableExist(new(MigrationLog)) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2020-07-23 21:47:26 +08:00
										 |  |  | 		return nil, errutil.Wrap("failed to check table existence", err) | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	if !exists { | 
					
						
							| 
									
										
										
										
											2015-01-19 17:44:16 +08:00
										 |  |  | 		return logMap, nil | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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 { | 
					
						
							| 
									
										
										
										
											2020-07-23 21:47:26 +08:00
										 |  |  | 	mg.Logger.Info("Starting DB migrations") | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	logMap, err := mg.GetMigrationLog() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, m := range mg.migrations { | 
					
						
							| 
									
										
										
										
											2020-06-29 23:04:38 +08:00
										 |  |  | 		m := m | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 		_, exists := logMap[m.Id()] | 
					
						
							|  |  |  | 		if exists { | 
					
						
							| 
									
										
										
										
											2016-06-07 05:06:44 +08:00
										 |  |  | 			mg.Logger.Debug("Skipping migration: Already executed", "id", m.Id()) | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 			continue | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-21 19:30:39 +08:00
										 |  |  | 		sql := m.Sql(mg.Dialect) | 
					
						
							| 
									
										
										
										
											2015-01-19 17:44:16 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 		record := MigrationLog{ | 
					
						
							|  |  |  | 			MigrationId: m.Id(), | 
					
						
							| 
									
										
										
										
											2015-01-19 17:44:16 +08:00
										 |  |  | 			Sql:         sql, | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 			Timestamp:   time.Now(), | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 13:49:40 +08:00
										 |  |  | 		err := mg.inTransaction(func(sess *xorm.Session) error { | 
					
						
							| 
									
										
											  
											
												Outdent code after if block that ends with return (golint)
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)
											
										 
											2018-04-28 04:42:49 +08:00
										 |  |  | 			err := mg.exec(m, sess) | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							| 
									
										
										
										
											2016-09-30 13:49:40 +08:00
										 |  |  | 				mg.Logger.Error("Exec failed", "error", err, "sql", sql) | 
					
						
							|  |  |  | 				record.Error = err.Error() | 
					
						
							| 
									
										
										
										
											2019-10-22 20:08:18 +08:00
										 |  |  | 				if _, err := sess.Insert(&record); err != nil { | 
					
						
							|  |  |  | 					return err | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2016-09-30 13:49:40 +08:00
										 |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
											  
											
												Outdent code after if block that ends with return (golint)
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)
											
										 
											2018-04-28 04:42:49 +08:00
										 |  |  | 			record.Success = true | 
					
						
							| 
									
										
										
										
											2019-10-22 20:08:18 +08:00
										 |  |  | 			_, err = sess.Insert(&record) | 
					
						
							|  |  |  | 			return err | 
					
						
							| 
									
										
										
										
											2016-09-30 13:49:40 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							| 
									
										
										
										
											2020-07-23 21:47:26 +08:00
										 |  |  | 			return errutil.Wrap("migration failed", err) | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 13:49:40 +08:00
										 |  |  | func (mg *Migrator) exec(m Migration, sess *xorm.Session) error { | 
					
						
							| 
									
										
										
										
											2016-06-11 17:38:25 +08:00
										 |  |  | 	mg.Logger.Info("Executing migration", "id", m.Id()) | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 13:49:40 +08:00
										 |  |  | 	condition := m.GetCondition() | 
					
						
							|  |  |  | 	if condition != nil { | 
					
						
							| 
									
										
										
										
											2018-08-21 19:30:39 +08:00
										 |  |  | 		sql, args := condition.Sql(mg.Dialect) | 
					
						
							| 
									
										
										
										
											2018-12-19 04:47:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-19 06:02:08 +08:00
										 |  |  | 		if sql != "" { | 
					
						
							|  |  |  | 			mg.Logger.Debug("Executing migration condition sql", "id", m.Id(), "sql", sql, "args", args) | 
					
						
							|  |  |  | 			results, err := sess.SQL(sql, args...).Query() | 
					
						
							|  |  |  | 			if err != nil { | 
					
						
							|  |  |  | 				mg.Logger.Error("Executing migration condition failed", "id", m.Id(), "error", err) | 
					
						
							|  |  |  | 				return err | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if !condition.IsFulfilled(results) { | 
					
						
							|  |  |  | 				mg.Logger.Warn("Skipping migration: Already executed, but not recorded in migration log", "id", m.Id()) | 
					
						
							|  |  |  | 				return nil | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-09-30 13:49:40 +08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-08-21 19:30:39 +08:00
										 |  |  | 	var err error | 
					
						
							|  |  |  | 	if codeMigration, ok := m.(CodeMigration); ok { | 
					
						
							| 
									
										
										
										
											2018-12-19 04:47:45 +08:00
										 |  |  | 		mg.Logger.Debug("Executing code migration", "id", m.Id()) | 
					
						
							| 
									
										
										
										
											2018-08-21 19:30:39 +08:00
										 |  |  | 		err = codeMigration.Exec(sess, mg) | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2018-12-19 04:47:45 +08:00
										 |  |  | 		sql := m.Sql(mg.Dialect) | 
					
						
							|  |  |  | 		mg.Logger.Debug("Executing sql migration", "id", m.Id(), "sql", sql) | 
					
						
							|  |  |  | 		_, err = sess.Exec(sql) | 
					
						
							| 
									
										
										
										
											2018-08-21 19:30:39 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-30 13:49:40 +08:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		mg.Logger.Error("Executing migration failed", "id", m.Id(), "error", err) | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type dbTransactionFunc func(sess *xorm.Session) error | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (mg *Migrator) inTransaction(callback dbTransactionFunc) error { | 
					
						
							|  |  |  | 	sess := mg.x.NewSession() | 
					
						
							|  |  |  | 	defer sess.Close() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-22 20:08:18 +08:00
										 |  |  | 	if err := sess.Begin(); err != nil { | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-22 20:08:18 +08:00
										 |  |  | 	if err := callback(sess); err != nil { | 
					
						
							|  |  |  | 		if rollErr := sess.Rollback(); err != rollErr { | 
					
						
							|  |  |  | 			return errutil.Wrapf(err, "Failed to roll back transaction due to error: %s", rollErr) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return err | 
					
						
							| 
									
										
										
										
											2019-10-22 20:08:18 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := sess.Commit(); err != nil { | 
					
						
							| 
									
										
										
										
											2015-01-19 01:41:03 +08:00
										 |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |