| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | package migrations | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 22:28:30 +08:00
										 |  |  | import ( | 
					
						
							|  |  |  | 	. "github.com/grafana/grafana/pkg/services/sqlstore/migrator" | 
					
						
							|  |  |  | ) | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func addDashboardMigration(mg *Migrator) { | 
					
						
							| 
									
										
										
										
											2015-02-25 00:59:21 +08:00
										 |  |  | 	var dashboardV1 = Table{ | 
					
						
							|  |  |  | 		Name: "dashboard", | 
					
						
							|  |  |  | 		Columns: []*Column{ | 
					
						
							| 
									
										
										
										
											2015-03-07 23:23:22 +08:00
										 |  |  | 			{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, | 
					
						
							|  |  |  | 			{Name: "version", Type: DB_Int, Nullable: false}, | 
					
						
							| 
									
										
										
										
											2017-06-04 20:28:03 +08:00
										 |  |  | 			{Name: "slug", Type: DB_NVarchar, Length: 189, Nullable: false}, | 
					
						
							| 
									
										
										
										
											2015-03-07 23:23:22 +08:00
										 |  |  | 			{Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false}, | 
					
						
							|  |  |  | 			{Name: "data", Type: DB_Text, Nullable: false}, | 
					
						
							|  |  |  | 			{Name: "account_id", Type: DB_BigInt, Nullable: false}, | 
					
						
							|  |  |  | 			{Name: "created", Type: DB_DateTime, Nullable: false}, | 
					
						
							|  |  |  | 			{Name: "updated", Type: DB_DateTime, Nullable: false}, | 
					
						
							| 
									
										
										
										
											2015-02-25 00:59:21 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		Indices: []*Index{ | 
					
						
							| 
									
										
										
										
											2015-03-07 23:23:22 +08:00
										 |  |  | 			{Cols: []string{"account_id"}}, | 
					
						
							|  |  |  | 			{Cols: []string{"account_id", "slug"}, Type: UniqueIndex}, | 
					
						
							| 
									
										
										
										
											2015-02-25 00:59:21 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mg.AddMigration("create dashboard table", NewAddTableMigration(dashboardV1)) | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	//-------  indexes ------------------
 | 
					
						
							| 
									
										
										
										
											2015-02-25 00:59:21 +08:00
										 |  |  | 	mg.AddMigration("add index dashboard.account_id", NewAddIndexMigration(dashboardV1, dashboardV1.Indices[0])) | 
					
						
							|  |  |  | 	mg.AddMigration("add unique index dashboard_account_id_slug", NewAddIndexMigration(dashboardV1, dashboardV1.Indices[1])) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dashboardTagV1 := Table{ | 
					
						
							|  |  |  | 		Name: "dashboard_tag", | 
					
						
							|  |  |  | 		Columns: []*Column{ | 
					
						
							| 
									
										
										
										
											2015-03-07 23:23:22 +08:00
										 |  |  | 			{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, | 
					
						
							|  |  |  | 			{Name: "dashboard_id", Type: DB_BigInt, Nullable: false}, | 
					
						
							|  |  |  | 			{Name: "term", Type: DB_NVarchar, Length: 50, Nullable: false}, | 
					
						
							| 
									
										
										
										
											2015-02-25 00:59:21 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		Indices: []*Index{ | 
					
						
							| 
									
										
										
										
											2015-03-07 23:23:22 +08:00
										 |  |  | 			{Cols: []string{"dashboard_id", "term"}, Type: UniqueIndex}, | 
					
						
							| 
									
										
										
										
											2015-02-25 00:59:21 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mg.AddMigration("create dashboard_tag table", NewAddTableMigration(dashboardTagV1)) | 
					
						
							|  |  |  | 	mg.AddMigration("add unique index dashboard_tag.dasboard_id_term", NewAddIndexMigration(dashboardTagV1, dashboardTagV1.Indices[0])) | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// ---------------------
 | 
					
						
							|  |  |  | 	// account -> org changes
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-25 00:59:21 +08:00
										 |  |  | 	//-------  drop dashboard indexes ------------------
 | 
					
						
							|  |  |  | 	addDropAllIndicesMigrations(mg, "v1", dashboardTagV1) | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | 	//------- rename table ------------------
 | 
					
						
							| 
									
										
										
										
											2015-02-25 00:59:21 +08:00
										 |  |  | 	addTableRenameMigration(mg, "dashboard", "dashboard_v1", "v1") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// dashboard v2
 | 
					
						
							|  |  |  | 	var dashboardV2 = Table{ | 
					
						
							|  |  |  | 		Name: "dashboard", | 
					
						
							|  |  |  | 		Columns: []*Column{ | 
					
						
							| 
									
										
										
										
											2015-03-07 23:23:22 +08:00
										 |  |  | 			{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, | 
					
						
							|  |  |  | 			{Name: "version", Type: DB_Int, Nullable: false}, | 
					
						
							| 
									
										
										
										
											2017-05-29 14:31:36 +08:00
										 |  |  | 			{Name: "slug", Type: DB_NVarchar, Length: 189, Nullable: false}, | 
					
						
							| 
									
										
										
										
											2015-03-07 23:23:22 +08:00
										 |  |  | 			{Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false}, | 
					
						
							|  |  |  | 			{Name: "data", Type: DB_Text, Nullable: false}, | 
					
						
							|  |  |  | 			{Name: "org_id", Type: DB_BigInt, Nullable: false}, | 
					
						
							|  |  |  | 			{Name: "created", Type: DB_DateTime, Nullable: false}, | 
					
						
							|  |  |  | 			{Name: "updated", Type: DB_DateTime, Nullable: false}, | 
					
						
							| 
									
										
										
										
											2015-02-25 00:59:21 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		Indices: []*Index{ | 
					
						
							| 
									
										
										
										
											2015-03-07 23:23:22 +08:00
										 |  |  | 			{Cols: []string{"org_id"}}, | 
					
						
							|  |  |  | 			{Cols: []string{"org_id", "slug"}, Type: UniqueIndex}, | 
					
						
							| 
									
										
										
										
											2015-02-25 00:59:21 +08:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// recreate table
 | 
					
						
							|  |  |  | 	mg.AddMigration("create dashboard v2", NewAddTableMigration(dashboardV2)) | 
					
						
							|  |  |  | 	// recreate indices
 | 
					
						
							|  |  |  | 	addTableIndicesMigrations(mg, "v2", dashboardV2) | 
					
						
							|  |  |  | 	// copy data
 | 
					
						
							|  |  |  | 	mg.AddMigration("copy dashboard v1 to v2", NewCopyTableDataMigration("dashboard", "dashboard_v1", map[string]string{ | 
					
						
							|  |  |  | 		"id":      "id", | 
					
						
							|  |  |  | 		"version": "version", | 
					
						
							|  |  |  | 		"slug":    "slug", | 
					
						
							|  |  |  | 		"title":   "title", | 
					
						
							|  |  |  | 		"data":    "data", | 
					
						
							|  |  |  | 		"org_id":  "account_id", | 
					
						
							|  |  |  | 		"created": "created", | 
					
						
							|  |  |  | 		"updated": "updated", | 
					
						
							|  |  |  | 	})) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mg.AddMigration("drop table dashboard_v1", NewDropTableMigration("dashboard_v1")) | 
					
						
							| 
									
										
										
										
											2015-04-23 22:18:00 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// change column type of dashboard.data
 | 
					
						
							| 
									
										
										
										
											2018-05-10 22:54:21 +08:00
										 |  |  | 	mg.AddMigration("alter dashboard.data to mediumtext v1", NewRawSqlMigration(""). | 
					
						
							| 
									
										
										
										
											2015-04-23 22:18:00 +08:00
										 |  |  | 		Mysql("ALTER TABLE dashboard MODIFY data MEDIUMTEXT;")) | 
					
						
							| 
									
										
										
										
											2015-12-07 15:51:43 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-07 15:59:58 +08:00
										 |  |  | 	// add column to store updater of a dashboard
 | 
					
						
							| 
									
										
										
										
											2015-12-18 16:20:23 +08:00
										 |  |  | 	mg.AddMigration("Add column updated_by in dashboard - v2", NewAddColumnMigration(dashboardV2, &Column{ | 
					
						
							| 
									
										
										
										
											2015-12-18 17:52:05 +08:00
										 |  |  | 		Name: "updated_by", Type: DB_Int, Nullable: true, | 
					
						
							| 
									
										
										
										
											2015-12-07 15:59:58 +08:00
										 |  |  | 	})) | 
					
						
							| 
									
										
										
										
											2016-01-28 08:33:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-28 14:00:24 +08:00
										 |  |  | 	// add column to store creator of a dashboard
 | 
					
						
							|  |  |  | 	mg.AddMigration("Add column created_by in dashboard - v2", NewAddColumnMigration(dashboardV2, &Column{ | 
					
						
							|  |  |  | 		Name: "created_by", Type: DB_Int, Nullable: true, | 
					
						
							|  |  |  | 	})) | 
					
						
							| 
									
										
										
										
											2016-05-27 22:42:32 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// add column to store gnetId
 | 
					
						
							|  |  |  | 	mg.AddMigration("Add column gnetId in dashboard", NewAddColumnMigration(dashboardV2, &Column{ | 
					
						
							|  |  |  | 		Name: "gnet_id", Type: DB_BigInt, Nullable: true, | 
					
						
							|  |  |  | 	})) | 
					
						
							| 
									
										
										
										
											2016-06-17 17:36:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	mg.AddMigration("Add index for gnetId in dashboard", NewAddIndexMigration(dashboardV2, &Index{ | 
					
						
							|  |  |  | 		Cols: []string{"gnet_id"}, Type: IndexType, | 
					
						
							|  |  |  | 	})) | 
					
						
							| 
									
										
										
										
											2016-07-08 15:35:06 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// add column to store plugin_id
 | 
					
						
							|  |  |  | 	mg.AddMigration("Add column plugin_id in dashboard", NewAddColumnMigration(dashboardV2, &Column{ | 
					
						
							| 
									
										
										
										
											2017-06-04 20:28:03 +08:00
										 |  |  | 		Name: "plugin_id", Type: DB_NVarchar, Nullable: true, Length: 189, | 
					
						
							| 
									
										
										
										
											2016-07-08 15:35:06 +08:00
										 |  |  | 	})) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mg.AddMigration("Add index for plugin_id in dashboard", NewAddIndexMigration(dashboardV2, &Index{ | 
					
						
							|  |  |  | 		Cols: []string{"org_id", "plugin_id"}, Type: IndexType, | 
					
						
							|  |  |  | 	})) | 
					
						
							| 
									
										
										
										
											2016-09-22 03:32:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// dashboard_id index for dashboard_tag table
 | 
					
						
							|  |  |  | 	mg.AddMigration("Add index for dashboard_id in dashboard_tag", NewAddIndexMigration(dashboardTagV1, &Index{ | 
					
						
							|  |  |  | 		Cols: []string{"dashboard_id"}, Type: IndexType, | 
					
						
							|  |  |  | 	})) | 
					
						
							| 
									
										
										
										
											2017-03-28 20:34:53 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	mg.AddMigration("Update dashboard table charset", NewTableCharsetMigration("dashboard", []*Column{ | 
					
						
							| 
									
										
										
										
											2017-05-29 14:31:36 +08:00
										 |  |  | 		{Name: "slug", Type: DB_NVarchar, Length: 189, Nullable: false}, | 
					
						
							| 
									
										
										
										
											2017-03-28 20:34:53 +08:00
										 |  |  | 		{Name: "title", Type: DB_NVarchar, Length: 255, Nullable: false}, | 
					
						
							| 
									
										
										
										
											2017-06-04 20:28:03 +08:00
										 |  |  | 		{Name: "plugin_id", Type: DB_NVarchar, Nullable: true, Length: 189}, | 
					
						
							| 
									
										
										
										
											2017-03-28 20:34:53 +08:00
										 |  |  | 		{Name: "data", Type: DB_MediumText, Nullable: false}, | 
					
						
							|  |  |  | 	})) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mg.AddMigration("Update dashboard_tag table charset", NewTableCharsetMigration("dashboard_tag", []*Column{ | 
					
						
							|  |  |  | 		{Name: "term", Type: DB_NVarchar, Length: 50, Nullable: false}, | 
					
						
							|  |  |  | 	})) | 
					
						
							| 
									
										
										
										
											2017-03-27 20:36:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-24 04:00:26 +08:00
										 |  |  | 	// add column to store folder_id for dashboard folder structure
 | 
					
						
							|  |  |  | 	mg.AddMigration("Add column folder_id in dashboard", NewAddColumnMigration(dashboardV2, &Column{ | 
					
						
							| 
									
										
										
										
											2017-11-20 19:47:03 +08:00
										 |  |  | 		Name: "folder_id", Type: DB_BigInt, Nullable: false, Default: "0", | 
					
						
							| 
									
										
										
										
											2017-03-27 20:36:28 +08:00
										 |  |  | 	})) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mg.AddMigration("Add column isFolder in dashboard", NewAddColumnMigration(dashboardV2, &Column{ | 
					
						
							|  |  |  | 		Name: "is_folder", Type: DB_Bool, Nullable: false, Default: "0", | 
					
						
							|  |  |  | 	})) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 04:36:27 +08:00
										 |  |  | 	// add column to flag if dashboard has an ACL
 | 
					
						
							|  |  |  | 	mg.AddMigration("Add column has_acl in dashboard", NewAddColumnMigration(dashboardV2, &Column{ | 
					
						
							|  |  |  | 		Name: "has_acl", Type: DB_Bool, Nullable: false, Default: "0", | 
					
						
							|  |  |  | 	})) | 
					
						
							| 
									
										
										
										
											2018-01-23 19:28:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-29 21:57:19 +08:00
										 |  |  | 	mg.AddMigration("Add column uid in dashboard", NewAddColumnMigration(dashboardV2, &Column{ | 
					
						
							| 
									
										
										
										
											2018-01-30 18:15:51 +08:00
										 |  |  | 		Name: "uid", Type: DB_NVarchar, Length: 40, Nullable: true, | 
					
						
							| 
									
										
										
										
											2018-01-29 21:57:19 +08:00
										 |  |  | 	})) | 
					
						
							| 
									
										
										
										
											2018-01-29 23:24:22 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-10 22:54:21 +08:00
										 |  |  | 	mg.AddMigration("Update uid column values in dashboard", NewRawSqlMigration(""). | 
					
						
							| 
									
										
										
										
											2018-01-29 23:24:22 +08:00
										 |  |  | 		Sqlite("UPDATE dashboard SET uid=printf('%09d',id) WHERE uid IS NULL;"). | 
					
						
							| 
									
										
										
										
											2019-04-15 23:49:24 +08:00
										 |  |  | 		Postgres("UPDATE dashboard SET uid=lpad('' || id::text,9,'0') WHERE uid IS NULL;"). | 
					
						
							| 
									
										
										
										
											2018-01-29 23:24:22 +08:00
										 |  |  | 		Mysql("UPDATE dashboard SET uid=lpad(id,9,'0') WHERE uid IS NULL;")) | 
					
						
							| 
									
										
										
										
											2018-01-29 23:27:52 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-30 18:15:51 +08:00
										 |  |  | 	mg.AddMigration("Add unique index dashboard_org_id_uid", NewAddIndexMigration(dashboardV2, &Index{ | 
					
						
							|  |  |  | 		Cols: []string{"org_id", "uid"}, Type: UniqueIndex, | 
					
						
							| 
									
										
										
										
											2018-01-29 23:27:52 +08:00
										 |  |  | 	})) | 
					
						
							| 
									
										
										
										
											2018-01-31 00:06:23 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	mg.AddMigration("Remove unique index org_id_slug", NewDropIndexMigration(dashboardV2, &Index{ | 
					
						
							|  |  |  | 		Cols: []string{"org_id", "slug"}, Type: UniqueIndex, | 
					
						
							|  |  |  | 	})) | 
					
						
							| 
									
										
										
										
											2018-02-05 21:11:19 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-06 18:03:37 +08:00
										 |  |  | 	mg.AddMigration("Update dashboard title length", NewTableCharsetMigration("dashboard", []*Column{ | 
					
						
							|  |  |  | 		{Name: "title", Type: DB_NVarchar, Length: 189, Nullable: false}, | 
					
						
							|  |  |  | 	})) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-05 21:11:19 +08:00
										 |  |  | 	mg.AddMigration("Add unique index for dashboard_org_id_title_folder_id", NewAddIndexMigration(dashboardV2, &Index{ | 
					
						
							|  |  |  | 		Cols: []string{"org_id", "folder_id", "title"}, Type: UniqueIndex, | 
					
						
							|  |  |  | 	})) | 
					
						
							| 
									
										
										
										
											2018-02-06 18:03:37 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-23 19:28:56 +08:00
										 |  |  | 	dashboardExtrasTable := Table{ | 
					
						
							|  |  |  | 		Name: "dashboard_provisioning", | 
					
						
							|  |  |  | 		Columns: []*Column{ | 
					
						
							|  |  |  | 			{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, | 
					
						
							|  |  |  | 			{Name: "dashboard_id", Type: DB_BigInt, Nullable: true}, | 
					
						
							| 
									
										
										
										
											2018-02-16 18:46:36 +08:00
										 |  |  | 			{Name: "name", Type: DB_NVarchar, Length: 150, Nullable: false}, | 
					
						
							| 
									
										
										
										
											2018-01-23 19:28:56 +08:00
										 |  |  | 			{Name: "external_id", Type: DB_Text, Nullable: false}, | 
					
						
							| 
									
										
										
										
											2018-02-09 22:23:38 +08:00
										 |  |  | 			{Name: "updated", Type: DB_DateTime, Nullable: false}, | 
					
						
							| 
									
										
										
										
											2018-01-23 19:28:56 +08:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-02-17 00:25:46 +08:00
										 |  |  | 		Indices: []*Index{}, | 
					
						
							| 
									
										
										
										
											2018-01-23 19:28:56 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mg.AddMigration("create dashboard_provisioning", NewAddTableMigration(dashboardExtrasTable)) | 
					
						
							| 
									
										
										
										
											2018-02-14 22:28:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	dashboardExtrasTableV2 := Table{ | 
					
						
							|  |  |  | 		Name: "dashboard_provisioning", | 
					
						
							|  |  |  | 		Columns: []*Column{ | 
					
						
							|  |  |  | 			{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, | 
					
						
							|  |  |  | 			{Name: "dashboard_id", Type: DB_BigInt, Nullable: true}, | 
					
						
							| 
									
										
										
										
											2018-02-16 18:46:36 +08:00
										 |  |  | 			{Name: "name", Type: DB_NVarchar, Length: 150, Nullable: false}, | 
					
						
							| 
									
										
										
										
											2018-02-14 22:28:30 +08:00
										 |  |  | 			{Name: "external_id", Type: DB_Text, Nullable: false}, | 
					
						
							|  |  |  | 			{Name: "updated", Type: DB_Int, Default: "0", Nullable: false}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		Indices: []*Index{ | 
					
						
							|  |  |  | 			{Cols: []string{"dashboard_id"}}, | 
					
						
							|  |  |  | 			{Cols: []string{"dashboard_id", "name"}, Type: IndexType}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	addTableReplaceMigrations(mg, dashboardExtrasTable, dashboardExtrasTableV2, 2, map[string]string{ | 
					
						
							|  |  |  | 		"id":           "id", | 
					
						
							|  |  |  | 		"dashboard_id": "dashboard_id", | 
					
						
							|  |  |  | 		"name":         "name", | 
					
						
							|  |  |  | 		"external_id":  "external_id", | 
					
						
							|  |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2018-05-31 20:13:34 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	mg.AddMigration("Add check_sum column", NewAddColumnMigration(dashboardExtrasTableV2, &Column{ | 
					
						
							|  |  |  | 		Name: "check_sum", Type: DB_NVarchar, Length: 32, Nullable: true, | 
					
						
							|  |  |  | 	})) | 
					
						
							| 
									
										
										
										
											2015-02-24 18:46:34 +08:00
										 |  |  | } |