alert_mig.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 index alert_notification org_id & name", NewAddIndexMigration(alert_notification, alert_notification.Indices[0]))
  68. notification_journal := Table{
  69. Name: "alert_notification_journal",
  70. Columns: []*Column{
  71. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  72. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  73. {Name: "alert_id", Type: DB_BigInt, Nullable: false},
  74. {Name: "notifier_id", Type: DB_BigInt, Nullable: false},
  75. {Name: "sent_at", Type: DB_DateTime, Nullable: false},
  76. {Name: "success", Type: DB_Bool, Nullable: false},
  77. },
  78. Indices: []*Index{
  79. {Cols: []string{"org_id", "alert_id", "notifier_id"}, Type: IndexType},
  80. },
  81. }
  82. mg.AddMigration("create notification_journal table v1", NewAddTableMigration(notification_journal))
  83. mg.AddMigration("add index notification_journal org_id & alert_id & notifier_id", NewAddIndexMigration(notification_journal, notification_journal.Indices[0]))
  84. mg.AddMigration("Update alert table charset", NewTableCharsetMigration("alert", []*Column{
  85. {Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
  86. {Name: "message", Type: DB_Text, Nullable: false},
  87. {Name: "state", Type: DB_NVarchar, Length: 190, Nullable: false},
  88. {Name: "settings", Type: DB_Text, Nullable: false},
  89. {Name: "severity", Type: DB_Text, Nullable: false},
  90. {Name: "execution_error", Type: DB_Text, Nullable: false},
  91. {Name: "eval_data", Type: DB_Text, Nullable: true},
  92. }))
  93. mg.AddMigration("Update alert_notification table charset", NewTableCharsetMigration("alert_notification", []*Column{
  94. {Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
  95. {Name: "type", Type: DB_NVarchar, Length: 255, Nullable: false},
  96. {Name: "settings", Type: DB_Text, Nullable: false},
  97. }))
  98. }