dashboard_mig.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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", new(RawSqlMigration).
  82. Sqlite("SELECT 0 WHERE 0;").
  83. Postgres("SELECT 0;").
  84. Mysql("ALTER TABLE dashboard MODIFY data MEDIUMTEXT;"))
  85. // add column to store updater of a dashboard
  86. mg.AddMigration("Add column updated_by in dashboard - v2", NewAddColumnMigration(dashboardV2, &Column{
  87. Name: "updated_by", Type: DB_Int, Nullable: true,
  88. }))
  89. // add column to store creator of a dashboard
  90. mg.AddMigration("Add column created_by in dashboard - v2", NewAddColumnMigration(dashboardV2, &Column{
  91. Name: "created_by", Type: DB_Int, Nullable: true,
  92. }))
  93. // add column to store gnetId
  94. mg.AddMigration("Add column gnetId in dashboard", NewAddColumnMigration(dashboardV2, &Column{
  95. Name: "gnet_id", Type: DB_BigInt, Nullable: true,
  96. }))
  97. mg.AddMigration("Add index for gnetId in dashboard", NewAddIndexMigration(dashboardV2, &Index{
  98. Cols: []string{"gnet_id"}, Type: IndexType,
  99. }))
  100. // add column to store plugin_id
  101. mg.AddMigration("Add column plugin_id in dashboard", NewAddColumnMigration(dashboardV2, &Column{
  102. Name: "plugin_id", Type: DB_NVarchar, Nullable: true, Length: 189,
  103. }))
  104. mg.AddMigration("Add index for plugin_id in dashboard", NewAddIndexMigration(dashboardV2, &Index{
  105. Cols: []string{"org_id", "plugin_id"}, Type: IndexType,
  106. }))
  107. // dashboard_id index for dashboard_tag table
  108. mg.AddMigration("Add index for dashboard_id in dashboard_tag", NewAddIndexMigration(dashboardTagV1, &Index{
  109. Cols: []string{"dashboard_id"}, Type: IndexType,
  110. }))
  111. mg.AddMigration("Update dashboard table charset", NewTableCharsetMigration("dashboard", []*Column{
  112. {Name: "slug", Type: DB_NVarchar, Length: 189, Nullable: false},
  113. {Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false},
  114. {Name: "plugin_id", Type: DB_NVarchar, Nullable: true, Length: 189},
  115. {Name: "data", Type: DB_MediumText, Nullable: false},
  116. }))
  117. mg.AddMigration("Update dashboard_tag table charset", NewTableCharsetMigration("dashboard_tag", []*Column{
  118. {Name: "term", Type: DB_NVarchar, Length: 50, Nullable: false},
  119. }))
  120. // add column to store folder_id for dashboard folder structure
  121. mg.AddMigration("Add column folder_id in dashboard", NewAddColumnMigration(dashboardV2, &Column{
  122. Name: "folder_id", Type: DB_BigInt, Nullable: false, Default: "0",
  123. }))
  124. mg.AddMigration("Add column isFolder in dashboard", NewAddColumnMigration(dashboardV2, &Column{
  125. Name: "is_folder", Type: DB_Bool, Nullable: false, Default: "0",
  126. }))
  127. // add column to flag if dashboard has an ACL
  128. mg.AddMigration("Add column has_acl in dashboard", NewAddColumnMigration(dashboardV2, &Column{
  129. Name: "has_acl", Type: DB_Bool, Nullable: false, Default: "0",
  130. }))
  131. mg.AddMigration("Add column uid in dashboard", NewAddColumnMigration(dashboardV2, &Column{
  132. Name: "uid", Type: DB_NVarchar, Length: 40, Nullable: true,
  133. }))
  134. mg.AddMigration("Update uid column values in dashboard", new(RawSqlMigration).
  135. Sqlite("UPDATE dashboard SET uid=printf('%09d',id) WHERE uid IS NULL;").
  136. Postgres("UPDATE dashboard SET uid=lpad('' || id,9,'0') WHERE uid IS NULL;").
  137. Mysql("UPDATE dashboard SET uid=lpad(id,9,'0') WHERE uid IS NULL;"))
  138. mg.AddMigration("Add unique index dashboard_org_id_uid", NewAddIndexMigration(dashboardV2, &Index{
  139. Cols: []string{"org_id", "uid"}, Type: UniqueIndex,
  140. }))
  141. mg.AddMigration("Remove unique index org_id_slug", NewDropIndexMigration(dashboardV2, &Index{
  142. Cols: []string{"org_id", "slug"}, Type: UniqueIndex,
  143. }))
  144. mg.AddMigration("Update dashboard title length", NewTableCharsetMigration("dashboard", []*Column{
  145. {Name: "title", Type: DB_NVarchar, Length: 189, Nullable: false},
  146. }))
  147. mg.AddMigration("Add unique index for dashboard_org_id_title_folder_id", NewAddIndexMigration(dashboardV2, &Index{
  148. Cols: []string{"org_id", "folder_id", "title"}, Type: UniqueIndex,
  149. }))
  150. dashboardExtrasTable := Table{
  151. Name: "dashboard_provisioning",
  152. Columns: []*Column{
  153. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  154. {Name: "dashboard_id", Type: DB_BigInt, Nullable: true},
  155. {Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
  156. {Name: "external_id", Type: DB_Text, Nullable: false},
  157. {Name: "updated", Type: DB_DateTime, Nullable: false},
  158. },
  159. Indices: []*Index{
  160. {Cols: []string{"dashboard_id"}},
  161. {Cols: []string{"dashboard_id", "name"}, Type: IndexType},
  162. },
  163. }
  164. mg.AddMigration("create dashboard_provisioning", NewAddTableMigration(dashboardExtrasTable))
  165. dashboardExtrasTableV2 := Table{
  166. Name: "dashboard_provisioning",
  167. Columns: []*Column{
  168. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  169. {Name: "dashboard_id", Type: DB_BigInt, Nullable: true},
  170. {Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
  171. {Name: "external_id", Type: DB_Text, Nullable: false},
  172. {Name: "updated", Type: DB_Int, Default: "0", Nullable: false},
  173. },
  174. Indices: []*Index{
  175. {Cols: []string{"dashboard_id"}},
  176. {Cols: []string{"dashboard_id", "name"}, Type: IndexType},
  177. },
  178. }
  179. addTableReplaceMigrations(mg, dashboardExtrasTable, dashboardExtrasTableV2, 2, map[string]string{
  180. "id": "id",
  181. "dashboard_id": "dashboard_id",
  182. "name": "name",
  183. "external_id": "external_id",
  184. })
  185. }