Forráskód Böngészése

Added loglevel to migrator, added dashboard table & index migrations

Torkel Ödegaard 11 éve
szülő
commit
2379c5b770

+ 41 - 11
pkg/services/sqlstore/migrations/migrations.go

@@ -3,8 +3,13 @@ package migrations
 import "time"
 
 func AddMigrations(mg *Migrator) {
+	addMigrationLogMigrations(mg)
+	addUserMigrations(mg)
+	addAccountMigrations(mg)
+	addDashboardMigration(mg)
+}
 
-	//-------  migration_log table -------------------
+func addMigrationLogMigrations(mg *Migrator) {
 	mg.AddMigration("create migration_log table", new(AddTableMigration).
 		Name("migration_log").WithColumns(
 		&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
@@ -14,8 +19,9 @@ func AddMigrations(mg *Migrator) {
 		&Column{Name: "error", Type: DB_Text},
 		&Column{Name: "timestamp", Type: DB_DateTime},
 	))
+}
 
-	//-------  user table -------------------
+func addUserMigrations(mg *Migrator) {
 	mg.AddMigration("create user table", new(AddTableMigration).
 		Name("user").WithColumns(
 		&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
@@ -30,17 +36,19 @@ func AddMigrations(mg *Migrator) {
 		&Column{Name: "created", Type: DB_DateTime, Nullable: false},
 		&Column{Name: "updated", Type: DB_DateTime, Nullable: false},
 	))
+
 	//-------  user table indexes ------------------
 	mg.AddMigration("add unique index UIX_user.login", new(AddIndexMigration).
 		Name("UIX_user_login").Table("user").Columns("login"))
 	mg.AddMigration("add unique index UIX_user.email", new(AddIndexMigration).
 		Name("UIX_user_email").Table("user").Columns("email"))
+}
 
-	//-------  account table -------------------
+func addAccountMigrations(mg *Migrator) {
 	mg.AddMigration("create account table", new(AddTableMigration).
 		Name("account").WithColumns(
 		&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
-		&Column{Name: "name", Type: DB_NVarchar, Length: 255},
+		&Column{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
 		&Column{Name: "created", Type: DB_DateTime, Nullable: false},
 		&Column{Name: "updated", Type: DB_DateTime, Nullable: false},
 	))
@@ -61,14 +69,36 @@ func AddMigrations(mg *Migrator) {
 
 	mg.AddMigration("add unique index UIX_account_user", new(AddIndexMigration).
 		Name("UIX_account_user").Table("account_user").Columns("account_id", "user_id"))
+}
+
+type Dashboard struct {
+	Id        int64
+	Slug      string `xorm:"index(IX_AccountIdSlug)"`
+	AccountId int64  `xorm:"index(IX_AccountIdSlug)"`
+
+	Created time.Time
+	Updated time.Time
 
+	Title string
+	Data  map[string]interface{}
 }
 
-type MigrationLog struct {
-	Id          int64
-	MigrationId string
-	Sql         string
-	Success     bool
-	Error       string
-	Timestamp   time.Time
+func addDashboardMigration(mg *Migrator) {
+	mg.AddMigration("create dashboard table", new(AddTableMigration).
+		Name("dashboard").WithColumns(
+		&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
+		&Column{Name: "slug", Type: DB_NVarchar, Length: 255, Nullable: false},
+		&Column{Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false},
+		&Column{Name: "data", Type: DB_Text, Nullable: false},
+		&Column{Name: "account_id", Type: DB_BigInt, Nullable: false},
+		&Column{Name: "created", Type: DB_DateTime, Nullable: false},
+		&Column{Name: "updated", Type: DB_DateTime, Nullable: false},
+	))
+
+	//-------  indexes ------------------
+	mg.AddMigration("add unique index UIX_dashboard.account_id", new(AddIndexMigration).
+		Name("UIX_dashboard_account_id").Table("dashboard").Columns("account_id"))
+
+	mg.AddMigration("add unique index UIX_dashboard_account_id_slug", new(AddIndexMigration).
+		Name("UIX_dashboard_account_id_slug").Table("dashboard").Columns("account_id", "slug"))
 }

+ 2 - 2
pkg/services/sqlstore/migrations/migrations_test.go

@@ -35,7 +35,7 @@ func TestMigrations(t *testing.T) {
 	log.NewLogger(0, "console", `{"level": 0}`)
 
 	testDBs := [][]string{
-		//[]string{"mysql", "grafana:password@tcp(localhost:3306)/grafana_tests?charset=utf8"},
+		[]string{"mysql", "grafana:password@tcp(localhost:3306)/grafana_tests?charset=utf8"},
 		[]string{"sqlite3", ":memory:"},
 	}
 
@@ -50,6 +50,7 @@ func TestMigrations(t *testing.T) {
 			}
 
 			mg := NewMigrator(x)
+			mg.LogLevel = log.DEBUG
 			AddMigrations(mg)
 
 			err = mg.Start()
@@ -58,7 +59,6 @@ func TestMigrations(t *testing.T) {
 			tables, err := x.DBMetas()
 			So(err, ShouldBeNil)
 
-			//So(len(tables), ShouldEqual, 2)
 			fmt.Printf("\nDB Schema after migration: table count: %v\n", len(tables))
 
 			for _, table := range tables {

+ 18 - 3
pkg/services/sqlstore/migrations/migrator.go

@@ -18,6 +18,15 @@ type Migrator struct {
 	migrations []Migration
 }
 
+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
@@ -79,7 +88,9 @@ func (mg *Migrator) Start() error {
 	for _, m := range mg.migrations {
 		_, exists := logMap[m.Id()]
 		if exists {
-			log.Debug("Migrator:: Skipping migration: %v, Already executed", m.Id())
+			if mg.LogLevel <= log.DEBUG {
+				log.Debug("Migrator:: Skipping migration: %v, Already executed", m.Id())
+			}
 			continue
 		}
 
@@ -91,7 +102,9 @@ func (mg *Migrator) Start() error {
 			Timestamp:   time.Now(),
 		}
 
-		log.Debug("Migrator: Executing SQL: \n %v \n", sql)
+		if mg.LogLevel <= log.DEBUG {
+			log.Debug("Migrator: Executing SQL: \n %v \n", sql)
+		}
 
 		if err := mg.exec(m); err != nil {
 			record.Error = err.Error()
@@ -107,7 +120,9 @@ func (mg *Migrator) Start() error {
 }
 
 func (mg *Migrator) exec(m Migration) error {
-	log.Info("Migrator::exec migration id: %v", m.Id())
+	if mg.LogLevel <= log.INFO {
+		log.Info("Migrator::exec migration id: %v", m.Id())
+	}
 
 	err := mg.inTransaction(func(sess *xorm.Session) error {
 		_, err := sess.Exec(m.Sql(mg.dialect))

+ 1 - 1
pkg/services/sqlstore/sqlstore.go

@@ -35,7 +35,7 @@ var (
 func init() {
 	tables = make([]interface{}, 0)
 
-	tables = append(tables, new(m.Dashboard), new(m.DataSource), new(DashboardTag),
+	tables = append(tables, new(m.DataSource), new(DashboardTag),
 		new(m.Token))
 }