alert_mig.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. alert_notification := Table{
  43. Name: "alert_notification",
  44. Columns: []*Column{
  45. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  46. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  47. {Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
  48. {Name: "type", Type: DB_NVarchar, Length: 255, Nullable: false},
  49. {Name: "settings", Type: DB_Text, Nullable: false},
  50. {Name: "created", Type: DB_DateTime, Nullable: false},
  51. {Name: "updated", Type: DB_DateTime, Nullable: false},
  52. },
  53. Indices: []*Index{
  54. {Cols: []string{"org_id", "name"}, Type: UniqueIndex},
  55. },
  56. }
  57. mg.AddMigration("create alert_notification table v1", NewAddTableMigration(alert_notification))
  58. mg.AddMigration("Add column is_default", NewAddColumnMigration(alert_notification, &Column{
  59. Name: "is_default", Type: DB_Bool, Nullable: false, Default: "0",
  60. }))
  61. mg.AddMigration("Add column frequency", NewAddColumnMigration(alert_notification, &Column{
  62. Name: "frequency", Type: DB_BigInt, Nullable: true,
  63. }))
  64. mg.AddMigration("Add column send_reminder", NewAddColumnMigration(alert_notification, &Column{
  65. Name: "send_reminder", Type: DB_Bool, Nullable: true, Default: "0",
  66. }))
  67. mg.AddMigration("Add column disable_resolve_message", NewAddColumnMigration(alert_notification, &Column{
  68. Name: "disable_resolve_message", Type: DB_Bool, Nullable: false, Default: "0",
  69. }))
  70. mg.AddMigration("add index alert_notification org_id & name", NewAddIndexMigration(alert_notification, alert_notification.Indices[0]))
  71. mg.AddMigration("Update alert table charset", NewTableCharsetMigration("alert", []*Column{
  72. {Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
  73. {Name: "message", Type: DB_Text, Nullable: false},
  74. {Name: "state", Type: DB_NVarchar, Length: 190, Nullable: false},
  75. {Name: "settings", Type: DB_Text, Nullable: false},
  76. {Name: "severity", Type: DB_Text, Nullable: false},
  77. {Name: "execution_error", Type: DB_Text, Nullable: false},
  78. {Name: "eval_data", Type: DB_Text, Nullable: true},
  79. }))
  80. mg.AddMigration("Update alert_notification table charset", NewTableCharsetMigration("alert_notification", []*Column{
  81. {Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
  82. {Name: "type", Type: DB_NVarchar, Length: 255, Nullable: false},
  83. {Name: "settings", Type: DB_Text, Nullable: false},
  84. }))
  85. notification_journal := Table{
  86. Name: "alert_notification_journal",
  87. Columns: []*Column{
  88. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  89. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  90. {Name: "alert_id", Type: DB_BigInt, Nullable: false},
  91. {Name: "notifier_id", Type: DB_BigInt, Nullable: false},
  92. {Name: "sent_at", Type: DB_BigInt, Nullable: false},
  93. {Name: "success", Type: DB_Bool, Nullable: false},
  94. },
  95. Indices: []*Index{
  96. {Cols: []string{"org_id", "alert_id", "notifier_id"}, Type: IndexType},
  97. },
  98. }
  99. mg.AddMigration("create notification_journal table v1", NewAddTableMigration(notification_journal))
  100. mg.AddMigration("add index notification_journal org_id & alert_id & notifier_id", NewAddIndexMigration(notification_journal, notification_journal.Indices[0]))
  101. mg.AddMigration("drop alert_notification_journal", NewDropTableMigration("alert_notification_journal"))
  102. alert_notification_state := Table{
  103. Name: "alert_notification_state",
  104. Columns: []*Column{
  105. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  106. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  107. {Name: "alert_id", Type: DB_BigInt, Nullable: false},
  108. {Name: "notifier_id", Type: DB_BigInt, Nullable: false},
  109. {Name: "state", Type: DB_NVarchar, Length: 50, Nullable: false},
  110. {Name: "version", Type: DB_BigInt, Nullable: false},
  111. {Name: "updated_at", Type: DB_BigInt, Nullable: false},
  112. {Name: "alert_rule_state_updated_version", Type: DB_BigInt, Nullable: false},
  113. },
  114. Indices: []*Index{
  115. {Cols: []string{"org_id", "alert_id", "notifier_id"}, Type: UniqueIndex},
  116. },
  117. }
  118. mg.AddMigration("create alert_notification_state table v1", NewAddTableMigration(alert_notification_state))
  119. mg.AddMigration("add index alert_notification_state org_id & alert_id & notifier_id",
  120. NewAddIndexMigration(alert_notification_state, alert_notification_state.Indices[0]))
  121. }