mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Added loglevel to migrator, added dashboard table & index migrations
This commit is contained in:
parent
581efa857b
commit
2379c5b770
@ -3,8 +3,13 @@ package migrations
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
func AddMigrations(mg *Migrator) {
|
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).
|
mg.AddMigration("create migration_log table", new(AddTableMigration).
|
||||||
Name("migration_log").WithColumns(
|
Name("migration_log").WithColumns(
|
||||||
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
&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: "error", Type: DB_Text},
|
||||||
&Column{Name: "timestamp", Type: DB_DateTime},
|
&Column{Name: "timestamp", Type: DB_DateTime},
|
||||||
))
|
))
|
||||||
|
}
|
||||||
|
|
||||||
//------- user table -------------------
|
func addUserMigrations(mg *Migrator) {
|
||||||
mg.AddMigration("create user table", new(AddTableMigration).
|
mg.AddMigration("create user table", new(AddTableMigration).
|
||||||
Name("user").WithColumns(
|
Name("user").WithColumns(
|
||||||
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
&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: "created", Type: DB_DateTime, Nullable: false},
|
||||||
&Column{Name: "updated", Type: DB_DateTime, Nullable: false},
|
&Column{Name: "updated", Type: DB_DateTime, Nullable: false},
|
||||||
))
|
))
|
||||||
|
|
||||||
//------- user table indexes ------------------
|
//------- user table indexes ------------------
|
||||||
mg.AddMigration("add unique index UIX_user.login", new(AddIndexMigration).
|
mg.AddMigration("add unique index UIX_user.login", new(AddIndexMigration).
|
||||||
Name("UIX_user_login").Table("user").Columns("login"))
|
Name("UIX_user_login").Table("user").Columns("login"))
|
||||||
mg.AddMigration("add unique index UIX_user.email", new(AddIndexMigration).
|
mg.AddMigration("add unique index UIX_user.email", new(AddIndexMigration).
|
||||||
Name("UIX_user_email").Table("user").Columns("email"))
|
Name("UIX_user_email").Table("user").Columns("email"))
|
||||||
|
}
|
||||||
|
|
||||||
//------- account table -------------------
|
func addAccountMigrations(mg *Migrator) {
|
||||||
mg.AddMigration("create account table", new(AddTableMigration).
|
mg.AddMigration("create account table", new(AddTableMigration).
|
||||||
Name("account").WithColumns(
|
Name("account").WithColumns(
|
||||||
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
&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: "created", Type: DB_DateTime, Nullable: false},
|
||||||
&Column{Name: "updated", 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).
|
mg.AddMigration("add unique index UIX_account_user", new(AddIndexMigration).
|
||||||
Name("UIX_account_user").Table("account_user").Columns("account_id", "user_id"))
|
Name("UIX_account_user").Table("account_user").Columns("account_id", "user_id"))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type MigrationLog struct {
|
type Dashboard struct {
|
||||||
Id int64
|
Id int64
|
||||||
MigrationId string
|
Slug string `xorm:"index(IX_AccountIdSlug)"`
|
||||||
Sql string
|
AccountId int64 `xorm:"index(IX_AccountIdSlug)"`
|
||||||
Success bool
|
|
||||||
Error string
|
Created time.Time
|
||||||
Timestamp time.Time
|
Updated time.Time
|
||||||
|
|
||||||
|
Title string
|
||||||
|
Data map[string]interface{}
|
||||||
|
}
|
||||||
|
|
||||||
|
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"))
|
||||||
}
|
}
|
||||||
|
@ -35,7 +35,7 @@ func TestMigrations(t *testing.T) {
|
|||||||
log.NewLogger(0, "console", `{"level": 0}`)
|
log.NewLogger(0, "console", `{"level": 0}`)
|
||||||
|
|
||||||
testDBs := [][]string{
|
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:"},
|
[]string{"sqlite3", ":memory:"},
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,6 +50,7 @@ func TestMigrations(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mg := NewMigrator(x)
|
mg := NewMigrator(x)
|
||||||
|
mg.LogLevel = log.DEBUG
|
||||||
AddMigrations(mg)
|
AddMigrations(mg)
|
||||||
|
|
||||||
err = mg.Start()
|
err = mg.Start()
|
||||||
@ -58,7 +59,6 @@ func TestMigrations(t *testing.T) {
|
|||||||
tables, err := x.DBMetas()
|
tables, err := x.DBMetas()
|
||||||
So(err, ShouldBeNil)
|
So(err, ShouldBeNil)
|
||||||
|
|
||||||
//So(len(tables), ShouldEqual, 2)
|
|
||||||
fmt.Printf("\nDB Schema after migration: table count: %v\n", len(tables))
|
fmt.Printf("\nDB Schema after migration: table count: %v\n", len(tables))
|
||||||
|
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
|
@ -18,6 +18,15 @@ type Migrator struct {
|
|||||||
migrations []Migration
|
migrations []Migration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type MigrationLog struct {
|
||||||
|
Id int64
|
||||||
|
MigrationId string
|
||||||
|
Sql string
|
||||||
|
Success bool
|
||||||
|
Error string
|
||||||
|
Timestamp time.Time
|
||||||
|
}
|
||||||
|
|
||||||
func NewMigrator(engine *xorm.Engine) *Migrator {
|
func NewMigrator(engine *xorm.Engine) *Migrator {
|
||||||
mg := &Migrator{}
|
mg := &Migrator{}
|
||||||
mg.x = engine
|
mg.x = engine
|
||||||
@ -79,7 +88,9 @@ func (mg *Migrator) Start() error {
|
|||||||
for _, m := range mg.migrations {
|
for _, m := range mg.migrations {
|
||||||
_, exists := logMap[m.Id()]
|
_, exists := logMap[m.Id()]
|
||||||
if exists {
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +102,9 @@ func (mg *Migrator) Start() error {
|
|||||||
Timestamp: time.Now(),
|
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 {
|
if err := mg.exec(m); err != nil {
|
||||||
record.Error = err.Error()
|
record.Error = err.Error()
|
||||||
@ -107,7 +120,9 @@ func (mg *Migrator) Start() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (mg *Migrator) exec(m Migration) 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 := mg.inTransaction(func(sess *xorm.Session) error {
|
||||||
_, err := sess.Exec(m.Sql(mg.dialect))
|
_, err := sess.Exec(m.Sql(mg.dialect))
|
||||||
|
@ -35,7 +35,7 @@ var (
|
|||||||
func init() {
|
func init() {
|
||||||
tables = make([]interface{}, 0)
|
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))
|
new(m.Token))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user