1package migrations
2
3import (
4	"github.com/grafana/grafana/pkg/models"
5	"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
6)
7
8// addLibraryElementsMigrations defines database migrations for library elements.
9func addLibraryElementsMigrations(mg *migrator.Migrator) {
10	libraryElementsV1 := migrator.Table{
11		Name: "library_element",
12		Columns: []*migrator.Column{
13			{Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
14			{Name: "org_id", Type: migrator.DB_BigInt, Nullable: false},
15			{Name: "folder_id", Type: migrator.DB_BigInt, Nullable: false},
16			{Name: "uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false},
17			{Name: "name", Type: migrator.DB_NVarchar, Length: 150, Nullable: false},
18			{Name: "kind", Type: migrator.DB_BigInt, Nullable: false},
19			{Name: "type", Type: migrator.DB_NVarchar, Length: 40, Nullable: false},
20			{Name: "description", Type: migrator.DB_NVarchar, Length: 255, Nullable: false},
21			{Name: "model", Type: migrator.DB_Text, Nullable: false},
22			{Name: "created", Type: migrator.DB_DateTime, Nullable: false},
23			{Name: "created_by", Type: migrator.DB_BigInt, Nullable: false},
24			{Name: "updated", Type: migrator.DB_DateTime, Nullable: false},
25			{Name: "updated_by", Type: migrator.DB_BigInt, Nullable: false},
26			{Name: "version", Type: migrator.DB_BigInt, Nullable: false},
27		},
28		Indices: []*migrator.Index{
29			{Cols: []string{"org_id", "folder_id", "name", "kind"}, Type: migrator.UniqueIndex},
30		},
31	}
32
33	mg.AddMigration("create library_element table v1", migrator.NewAddTableMigration(libraryElementsV1))
34	mg.AddMigration("add index library_element org_id-folder_id-name-kind", migrator.NewAddIndexMigration(libraryElementsV1, libraryElementsV1.Indices[0]))
35
36	libraryElementConnectionV1 := migrator.Table{
37		Name: models.LibraryElementConnectionTableName,
38		Columns: []*migrator.Column{
39			{Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
40			{Name: "element_id", Type: migrator.DB_BigInt, Nullable: false},
41			{Name: "kind", Type: migrator.DB_BigInt, Nullable: false},
42			{Name: "connection_id", Type: migrator.DB_BigInt, Nullable: false},
43			{Name: "created", Type: migrator.DB_DateTime, Nullable: false},
44			{Name: "created_by", Type: migrator.DB_BigInt, Nullable: false},
45		},
46		Indices: []*migrator.Index{
47			{Cols: []string{"element_id", "kind", "connection_id"}, Type: migrator.UniqueIndex},
48		},
49	}
50
51	mg.AddMigration("create "+models.LibraryElementConnectionTableName+" table v1", migrator.NewAddTableMigration(libraryElementConnectionV1))
52	mg.AddMigration("add index "+models.LibraryElementConnectionTableName+" element_id-kind-connection_id", migrator.NewAddIndexMigration(libraryElementConnectionV1, libraryElementConnectionV1.Indices[0]))
53
54	mg.AddMigration("add unique index library_element org_id_uid", migrator.NewAddIndexMigration(libraryElementsV1, &migrator.Index{
55		Cols: []string{"org_id", "uid"}, Type: migrator.UniqueIndex,
56	}))
57}
58