annotation_mig.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package migrations
  2. import (
  3. "github.com/go-xorm/xorm"
  4. . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
  5. )
  6. func addAnnotationMig(mg *Migrator) {
  7. table := Table{
  8. Name: "annotation",
  9. Columns: []*Column{
  10. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  11. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  12. {Name: "alert_id", Type: DB_BigInt, Nullable: true},
  13. {Name: "user_id", Type: DB_BigInt, Nullable: true},
  14. {Name: "dashboard_id", Type: DB_BigInt, Nullable: true},
  15. {Name: "panel_id", Type: DB_BigInt, Nullable: true},
  16. {Name: "category_id", Type: DB_BigInt, Nullable: true},
  17. {Name: "type", Type: DB_NVarchar, Length: 25, Nullable: false},
  18. {Name: "title", Type: DB_Text, Nullable: false},
  19. {Name: "text", Type: DB_Text, Nullable: false},
  20. {Name: "metric", Type: DB_NVarchar, Length: 255, Nullable: true},
  21. {Name: "prev_state", Type: DB_NVarchar, Length: 25, Nullable: false},
  22. {Name: "new_state", Type: DB_NVarchar, Length: 25, Nullable: false},
  23. {Name: "data", Type: DB_Text, Nullable: false},
  24. {Name: "epoch", Type: DB_BigInt, Nullable: false},
  25. },
  26. Indices: []*Index{
  27. {Cols: []string{"org_id", "alert_id"}, Type: IndexType},
  28. {Cols: []string{"org_id", "type"}, Type: IndexType},
  29. {Cols: []string{"org_id", "category_id"}, Type: IndexType},
  30. {Cols: []string{"org_id", "dashboard_id", "panel_id", "epoch"}, Type: IndexType},
  31. {Cols: []string{"org_id", "epoch"}, Type: IndexType},
  32. },
  33. }
  34. mg.AddMigration("Drop old annotation table v4", NewDropTableMigration("annotation"))
  35. mg.AddMigration("create annotation table v5", NewAddTableMigration(table))
  36. // create indices
  37. mg.AddMigration("add index annotation 0 v3", NewAddIndexMigration(table, table.Indices[0]))
  38. mg.AddMigration("add index annotation 1 v3", NewAddIndexMigration(table, table.Indices[1]))
  39. mg.AddMigration("add index annotation 2 v3", NewAddIndexMigration(table, table.Indices[2]))
  40. mg.AddMigration("add index annotation 3 v3", NewAddIndexMigration(table, table.Indices[3]))
  41. mg.AddMigration("add index annotation 4 v3", NewAddIndexMigration(table, table.Indices[4]))
  42. mg.AddMigration("Update annotation table charset", NewTableCharsetMigration("annotation", []*Column{
  43. {Name: "type", Type: DB_NVarchar, Length: 25, Nullable: false},
  44. {Name: "title", Type: DB_Text, Nullable: false},
  45. {Name: "text", Type: DB_Text, Nullable: false},
  46. {Name: "metric", Type: DB_NVarchar, Length: 255, Nullable: true},
  47. {Name: "prev_state", Type: DB_NVarchar, Length: 25, Nullable: false},
  48. {Name: "new_state", Type: DB_NVarchar, Length: 25, Nullable: false},
  49. {Name: "data", Type: DB_Text, Nullable: false},
  50. }))
  51. mg.AddMigration("Add column region_id to annotation table", NewAddColumnMigration(table, &Column{
  52. Name: "region_id", Type: DB_BigInt, Nullable: true, Default: "0",
  53. }))
  54. categoryIdIndex := &Index{Cols: []string{"org_id", "category_id"}, Type: IndexType}
  55. mg.AddMigration("Drop category_id index", NewDropIndexMigration(table, categoryIdIndex))
  56. mg.AddMigration("Add column tags to annotation table", NewAddColumnMigration(table, &Column{
  57. Name: "tags", Type: DB_NVarchar, Nullable: true, Length: 500,
  58. }))
  59. ///
  60. /// Annotation tag
  61. ///
  62. annotationTagTable := Table{
  63. Name: "annotation_tag",
  64. Columns: []*Column{
  65. {Name: "annotation_id", Type: DB_BigInt, Nullable: false},
  66. {Name: "tag_id", Type: DB_BigInt, Nullable: false},
  67. },
  68. Indices: []*Index{
  69. {Cols: []string{"annotation_id", "tag_id"}, Type: UniqueIndex},
  70. },
  71. }
  72. mg.AddMigration("Create annotation_tag table v2", NewAddTableMigration(annotationTagTable))
  73. mg.AddMigration("Add unique index annotation_tag.annotation_id_tag_id", NewAddIndexMigration(annotationTagTable, annotationTagTable.Indices[0]))
  74. //
  75. // clear alert text
  76. //
  77. updateTextFieldSql := "UPDATE annotation SET TEXT = '' WHERE alert_id > 0"
  78. mg.AddMigration("Update alert annotations and set TEXT to empty", NewRawSqlMigration(updateTextFieldSql))
  79. //
  80. // Add a 'created' & 'updated' column
  81. //
  82. mg.AddMigration("Add created time to annotation table", NewAddColumnMigration(table, &Column{
  83. Name: "created", Type: DB_BigInt, Nullable: true, Default: "0",
  84. }))
  85. mg.AddMigration("Add updated time to annotation table", NewAddColumnMigration(table, &Column{
  86. Name: "updated", Type: DB_BigInt, Nullable: true, Default: "0",
  87. }))
  88. mg.AddMigration("Add index for created in annotation table", NewAddIndexMigration(table, &Index{
  89. Cols: []string{"org_id", "created"}, Type: IndexType,
  90. }))
  91. mg.AddMigration("Add index for updated in annotation table", NewAddIndexMigration(table, &Index{
  92. Cols: []string{"org_id", "updated"}, Type: IndexType,
  93. }))
  94. //
  95. // Convert epoch saved as seconds to milliseconds
  96. //
  97. updateEpochSql := "UPDATE annotation SET epoch = (epoch*1000) where epoch < 9999999999"
  98. mg.AddMigration("Convert existing annotations from seconds to milliseconds", NewRawSqlMigration(updateEpochSql))
  99. //
  100. // 6.4: Make Regions a single annotation row
  101. //
  102. mg.AddMigration("Add epoch_end column", NewAddColumnMigration(table, &Column{
  103. Name: "epoch_end", Type: DB_BigInt, Nullable: false, Default: "0",
  104. }))
  105. mg.AddMigration("Add index for epoch_end", NewAddIndexMigration(table, &Index{
  106. Cols: []string{"org_id", "epoch", "epoch_end"}, Type: IndexType,
  107. }))
  108. mg.AddMigration("Make epoch_end the same as epoch", NewRawSqlMigration("UPDATE annotation SET epoch_end = epoch"))
  109. mg.AddMigration("Move region to single row", &AddMakeRegionSingleRowMigration{})
  110. // TODO! drop region_id column?
  111. }
  112. type AddMakeRegionSingleRowMigration struct {
  113. MigrationBase
  114. }
  115. func (m *AddMakeRegionSingleRowMigration) Sql(dialect Dialect) string {
  116. return "code migration"
  117. }
  118. type TempRegionInfoDTO struct {
  119. RegionId int64
  120. Epoch int64
  121. }
  122. func (m *AddMakeRegionSingleRowMigration) Exec(sess *xorm.Session, mg *Migrator) error {
  123. regions := make([]*TempRegionInfoDTO, 0)
  124. err := sess.SQL("SELECT region_id, epoch FROM annotation WHERE region_id>0 AND region_id <> id").Find(&regions)
  125. if err != nil {
  126. return err
  127. }
  128. for _, region := range regions {
  129. _, err := sess.Exec("UPDATE annotation SET epoch_end = ? WHERE id = ?", region.Epoch, region.RegionId)
  130. if err != nil {
  131. return err
  132. }
  133. }
  134. sess.Exec("DELETE FROM annotation WHERE region_id > 0 AND id <> region_id")
  135. return nil
  136. }