dashboard_mig.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package migrations
  2. import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
  3. func addDashboardMigration(mg *Migrator) {
  4. var dashboardV1 = Table{
  5. Name: "dashboard",
  6. Columns: []*Column{
  7. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  8. {Name: "version", Type: DB_Int, Nullable: false},
  9. {Name: "slug", Type: DB_NVarchar, Length: 255, Nullable: false},
  10. {Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false},
  11. {Name: "data", Type: DB_Text, Nullable: false},
  12. {Name: "account_id", Type: DB_BigInt, Nullable: false},
  13. {Name: "created", Type: DB_DateTime, Nullable: false},
  14. {Name: "updated", Type: DB_DateTime, Nullable: false},
  15. },
  16. Indices: []*Index{
  17. {Cols: []string{"account_id"}},
  18. {Cols: []string{"account_id", "slug"}, Type: UniqueIndex},
  19. },
  20. }
  21. mg.AddMigration("create dashboard table", NewAddTableMigration(dashboardV1))
  22. //------- indexes ------------------
  23. mg.AddMigration("add index dashboard.account_id", NewAddIndexMigration(dashboardV1, dashboardV1.Indices[0]))
  24. mg.AddMigration("add unique index dashboard_account_id_slug", NewAddIndexMigration(dashboardV1, dashboardV1.Indices[1]))
  25. dashboardTagV1 := Table{
  26. Name: "dashboard_tag",
  27. Columns: []*Column{
  28. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  29. {Name: "dashboard_id", Type: DB_BigInt, Nullable: false},
  30. {Name: "term", Type: DB_NVarchar, Length: 50, Nullable: false},
  31. },
  32. Indices: []*Index{
  33. {Cols: []string{"dashboard_id", "term"}, Type: UniqueIndex},
  34. },
  35. }
  36. mg.AddMigration("create dashboard_tag table", NewAddTableMigration(dashboardTagV1))
  37. mg.AddMigration("add unique index dashboard_tag.dasboard_id_term", NewAddIndexMigration(dashboardTagV1, dashboardTagV1.Indices[0]))
  38. // ---------------------
  39. // account -> org changes
  40. //------- drop dashboard indexes ------------------
  41. addDropAllIndicesMigrations(mg, "v1", dashboardTagV1)
  42. //------- rename table ------------------
  43. addTableRenameMigration(mg, "dashboard", "dashboard_v1", "v1")
  44. // dashboard v2
  45. var dashboardV2 = Table{
  46. Name: "dashboard",
  47. Columns: []*Column{
  48. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  49. {Name: "version", Type: DB_Int, Nullable: false},
  50. {Name: "slug", Type: DB_NVarchar, Length: 255, Nullable: false},
  51. {Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false},
  52. {Name: "data", Type: DB_Text, Nullable: false},
  53. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  54. {Name: "created", Type: DB_DateTime, Nullable: false},
  55. {Name: "updated", Type: DB_DateTime, Nullable: false},
  56. },
  57. Indices: []*Index{
  58. {Cols: []string{"org_id"}},
  59. {Cols: []string{"org_id", "slug"}, Type: UniqueIndex},
  60. },
  61. }
  62. // recreate table
  63. mg.AddMigration("create dashboard v2", NewAddTableMigration(dashboardV2))
  64. // recreate indices
  65. addTableIndicesMigrations(mg, "v2", dashboardV2)
  66. // copy data
  67. mg.AddMigration("copy dashboard v1 to v2", NewCopyTableDataMigration("dashboard", "dashboard_v1", map[string]string{
  68. "id": "id",
  69. "version": "version",
  70. "slug": "slug",
  71. "title": "title",
  72. "data": "data",
  73. "org_id": "account_id",
  74. "created": "created",
  75. "updated": "updated",
  76. }))
  77. mg.AddMigration("drop table dashboard_v1", NewDropTableMigration("dashboard_v1"))
  78. // change column type of dashboard.data
  79. mg.AddMigration("alter dashboard.data to mediumtext v1", new(RawSqlMigration).
  80. Sqlite("SELECT 0 WHERE 0;").
  81. Postgres("SELECT 0;").
  82. Mysql("ALTER TABLE dashboard MODIFY data MEDIUMTEXT;"))
  83. // add column to store updater of a dashboard
  84. mg.AddMigration("Add column updated_by in dashboard - v2", NewAddColumnMigration(dashboardV2, &Column{
  85. Name: "updated_by", Type: DB_Int, Nullable: true,
  86. }))
  87. // add column to store creator of a dashboard
  88. mg.AddMigration("Add column created_by in dashboard - v2", NewAddColumnMigration(dashboardV2, &Column{
  89. Name: "created_by", Type: DB_Int, Nullable: true,
  90. }))
  91. // add column to store gnetId
  92. mg.AddMigration("Add column gnetId in dashboard", NewAddColumnMigration(dashboardV2, &Column{
  93. Name: "gnet_id", Type: DB_BigInt, Nullable: true,
  94. }))
  95. mg.AddMigration("Add index for gnetId in dashboard", NewAddIndexMigration(dashboardV2, &Index{
  96. Cols: []string{"gnet_id"}, Type: IndexType,
  97. }))
  98. // add column to store plugin_id
  99. mg.AddMigration("Add column plugin_id in dashboard", NewAddColumnMigration(dashboardV2, &Column{
  100. Name: "plugin_id", Type: DB_NVarchar, Nullable: true, Length: 255,
  101. }))
  102. mg.AddMigration("Add index for plugin_id in dashboard", NewAddIndexMigration(dashboardV2, &Index{
  103. Cols: []string{"org_id", "plugin_id"}, Type: IndexType,
  104. }))
  105. }