dashboard_mig.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package migrations
  2. import (
  3. . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
  4. )
  5. func addDashboardMigration(mg *Migrator) {
  6. var dashboardV1 = Table{
  7. Name: "dashboard",
  8. Columns: []*Column{
  9. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  10. {Name: "version", Type: DB_Int, Nullable: false},
  11. {Name: "slug", Type: DB_NVarchar, Length: 189, Nullable: false},
  12. {Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false},
  13. {Name: "data", Type: DB_Text, Nullable: false},
  14. {Name: "account_id", Type: DB_BigInt, Nullable: false},
  15. {Name: "created", Type: DB_DateTime, Nullable: false},
  16. {Name: "updated", Type: DB_DateTime, Nullable: false},
  17. },
  18. Indices: []*Index{
  19. {Cols: []string{"account_id"}},
  20. {Cols: []string{"account_id", "slug"}, Type: UniqueIndex},
  21. },
  22. }
  23. mg.AddMigration("create dashboard table", NewAddTableMigration(dashboardV1))
  24. //------- indexes ------------------
  25. mg.AddMigration("add index dashboard.account_id", NewAddIndexMigration(dashboardV1, dashboardV1.Indices[0]))
  26. mg.AddMigration("add unique index dashboard_account_id_slug", NewAddIndexMigration(dashboardV1, dashboardV1.Indices[1]))
  27. dashboardTagV1 := Table{
  28. Name: "dashboard_tag",
  29. Columns: []*Column{
  30. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  31. {Name: "dashboard_id", Type: DB_BigInt, Nullable: false},
  32. {Name: "term", Type: DB_NVarchar, Length: 50, Nullable: false},
  33. },
  34. Indices: []*Index{
  35. {Cols: []string{"dashboard_id", "term"}, Type: UniqueIndex},
  36. },
  37. }
  38. mg.AddMigration("create dashboard_tag table", NewAddTableMigration(dashboardTagV1))
  39. mg.AddMigration("add unique index dashboard_tag.dasboard_id_term", NewAddIndexMigration(dashboardTagV1, dashboardTagV1.Indices[0]))
  40. // ---------------------
  41. // account -> org changes
  42. //------- drop dashboard indexes ------------------
  43. addDropAllIndicesMigrations(mg, "v1", dashboardTagV1)
  44. //------- rename table ------------------
  45. addTableRenameMigration(mg, "dashboard", "dashboard_v1", "v1")
  46. // dashboard v2
  47. var dashboardV2 = Table{
  48. Name: "dashboard",
  49. Columns: []*Column{
  50. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  51. {Name: "version", Type: DB_Int, Nullable: false},
  52. {Name: "slug", Type: DB_NVarchar, Length: 189, Nullable: false},
  53. {Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false},
  54. {Name: "data", Type: DB_Text, Nullable: false},
  55. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  56. {Name: "created", Type: DB_DateTime, Nullable: false},
  57. {Name: "updated", Type: DB_DateTime, Nullable: false},
  58. },
  59. Indices: []*Index{
  60. {Cols: []string{"org_id"}},
  61. {Cols: []string{"org_id", "slug"}, Type: UniqueIndex},
  62. },
  63. }
  64. // recreate table
  65. mg.AddMigration("create dashboard v2", NewAddTableMigration(dashboardV2))
  66. // recreate indices
  67. addTableIndicesMigrations(mg, "v2", dashboardV2)
  68. // copy data
  69. mg.AddMigration("copy dashboard v1 to v2", NewCopyTableDataMigration("dashboard", "dashboard_v1", map[string]string{
  70. "id": "id",
  71. "version": "version",
  72. "slug": "slug",
  73. "title": "title",
  74. "data": "data",
  75. "org_id": "account_id",
  76. "created": "created",
  77. "updated": "updated",
  78. }))
  79. mg.AddMigration("drop table dashboard_v1", NewDropTableMigration("dashboard_v1"))
  80. // change column type of dashboard.data
  81. mg.AddMigration("alter dashboard.data to mediumtext v1", NewRawSqlMigration("").
  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: 189,
  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. // dashboard_id index for dashboard_tag table
  106. mg.AddMigration("Add index for dashboard_id in dashboard_tag", NewAddIndexMigration(dashboardTagV1, &Index{
  107. Cols: []string{"dashboard_id"}, Type: IndexType,
  108. }))
  109. mg.AddMigration("Update dashboard table charset", NewTableCharsetMigration("dashboard", []*Column{
  110. {Name: "slug", Type: DB_NVarchar, Length: 189, Nullable: false},
  111. {Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false},
  112. {Name: "plugin_id", Type: DB_NVarchar, Nullable: true, Length: 189},
  113. {Name: "data", Type: DB_MediumText, Nullable: false},
  114. }))
  115. mg.AddMigration("Update dashboard_tag table charset", NewTableCharsetMigration("dashboard_tag", []*Column{
  116. {Name: "term", Type: DB_NVarchar, Length: 50, Nullable: false},
  117. }))
  118. // add column to store folder_id for dashboard folder structure
  119. mg.AddMigration("Add column folder_id in dashboard", NewAddColumnMigration(dashboardV2, &Column{
  120. Name: "folder_id", Type: DB_BigInt, Nullable: false, Default: "0",
  121. }))
  122. mg.AddMigration("Add column isFolder in dashboard", NewAddColumnMigration(dashboardV2, &Column{
  123. Name: "is_folder", Type: DB_Bool, Nullable: false, Default: "0",
  124. }))
  125. // add column to flag if dashboard has an ACL
  126. mg.AddMigration("Add column has_acl in dashboard", NewAddColumnMigration(dashboardV2, &Column{
  127. Name: "has_acl", Type: DB_Bool, Nullable: false, Default: "0",
  128. }))
  129. mg.AddMigration("Add column uid in dashboard", NewAddColumnMigration(dashboardV2, &Column{
  130. Name: "uid", Type: DB_NVarchar, Length: 40, Nullable: true,
  131. }))
  132. mg.AddMigration("Update uid column values in dashboard", NewRawSqlMigration("").
  133. Sqlite("UPDATE dashboard SET uid=printf('%09d',id) WHERE uid IS NULL;").
  134. Postgres("UPDATE dashboard SET uid=lpad('' || id,9,'0') WHERE uid IS NULL;").
  135. Mysql("UPDATE dashboard SET uid=lpad(id,9,'0') WHERE uid IS NULL;"))
  136. mg.AddMigration("Add unique index dashboard_org_id_uid", NewAddIndexMigration(dashboardV2, &Index{
  137. Cols: []string{"org_id", "uid"}, Type: UniqueIndex,
  138. }))
  139. mg.AddMigration("Remove unique index org_id_slug", NewDropIndexMigration(dashboardV2, &Index{
  140. Cols: []string{"org_id", "slug"}, Type: UniqueIndex,
  141. }))
  142. mg.AddMigration("Update dashboard title length", NewTableCharsetMigration("dashboard", []*Column{
  143. {Name: "title", Type: DB_NVarchar, Length: 189, Nullable: false},
  144. }))
  145. mg.AddMigration("Add unique index for dashboard_org_id_title_folder_id", NewAddIndexMigration(dashboardV2, &Index{
  146. Cols: []string{"org_id", "folder_id", "title"}, Type: UniqueIndex,
  147. }))
  148. dashboardExtrasTable := Table{
  149. Name: "dashboard_provisioning",
  150. Columns: []*Column{
  151. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  152. {Name: "dashboard_id", Type: DB_BigInt, Nullable: true},
  153. {Name: "name", Type: DB_NVarchar, Length: 150, Nullable: false},
  154. {Name: "external_id", Type: DB_Text, Nullable: false},
  155. {Name: "updated", Type: DB_DateTime, Nullable: false},
  156. },
  157. Indices: []*Index{},
  158. }
  159. mg.AddMigration("create dashboard_provisioning", NewAddTableMigration(dashboardExtrasTable))
  160. dashboardExtrasTableV2 := Table{
  161. Name: "dashboard_provisioning",
  162. Columns: []*Column{
  163. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  164. {Name: "dashboard_id", Type: DB_BigInt, Nullable: true},
  165. {Name: "name", Type: DB_NVarchar, Length: 150, Nullable: false},
  166. {Name: "external_id", Type: DB_Text, Nullable: false},
  167. {Name: "updated", Type: DB_Int, Default: "0", Nullable: false},
  168. },
  169. Indices: []*Index{
  170. {Cols: []string{"dashboard_id"}},
  171. {Cols: []string{"dashboard_id", "name"}, Type: IndexType},
  172. },
  173. }
  174. addTableReplaceMigrations(mg, dashboardExtrasTable, dashboardExtrasTableV2, 2, map[string]string{
  175. "id": "id",
  176. "dashboard_id": "dashboard_id",
  177. "name": "name",
  178. "external_id": "external_id",
  179. })
  180. mg.AddMigration("Add check_sum column", NewAddColumnMigration(dashboardExtrasTableV2, &Column{
  181. Name: "check_sum", Type: DB_NVarchar, Length: 32, Nullable: true,
  182. }))
  183. }