annotation_mig.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package migrations
  2. import (
  3. . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
  4. )
  5. func addAnnotationMig(mg *Migrator) {
  6. table := Table{
  7. Name: "annotation",
  8. Columns: []*Column{
  9. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  10. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  11. {Name: "alert_id", Type: DB_BigInt, Nullable: true},
  12. {Name: "user_id", Type: DB_BigInt, Nullable: true},
  13. {Name: "dashboard_id", Type: DB_BigInt, Nullable: true},
  14. {Name: "panel_id", Type: DB_BigInt, Nullable: true},
  15. {Name: "category_id", Type: DB_BigInt, Nullable: true},
  16. {Name: "type", Type: DB_NVarchar, Length: 25, Nullable: false},
  17. {Name: "title", Type: DB_Text, Nullable: false},
  18. {Name: "text", Type: DB_Text, Nullable: false},
  19. {Name: "metric", Type: DB_NVarchar, Length: 255, Nullable: true},
  20. {Name: "prev_state", Type: DB_NVarchar, Length: 25, Nullable: false},
  21. {Name: "new_state", Type: DB_NVarchar, Length: 25, Nullable: false},
  22. {Name: "data", Type: DB_Text, Nullable: false},
  23. {Name: "epoch", Type: DB_BigInt, Nullable: false},
  24. },
  25. Indices: []*Index{
  26. {Cols: []string{"org_id", "alert_id"}, Type: IndexType},
  27. {Cols: []string{"org_id", "type"}, Type: IndexType},
  28. {Cols: []string{"org_id", "category_id"}, Type: IndexType},
  29. {Cols: []string{"org_id", "dashboard_id", "panel_id", "epoch"}, Type: IndexType},
  30. {Cols: []string{"org_id", "epoch"}, Type: IndexType},
  31. },
  32. }
  33. mg.AddMigration("Drop old annotation table v4", NewDropTableMigration("annotation"))
  34. mg.AddMigration("create annotation table v5", NewAddTableMigration(table))
  35. // create indices
  36. mg.AddMigration("add index annotation 0 v3", NewAddIndexMigration(table, table.Indices[0]))
  37. mg.AddMigration("add index annotation 1 v3", NewAddIndexMigration(table, table.Indices[1]))
  38. mg.AddMigration("add index annotation 2 v3", NewAddIndexMigration(table, table.Indices[2]))
  39. mg.AddMigration("add index annotation 3 v3", NewAddIndexMigration(table, table.Indices[3]))
  40. mg.AddMigration("add index annotation 4 v3", NewAddIndexMigration(table, table.Indices[4]))
  41. mg.AddMigration("Update annotation table charset", NewTableCharsetMigration("annotation", []*Column{
  42. {Name: "type", Type: DB_NVarchar, Length: 25, Nullable: false},
  43. {Name: "title", Type: DB_Text, Nullable: false},
  44. {Name: "text", Type: DB_Text, Nullable: false},
  45. {Name: "metric", Type: DB_NVarchar, Length: 255, Nullable: true},
  46. {Name: "prev_state", Type: DB_NVarchar, Length: 25, Nullable: false},
  47. {Name: "new_state", Type: DB_NVarchar, Length: 25, Nullable: false},
  48. {Name: "data", Type: DB_Text, Nullable: false},
  49. }))
  50. mg.AddMigration("Add column region_id to annotation table", NewAddColumnMigration(table, &Column{
  51. Name: "region_id", Type: DB_BigInt, Nullable: true, Default: "0",
  52. }))
  53. categoryIdIndex := &Index{Cols: []string{"org_id", "category_id"}, Type: IndexType}
  54. mg.AddMigration("Drop category_id index", NewDropIndexMigration(table, categoryIdIndex))
  55. mg.AddMigration("Add column tags to annotation table", NewAddColumnMigration(table, &Column{
  56. Name: "tags", Type: DB_NVarchar, Nullable: true, Length: 500,
  57. }))
  58. ///
  59. /// Annotation tag
  60. ///
  61. annotationTagTable := Table{
  62. Name: "annotation_tag",
  63. Columns: []*Column{
  64. {Name: "annotation_id", Type: DB_BigInt, Nullable: false},
  65. {Name: "tag_id", Type: DB_BigInt, Nullable: false},
  66. },
  67. Indices: []*Index{
  68. {Cols: []string{"annotation_id", "tag_id"}, Type: UniqueIndex},
  69. },
  70. }
  71. mg.AddMigration("Create annotation_tag table v2", NewAddTableMigration(annotationTagTable))
  72. mg.AddMigration("Add unique index annotation_tag.annotation_id_tag_id", NewAddIndexMigration(annotationTagTable, annotationTagTable.Indices[0]))
  73. //
  74. // clear alert text
  75. //
  76. updateTextFieldSql := "UPDATE annotation SET TEXT = '' WHERE alert_id > 0"
  77. mg.AddMigration("Update alert annotations and set TEXT to empty", new(RawSqlMigration).
  78. Sqlite(updateTextFieldSql).
  79. Postgres(updateTextFieldSql).
  80. Mysql(updateTextFieldSql))
  81. }