2016-08-01 16:07:00 +08:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func addAnnotationMig(mg *Migrator) {
|
2016-09-08 17:25:45 +08:00
|
|
|
|
2016-08-01 16:07:00 +08:00
|
|
|
table := Table{
|
|
|
|
|
Name: "annotation",
|
|
|
|
|
Columns: []*Column{
|
|
|
|
|
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
|
|
|
|
{Name: "org_id", Type: DB_BigInt, Nullable: false},
|
|
|
|
|
{Name: "alert_id", Type: DB_BigInt, Nullable: true},
|
|
|
|
|
{Name: "user_id", Type: DB_BigInt, Nullable: true},
|
2016-09-09 17:30:55 +08:00
|
|
|
{Name: "dashboard_id", Type: DB_BigInt, Nullable: true},
|
|
|
|
|
{Name: "panel_id", Type: DB_BigInt, Nullable: true},
|
2016-09-13 22:24:00 +08:00
|
|
|
{Name: "category_id", Type: DB_BigInt, Nullable: true},
|
2016-08-01 16:07:00 +08:00
|
|
|
{Name: "type", Type: DB_NVarchar, Length: 25, Nullable: false},
|
|
|
|
|
{Name: "title", Type: DB_Text, Nullable: false},
|
|
|
|
|
{Name: "text", Type: DB_Text, Nullable: false},
|
|
|
|
|
{Name: "metric", Type: DB_NVarchar, Length: 255, Nullable: true},
|
|
|
|
|
{Name: "prev_state", Type: DB_NVarchar, Length: 25, Nullable: false},
|
|
|
|
|
{Name: "new_state", Type: DB_NVarchar, Length: 25, Nullable: false},
|
|
|
|
|
{Name: "data", Type: DB_Text, Nullable: false},
|
2016-09-08 17:25:45 +08:00
|
|
|
{Name: "epoch", Type: DB_BigInt, Nullable: false},
|
2016-08-01 16:07:00 +08:00
|
|
|
},
|
|
|
|
|
Indices: []*Index{
|
|
|
|
|
{Cols: []string{"org_id", "alert_id"}, Type: IndexType},
|
|
|
|
|
{Cols: []string{"org_id", "type"}, Type: IndexType},
|
2016-09-13 22:24:00 +08:00
|
|
|
{Cols: []string{"org_id", "category_id"}, Type: IndexType},
|
|
|
|
|
{Cols: []string{"org_id", "dashboard_id", "panel_id", "epoch"}, Type: IndexType},
|
|
|
|
|
{Cols: []string{"org_id", "epoch"}, Type: IndexType},
|
2016-08-01 16:07:00 +08:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-13 22:24:00 +08:00
|
|
|
mg.AddMigration("Drop old annotation table v4", NewDropTableMigration("annotation"))
|
|
|
|
|
mg.AddMigration("create annotation table v5", NewAddTableMigration(table))
|
2016-08-01 16:07:00 +08:00
|
|
|
|
|
|
|
|
// create indices
|
2016-09-13 22:24:00 +08:00
|
|
|
mg.AddMigration("add index annotation 0 v3", NewAddIndexMigration(table, table.Indices[0]))
|
|
|
|
|
mg.AddMigration("add index annotation 1 v3", NewAddIndexMigration(table, table.Indices[1]))
|
|
|
|
|
mg.AddMigration("add index annotation 2 v3", NewAddIndexMigration(table, table.Indices[2]))
|
|
|
|
|
mg.AddMigration("add index annotation 3 v3", NewAddIndexMigration(table, table.Indices[3]))
|
|
|
|
|
mg.AddMigration("add index annotation 4 v3", NewAddIndexMigration(table, table.Indices[4]))
|
2017-03-28 20:34:53 +08:00
|
|
|
|
|
|
|
|
mg.AddMigration("Update annotation table charset", NewTableCharsetMigration("annotation", []*Column{
|
|
|
|
|
{Name: "type", Type: DB_NVarchar, Length: 25, Nullable: false},
|
|
|
|
|
{Name: "title", Type: DB_Text, Nullable: false},
|
|
|
|
|
{Name: "text", Type: DB_Text, Nullable: false},
|
|
|
|
|
{Name: "metric", Type: DB_NVarchar, Length: 255, Nullable: true},
|
|
|
|
|
{Name: "prev_state", Type: DB_NVarchar, Length: 25, Nullable: false},
|
|
|
|
|
{Name: "new_state", Type: DB_NVarchar, Length: 25, Nullable: false},
|
|
|
|
|
{Name: "data", Type: DB_Text, Nullable: false},
|
|
|
|
|
}))
|
2017-04-12 22:26:34 +08:00
|
|
|
|
|
|
|
|
mg.AddMigration("Add column region_id to annotation table", NewAddColumnMigration(table, &Column{
|
|
|
|
|
Name: "region_id", Type: DB_BigInt, Nullable: true, Default: "0",
|
|
|
|
|
}))
|
2017-10-07 16:31:39 +08:00
|
|
|
|
|
|
|
|
categoryIdIndex := &Index{Cols: []string{"org_id", "category_id"}, Type: IndexType}
|
|
|
|
|
mg.AddMigration("Drop category_id index", NewDropIndexMigration(table, categoryIdIndex))
|
|
|
|
|
|
|
|
|
|
mg.AddMigration("Add column tags to annotation table", NewAddColumnMigration(table, &Column{
|
|
|
|
|
Name: "tags", Type: DB_NVarchar, Nullable: true, Length: 500,
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
///
|
|
|
|
|
/// Annotation tag
|
|
|
|
|
///
|
|
|
|
|
annotationTagTable := Table{
|
|
|
|
|
Name: "annotation_tag",
|
|
|
|
|
Columns: []*Column{
|
|
|
|
|
{Name: "annotation_id", Type: DB_BigInt, Nullable: false},
|
|
|
|
|
{Name: "tag_id", Type: DB_BigInt, Nullable: false},
|
|
|
|
|
},
|
|
|
|
|
Indices: []*Index{
|
|
|
|
|
{Cols: []string{"annotation_id", "tag_id"}, Type: UniqueIndex},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mg.AddMigration("Create annotation_tag table v2", NewAddTableMigration(annotationTagTable))
|
|
|
|
|
mg.AddMigration("Add unique index annotation_tag.annotation_id_tag_id", NewAddIndexMigration(annotationTagTable, annotationTagTable.Indices[0]))
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// clear alert text
|
|
|
|
|
//
|
|
|
|
|
updateTextFieldSql := "UPDATE annotation SET TEXT = '' WHERE alert_id > 0"
|
2018-05-10 22:54:21 +08:00
|
|
|
mg.AddMigration("Update alert annotations and set TEXT to empty", NewRawSqlMigration(updateTextFieldSql))
|
2018-03-22 09:22:58 +08:00
|
|
|
|
|
|
|
|
//
|
2018-03-22 22:52:09 +08:00
|
|
|
// Add a 'created' & 'updated' column
|
2018-03-22 09:22:58 +08:00
|
|
|
//
|
|
|
|
|
mg.AddMigration("Add created time to annotation table", NewAddColumnMigration(table, &Column{
|
|
|
|
|
Name: "created", Type: DB_BigInt, Nullable: true, Default: "0",
|
|
|
|
|
}))
|
2018-03-22 22:52:09 +08:00
|
|
|
mg.AddMigration("Add updated time to annotation table", NewAddColumnMigration(table, &Column{
|
|
|
|
|
Name: "updated", Type: DB_BigInt, Nullable: true, Default: "0",
|
|
|
|
|
}))
|
2018-03-22 09:22:58 +08:00
|
|
|
mg.AddMigration("Add index for created in annotation table", NewAddIndexMigration(table, &Index{
|
|
|
|
|
Cols: []string{"org_id", "created"}, Type: IndexType,
|
|
|
|
|
}))
|
2018-03-22 22:52:09 +08:00
|
|
|
mg.AddMigration("Add index for updated in annotation table", NewAddIndexMigration(table, &Index{
|
|
|
|
|
Cols: []string{"org_id", "updated"}, Type: IndexType,
|
|
|
|
|
}))
|
2018-03-22 23:21:47 +08:00
|
|
|
|
|
|
|
|
//
|
2018-09-21 17:51:26 +08:00
|
|
|
// Convert epoch saved as seconds to milliseconds
|
2018-03-22 23:21:47 +08:00
|
|
|
//
|
2018-04-05 15:51:08 +08:00
|
|
|
updateEpochSql := "UPDATE annotation SET epoch = (epoch*1000) where epoch < 9999999999"
|
2018-05-10 22:54:21 +08:00
|
|
|
mg.AddMigration("Convert existing annotations from seconds to milliseconds", NewRawSqlMigration(updateEpochSql))
|
2016-08-01 16:07:00 +08:00
|
|
|
}
|