dashboard_version_mig.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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", new(RawSqlMigration).
  28. Sqlite(setVersionTo1WhereZeroSQL).
  29. Postgres(setVersionTo1WhereZeroSQL).
  30. Mysql(setVersionTo1WhereZeroSQL))
  31. const rawSQL = `INSERT INTO dashboard_version
  32. (
  33. dashboard_id,
  34. version,
  35. parent_version,
  36. restored_from,
  37. created,
  38. created_by,
  39. message,
  40. data
  41. )
  42. SELECT
  43. dashboard.id,
  44. dashboard.version,
  45. dashboard.version,
  46. dashboard.version,
  47. dashboard.updated,
  48. COALESCE(dashboard.updated_by, -1),
  49. '',
  50. dashboard.data
  51. FROM dashboard;`
  52. mg.AddMigration("save existing dashboard data in dashboard_version table v1", new(RawSqlMigration).
  53. Sqlite(rawSQL).
  54. Postgres(rawSQL).
  55. Mysql(rawSQL))
  56. }