dashboard_version_mig.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package migrations
  2. import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
  3. func addDashboardVersionMigration(mg *Migrator) {
  4. dashboardVersionV1 := Table{
  5. Name: "dashboard_version",
  6. Columns: []*Column{
  7. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  8. {Name: "dashboard_id", Type: DB_BigInt},
  9. {Name: "parent_version", Type: DB_Int, Nullable: false},
  10. {Name: "restored_from", Type: DB_Int, Nullable: false},
  11. {Name: "version", Type: DB_Int, Nullable: false},
  12. {Name: "created", Type: DB_DateTime, Nullable: false},
  13. {Name: "created_by", Type: DB_BigInt, Nullable: false},
  14. {Name: "message", Type: DB_Text, Nullable: false},
  15. {Name: "data", Type: DB_Text, Nullable: false},
  16. },
  17. Indices: []*Index{
  18. {Cols: []string{"dashboard_id"}},
  19. {Cols: []string{"dashboard_id", "version"}, Type: UniqueIndex},
  20. },
  21. }
  22. mg.AddMigration("create dashboard_version table v1", NewAddTableMigration(dashboardVersionV1))
  23. mg.AddMigration("add index dashboard_version.dashboard_id", NewAddIndexMigration(dashboardVersionV1, dashboardVersionV1.Indices[0]))
  24. mg.AddMigration("add unique index dashboard_version.dashboard_id and dashboard_version.version", NewAddIndexMigration(dashboardVersionV1, dashboardVersionV1.Indices[1]))
  25. // before new dashboards where created with version 0, now they are always inserted with version 1
  26. const setVersionTo1WhereZeroSQL = `UPDATE dashboard SET version = 1 WHERE version = 0`
  27. mg.AddMigration("Set dashboard version to 1 where 0", NewRawSqlMigration(setVersionTo1WhereZeroSQL))
  28. const rawSQL = `INSERT INTO dashboard_version
  29. (
  30. dashboard_id,
  31. version,
  32. parent_version,
  33. restored_from,
  34. created,
  35. created_by,
  36. message,
  37. data
  38. )
  39. SELECT
  40. dashboard.id,
  41. dashboard.version,
  42. dashboard.version,
  43. dashboard.version,
  44. dashboard.updated,
  45. COALESCE(dashboard.updated_by, -1),
  46. '',
  47. dashboard.data
  48. FROM dashboard;`
  49. mg.AddMigration("save existing dashboard data in dashboard_version table v1", NewRawSqlMigration(rawSQL))
  50. // change column type of dashboard_version.data
  51. mg.AddMigration("alter dashboard_version.data to mediumtext v1", NewRawSqlMigration("").
  52. Mysql("ALTER TABLE dashboard_version MODIFY data MEDIUMTEXT;"))
  53. }