alert_mig.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package migrations
  2. import (
  3. . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
  4. )
  5. func addAlertMigrations(mg *Migrator) {
  6. alertV1 := Table{
  7. Name: "alert",
  8. Columns: []*Column{
  9. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  10. {Name: "version", Type: DB_BigInt, Nullable: false},
  11. {Name: "dashboard_id", Type: DB_BigInt, Nullable: false},
  12. {Name: "panel_id", Type: DB_BigInt, Nullable: false},
  13. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  14. {Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
  15. {Name: "message", Type: DB_Text, Nullable: false},
  16. {Name: "state", Type: DB_NVarchar, Length: 190, Nullable: false},
  17. {Name: "settings", Type: DB_Text, Nullable: false},
  18. {Name: "frequency", Type: DB_BigInt, Nullable: false},
  19. {Name: "handler", Type: DB_BigInt, Nullable: false},
  20. {Name: "severity", Type: DB_Text, Nullable: false},
  21. {Name: "silenced", Type: DB_Bool, Nullable: false},
  22. {Name: "execution_error", Type: DB_Text, Nullable: false},
  23. {Name: "eval_data", Type: DB_Text, Nullable: true},
  24. {Name: "eval_date", Type: DB_DateTime, Nullable: true},
  25. {Name: "new_state_date", Type: DB_DateTime, Nullable: false},
  26. {Name: "state_changes", Type: DB_Int, Nullable: false},
  27. {Name: "created", Type: DB_DateTime, Nullable: false},
  28. {Name: "updated", Type: DB_DateTime, Nullable: false},
  29. },
  30. Indices: []*Index{
  31. {Cols: []string{"org_id", "id"}, Type: IndexType},
  32. {Cols: []string{"state"}, Type: IndexType},
  33. {Cols: []string{"dashboard_id"}, Type: IndexType},
  34. },
  35. }
  36. // create table
  37. mg.AddMigration("create alert table v1", NewAddTableMigration(alertV1))
  38. // create indices
  39. mg.AddMigration("add index alert org_id & id ", NewAddIndexMigration(alertV1, alertV1.Indices[0]))
  40. mg.AddMigration("add index alert state", NewAddIndexMigration(alertV1, alertV1.Indices[1]))
  41. mg.AddMigration("add index alert dashboard_id", NewAddIndexMigration(alertV1, alertV1.Indices[2]))
  42. alertRuleTagTable := Table{
  43. Name: "alert_rule_tag",
  44. Columns: []*Column{
  45. {Name: "alert_id", Type: DB_BigInt, Nullable: false},
  46. {Name: "tag_id", Type: DB_BigInt, Nullable: false},
  47. },
  48. Indices: []*Index{
  49. {Cols: []string{"alert_id", "tag_id"}, Type: UniqueIndex},
  50. },
  51. }
  52. mg.AddMigration("Create alert_rule_tag table v1", NewAddTableMigration(alertRuleTagTable))
  53. mg.AddMigration("Add unique index alert_rule_tag.alert_id_tag_id", NewAddIndexMigration(alertRuleTagTable, alertRuleTagTable.Indices[0]))
  54. alert_notification := Table{
  55. Name: "alert_notification",
  56. Columns: []*Column{
  57. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  58. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  59. {Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
  60. {Name: "type", Type: DB_NVarchar, Length: 255, Nullable: false},
  61. {Name: "settings", Type: DB_Text, Nullable: false},
  62. {Name: "created", Type: DB_DateTime, Nullable: false},
  63. {Name: "updated", Type: DB_DateTime, Nullable: false},
  64. },
  65. Indices: []*Index{
  66. {Cols: []string{"org_id", "name"}, Type: UniqueIndex},
  67. },
  68. }
  69. mg.AddMigration("create alert_notification table v1", NewAddTableMigration(alert_notification))
  70. mg.AddMigration("Add column is_default", NewAddColumnMigration(alert_notification, &Column{
  71. Name: "is_default", Type: DB_Bool, Nullable: false, Default: "0",
  72. }))
  73. mg.AddMigration("Add column frequency", NewAddColumnMigration(alert_notification, &Column{
  74. Name: "frequency", Type: DB_BigInt, Nullable: true,
  75. }))
  76. mg.AddMigration("Add column send_reminder", NewAddColumnMigration(alert_notification, &Column{
  77. Name: "send_reminder", Type: DB_Bool, Nullable: true, Default: "0",
  78. }))
  79. mg.AddMigration("Add column disable_resolve_message", NewAddColumnMigration(alert_notification, &Column{
  80. Name: "disable_resolve_message", Type: DB_Bool, Nullable: false, Default: "0",
  81. }))
  82. mg.AddMigration("add index alert_notification org_id & name", NewAddIndexMigration(alert_notification, alert_notification.Indices[0]))
  83. mg.AddMigration("Update alert table charset", NewTableCharsetMigration("alert", []*Column{
  84. {Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
  85. {Name: "message", Type: DB_Text, Nullable: false},
  86. {Name: "state", Type: DB_NVarchar, Length: 190, Nullable: false},
  87. {Name: "settings", Type: DB_Text, Nullable: false},
  88. {Name: "severity", Type: DB_Text, Nullable: false},
  89. {Name: "execution_error", Type: DB_Text, Nullable: false},
  90. {Name: "eval_data", Type: DB_Text, Nullable: true},
  91. }))
  92. mg.AddMigration("Update alert_notification table charset", NewTableCharsetMigration("alert_notification", []*Column{
  93. {Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
  94. {Name: "type", Type: DB_NVarchar, Length: 255, Nullable: false},
  95. {Name: "settings", Type: DB_Text, Nullable: false},
  96. }))
  97. notification_journal := Table{
  98. Name: "alert_notification_journal",
  99. Columns: []*Column{
  100. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  101. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  102. {Name: "alert_id", Type: DB_BigInt, Nullable: false},
  103. {Name: "notifier_id", Type: DB_BigInt, Nullable: false},
  104. {Name: "sent_at", Type: DB_BigInt, Nullable: false},
  105. {Name: "success", Type: DB_Bool, Nullable: false},
  106. },
  107. Indices: []*Index{
  108. {Cols: []string{"org_id", "alert_id", "notifier_id"}, Type: IndexType},
  109. },
  110. }
  111. mg.AddMigration("create notification_journal table v1", NewAddTableMigration(notification_journal))
  112. mg.AddMigration("add index notification_journal org_id & alert_id & notifier_id", NewAddIndexMigration(notification_journal, notification_journal.Indices[0]))
  113. mg.AddMigration("drop alert_notification_journal", NewDropTableMigration("alert_notification_journal"))
  114. alert_notification_state := Table{
  115. Name: "alert_notification_state",
  116. Columns: []*Column{
  117. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  118. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  119. {Name: "alert_id", Type: DB_BigInt, Nullable: false},
  120. {Name: "notifier_id", Type: DB_BigInt, Nullable: false},
  121. {Name: "state", Type: DB_NVarchar, Length: 50, Nullable: false},
  122. {Name: "version", Type: DB_BigInt, Nullable: false},
  123. {Name: "updated_at", Type: DB_BigInt, Nullable: false},
  124. {Name: "alert_rule_state_updated_version", Type: DB_BigInt, Nullable: false},
  125. },
  126. Indices: []*Index{
  127. {Cols: []string{"org_id", "alert_id", "notifier_id"}, Type: UniqueIndex},
  128. },
  129. }
  130. mg.AddMigration("create alert_notification_state table v1", NewAddTableMigration(alert_notification_state))
  131. mg.AddMigration("add index alert_notification_state org_id & alert_id & notifier_id",
  132. NewAddIndexMigration(alert_notification_state, alert_notification_state.Indices[0]))
  133. mg.AddMigration("Add for to alert table", NewAddColumnMigration(alertV1, &Column{
  134. Name: "for", Type: DB_BigInt, Nullable: true,
  135. }))
  136. mg.AddMigration("Add column uid in alert_notification", NewAddColumnMigration(alert_notification, &Column{
  137. Name: "uid", Type: DB_NVarchar, Length: 40, Nullable: true,
  138. }))
  139. mg.AddMigration("Update uid column values in alert_notification", new(RawSqlMigration).
  140. Sqlite("UPDATE alert_notification SET uid=printf('%09d',id) WHERE uid IS NULL;").
  141. Postgres("UPDATE alert_notification SET uid=lpad('' || id::text,9,'0') WHERE uid IS NULL;").
  142. Mysql("UPDATE alert_notification SET uid=lpad(id,9,'0') WHERE uid IS NULL;"))
  143. mg.AddMigration("Add unique index alert_notification_org_id_uid", NewAddIndexMigration(alert_notification, &Index{
  144. Cols: []string{"org_id", "uid"}, Type: UniqueIndex,
  145. }))
  146. mg.AddMigration("Remove unique index org_id_name", NewDropIndexMigration(alert_notification, &Index{
  147. Cols: []string{"org_id", "name"}, Type: UniqueIndex,
  148. }))
  149. }