2016-08-01 03:07:00 -05:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
2020-04-01 08:57:21 -05:00
|
|
|
"xorm.io/xorm"
|
2023-01-30 02:21:27 -06:00
|
|
|
|
|
|
|
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
2016-08-01 03:07:00 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func addAnnotationMig(mg *Migrator) {
|
|
|
|
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 04:30:55 -05:00
|
|
|
{Name: "dashboard_id", Type: DB_BigInt, Nullable: true},
|
|
|
|
{Name: "panel_id", Type: DB_BigInt, Nullable: true},
|
2016-09-13 09:24:00 -05:00
|
|
|
{Name: "category_id", Type: DB_BigInt, Nullable: true},
|
2016-08-01 03:07:00 -05: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 04:25:45 -05:00
|
|
|
{Name: "epoch", Type: DB_BigInt, Nullable: false},
|
2016-08-01 03:07:00 -05:00
|
|
|
},
|
|
|
|
Indices: []*Index{
|
|
|
|
{Cols: []string{"org_id", "alert_id"}, Type: IndexType},
|
|
|
|
{Cols: []string{"org_id", "type"}, Type: IndexType},
|
2016-09-13 09:24:00 -05: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 03:07:00 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-09-13 09:24:00 -05:00
|
|
|
mg.AddMigration("Drop old annotation table v4", NewDropTableMigration("annotation"))
|
|
|
|
mg.AddMigration("create annotation table v5", NewAddTableMigration(table))
|
2016-08-01 03:07:00 -05:00
|
|
|
|
|
|
|
// create indices
|
2016-09-13 09:24:00 -05: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 07:34:53 -05: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 09:26:34 -05: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 03:31:39 -05: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,
|
|
|
|
}))
|
|
|
|
|
2020-09-22 09:22:19 -05:00
|
|
|
//
|
|
|
|
// Annotation tag
|
|
|
|
//
|
2017-10-07 03:31:39 -05:00
|
|
|
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]))
|
|
|
|
|
2020-11-17 10:58:56 -06:00
|
|
|
// drop dashboard indexes
|
|
|
|
addDropAllIndicesMigrations(mg, "v2", annotationTagTable)
|
|
|
|
// rename table
|
|
|
|
addTableRenameMigration(mg, "annotation_tag", "annotation_tag_v2", "v2")
|
|
|
|
|
|
|
|
// annotation_tag v3
|
|
|
|
annotationTagTableV3 := Table{
|
|
|
|
Name: "annotation_tag",
|
|
|
|
Columns: []*Column{
|
|
|
|
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
|
|
|
{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},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// recreate table
|
|
|
|
mg.AddMigration("Create annotation_tag table v3", NewAddTableMigration(annotationTagTableV3))
|
|
|
|
// recreate indices
|
|
|
|
addTableIndicesMigrations(mg, "Add unique index annotation_tag.annotation_id_tag_id V3", annotationTagTableV3)
|
|
|
|
// copy data
|
|
|
|
mg.AddMigration("copy annotation_tag v2 to v3", NewCopyTableDataMigration("annotation_tag", "annotation_tag_v2", map[string]string{
|
|
|
|
"annotation_id": "annotation_id",
|
|
|
|
"tag_id": "tag_id",
|
|
|
|
}))
|
|
|
|
|
|
|
|
mg.AddMigration("drop table annotation_tag_v2", NewDropTableMigration("annotation_tag_v2"))
|
|
|
|
|
2017-10-07 03:31:39 -05:00
|
|
|
//
|
|
|
|
// clear alert text
|
|
|
|
//
|
2020-11-10 23:21:08 -06:00
|
|
|
updateTextFieldSQL := "UPDATE annotation SET TEXT = '' WHERE alert_id > 0"
|
|
|
|
mg.AddMigration("Update alert annotations and set TEXT to empty", NewRawSQLMigration(updateTextFieldSQL))
|
2018-03-21 20:22:58 -05:00
|
|
|
|
|
|
|
//
|
2018-03-22 09:52:09 -05:00
|
|
|
// Add a 'created' & 'updated' column
|
2018-03-21 20:22:58 -05:00
|
|
|
//
|
|
|
|
mg.AddMigration("Add created time to annotation table", NewAddColumnMigration(table, &Column{
|
|
|
|
Name: "created", Type: DB_BigInt, Nullable: true, Default: "0",
|
|
|
|
}))
|
2018-03-22 09:52:09 -05:00
|
|
|
mg.AddMigration("Add updated time to annotation table", NewAddColumnMigration(table, &Column{
|
|
|
|
Name: "updated", Type: DB_BigInt, Nullable: true, Default: "0",
|
|
|
|
}))
|
2018-03-21 20:22:58 -05:00
|
|
|
mg.AddMigration("Add index for created in annotation table", NewAddIndexMigration(table, &Index{
|
|
|
|
Cols: []string{"org_id", "created"}, Type: IndexType,
|
|
|
|
}))
|
2018-03-22 09:52:09 -05:00
|
|
|
mg.AddMigration("Add index for updated in annotation table", NewAddIndexMigration(table, &Index{
|
|
|
|
Cols: []string{"org_id", "updated"}, Type: IndexType,
|
|
|
|
}))
|
2018-03-22 10:21:47 -05:00
|
|
|
|
|
|
|
//
|
2018-09-21 04:51:26 -05:00
|
|
|
// Convert epoch saved as seconds to milliseconds
|
2018-03-22 10:21:47 -05:00
|
|
|
//
|
2020-11-10 23:21:08 -06:00
|
|
|
updateEpochSQL := "UPDATE annotation SET epoch = (epoch*1000) where epoch < 9999999999"
|
|
|
|
mg.AddMigration("Convert existing annotations from seconds to milliseconds", NewRawSQLMigration(updateEpochSQL))
|
2019-08-16 03:49:30 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// 6.4: Make Regions a single annotation row
|
|
|
|
//
|
|
|
|
mg.AddMigration("Add epoch_end column", NewAddColumnMigration(table, &Column{
|
|
|
|
Name: "epoch_end", Type: DB_BigInt, Nullable: false, Default: "0",
|
|
|
|
}))
|
|
|
|
mg.AddMigration("Add index for epoch_end", NewAddIndexMigration(table, &Index{
|
|
|
|
Cols: []string{"org_id", "epoch", "epoch_end"}, Type: IndexType,
|
|
|
|
}))
|
2020-11-10 23:21:08 -06:00
|
|
|
mg.AddMigration("Make epoch_end the same as epoch", NewRawSQLMigration("UPDATE annotation SET epoch_end = epoch"))
|
2019-08-16 03:49:30 -05:00
|
|
|
mg.AddMigration("Move region to single row", &AddMakeRegionSingleRowMigration{})
|
|
|
|
|
2020-02-05 09:52:41 -06:00
|
|
|
//
|
|
|
|
// 6.6.1: Optimize annotation queries
|
|
|
|
//
|
|
|
|
mg.AddMigration("Remove index org_id_epoch from annotation table", NewDropIndexMigration(table, &Index{
|
|
|
|
Cols: []string{"org_id", "epoch"}, Type: IndexType,
|
|
|
|
}))
|
|
|
|
|
|
|
|
mg.AddMigration("Remove index org_id_dashboard_id_panel_id_epoch from annotation table", NewDropIndexMigration(table, &Index{
|
|
|
|
Cols: []string{"org_id", "dashboard_id", "panel_id", "epoch"}, Type: IndexType,
|
|
|
|
}))
|
|
|
|
|
|
|
|
mg.AddMigration("Add index for org_id_dashboard_id_epoch_end_epoch on annotation table", NewAddIndexMigration(table, &Index{
|
|
|
|
Cols: []string{"org_id", "dashboard_id", "epoch_end", "epoch"}, Type: IndexType,
|
|
|
|
}))
|
|
|
|
|
|
|
|
mg.AddMigration("Add index for org_id_epoch_end_epoch on annotation table", NewAddIndexMigration(table, &Index{
|
|
|
|
Cols: []string{"org_id", "epoch_end", "epoch"}, Type: IndexType,
|
|
|
|
}))
|
|
|
|
|
|
|
|
mg.AddMigration("Remove index org_id_epoch_epoch_end from annotation table", NewDropIndexMigration(table, &Index{
|
|
|
|
Cols: []string{"org_id", "epoch", "epoch_end"}, Type: IndexType,
|
|
|
|
}))
|
2020-03-18 11:59:42 -05:00
|
|
|
|
|
|
|
mg.AddMigration("Add index for alert_id on annotation table", NewAddIndexMigration(table, &Index{
|
|
|
|
Cols: []string{"alert_id"}, Type: IndexType,
|
|
|
|
}))
|
2022-09-23 05:04:41 -05:00
|
|
|
|
|
|
|
mg.AddMigration("Increase tags column to length 4096", NewRawSQLMigration("").
|
|
|
|
Postgres("ALTER TABLE annotation ALTER COLUMN tags TYPE VARCHAR(4096);").
|
|
|
|
Mysql("ALTER TABLE annotation MODIFY tags VARCHAR(4096);"))
|
2019-08-16 03:49:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type AddMakeRegionSingleRowMigration struct {
|
|
|
|
MigrationBase
|
|
|
|
}
|
|
|
|
|
2020-11-10 23:21:08 -06:00
|
|
|
func (m *AddMakeRegionSingleRowMigration) SQL(dialect Dialect) string {
|
2019-08-16 03:49:30 -05:00
|
|
|
return "code migration"
|
|
|
|
}
|
|
|
|
|
|
|
|
type TempRegionInfoDTO struct {
|
|
|
|
RegionId int64
|
|
|
|
Epoch int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddMakeRegionSingleRowMigration) Exec(sess *xorm.Session, mg *Migrator) error {
|
|
|
|
regions := make([]*TempRegionInfoDTO, 0)
|
|
|
|
|
|
|
|
err := sess.SQL("SELECT region_id, epoch FROM annotation WHERE region_id>0 AND region_id <> id").Find(®ions)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, region := range regions {
|
|
|
|
_, err := sess.Exec("UPDATE annotation SET epoch_end = ? WHERE id = ?", region.Epoch, region.RegionId)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-22 07:08:18 -05:00
|
|
|
_, err = sess.Exec("DELETE FROM annotation WHERE region_id > 0 AND id <> region_id")
|
|
|
|
return err
|
2016-08-01 03:07:00 -05:00
|
|
|
}
|