|
|
@ -3,8 +3,9 @@ package migrations |
|
|
|
import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator" |
|
|
|
import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator" |
|
|
|
|
|
|
|
|
|
|
|
func addDashboardMigration(mg *Migrator) { |
|
|
|
func addDashboardMigration(mg *Migrator) { |
|
|
|
mg.AddMigration("create dashboard table", new(AddTableMigration). |
|
|
|
var dashboardV1 = Table{ |
|
|
|
Name("dashboard").WithColumns( |
|
|
|
Name: "dashboard", |
|
|
|
|
|
|
|
Columns: []*Column{ |
|
|
|
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, |
|
|
|
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, |
|
|
|
&Column{Name: "version", Type: DB_Int, Nullable: false}, |
|
|
|
&Column{Name: "version", Type: DB_Int, Nullable: false}, |
|
|
|
&Column{Name: "slug", Type: DB_NVarchar, Length: 255, Nullable: false}, |
|
|
|
&Column{Name: "slug", Type: DB_NVarchar, Length: 255, Nullable: false}, |
|
|
@ -13,45 +14,46 @@ func addDashboardMigration(mg *Migrator) { |
|
|
|
&Column{Name: "account_id", Type: DB_BigInt, Nullable: false}, |
|
|
|
&Column{Name: "account_id", Type: DB_BigInt, Nullable: false}, |
|
|
|
&Column{Name: "created", Type: DB_DateTime, Nullable: false}, |
|
|
|
&Column{Name: "created", Type: DB_DateTime, Nullable: false}, |
|
|
|
&Column{Name: "updated", Type: DB_DateTime, Nullable: false}, |
|
|
|
&Column{Name: "updated", Type: DB_DateTime, Nullable: false}, |
|
|
|
)) |
|
|
|
}, |
|
|
|
|
|
|
|
Indices: []*Index{ |
|
|
|
|
|
|
|
&Index{Cols: []string{"account_id"}}, |
|
|
|
|
|
|
|
&Index{Cols: []string{"account_id", "slug"}, Type: UniqueIndex}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mg.AddMigration("create dashboard_tag table", new(AddTableMigration). |
|
|
|
mg.AddMigration("create dashboard table", NewAddTableMigration(dashboardV1)) |
|
|
|
Name("dashboard_tag").WithColumns( |
|
|
|
|
|
|
|
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, |
|
|
|
|
|
|
|
&Column{Name: "dashboard_id", Type: DB_BigInt, Nullable: false}, |
|
|
|
|
|
|
|
&Column{Name: "term", Type: DB_NVarchar, Length: 50, Nullable: false}, |
|
|
|
|
|
|
|
)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//------- indexes ------------------
|
|
|
|
//------- indexes ------------------
|
|
|
|
mg.AddMigration("add index dashboard.account_id", new(AddIndexMigration). |
|
|
|
mg.AddMigration("add index dashboard.account_id", NewAddIndexMigration(dashboardV1, dashboardV1.Indices[0])) |
|
|
|
Table("dashboard").Columns("account_id")) |
|
|
|
mg.AddMigration("add unique index dashboard_account_id_slug", NewAddIndexMigration(dashboardV1, dashboardV1.Indices[1])) |
|
|
|
|
|
|
|
|
|
|
|
mg.AddMigration("add unique index dashboard_account_id_slug", new(AddIndexMigration). |
|
|
|
dashboardTagV1 := Table{ |
|
|
|
Table("dashboard").Columns("account_id", "slug").Unique()) |
|
|
|
Name: "dashboard_tag", |
|
|
|
|
|
|
|
Columns: []*Column{ |
|
|
|
|
|
|
|
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, |
|
|
|
|
|
|
|
&Column{Name: "dashboard_id", Type: DB_BigInt, Nullable: false}, |
|
|
|
|
|
|
|
&Column{Name: "term", Type: DB_NVarchar, Length: 50, Nullable: false}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
Indices: []*Index{ |
|
|
|
|
|
|
|
&Index{Cols: []string{"dashboard_id", "term"}, Type: UniqueIndex}, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
mg.AddMigration("add unique index dashboard_tag.dasboard_id_term", new(AddIndexMigration). |
|
|
|
mg.AddMigration("create dashboard_tag table", NewAddTableMigration(dashboardTagV1)) |
|
|
|
Table("dashboard_tag").Columns("dashboard_id", "term").Unique()) |
|
|
|
mg.AddMigration("add unique index dashboard_tag.dasboard_id_term", NewAddIndexMigration(dashboardTagV1, dashboardTagV1.Indices[0])) |
|
|
|
|
|
|
|
|
|
|
|
// ---------------------
|
|
|
|
// ---------------------
|
|
|
|
// account -> org changes
|
|
|
|
// account -> org changes
|
|
|
|
|
|
|
|
|
|
|
|
//------- drop indexes ------------------
|
|
|
|
//------- drop dashboard indexes ------------------
|
|
|
|
mg.AddMigration("drop index dashboard.account_id", new(DropIndexMigration). |
|
|
|
addDropAllIndicesMigrations(mg, "v1", dashboardTagV1) |
|
|
|
Table("dashboard").Columns("account_id")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mg.AddMigration("drop unique index dashboard_account_id_slug", new(DropIndexMigration). |
|
|
|
|
|
|
|
Table("dashboard").Columns("account_id", "slug").Unique()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mg.AddMigration("drop unique index dashboard_tag.dasboard_id_term", new(DropIndexMigration). |
|
|
|
|
|
|
|
Table("dashboard_tag").Columns("dashboard_id", "term").Unique()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//------- rename table ------------------
|
|
|
|
//------- rename table ------------------
|
|
|
|
mg.AddMigration("rename table dashboard to dashboard_old", new(RenameTableMigration). |
|
|
|
addTableRenameMigration(mg, "dashboard", "dashboard_v1", "v1") |
|
|
|
Rename("dashboard", "dashboard_old")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//------- recreate table with new column names ------------------
|
|
|
|
// dashboard v2
|
|
|
|
mg.AddMigration("create dashboard table v2", new(AddTableMigration). |
|
|
|
var dashboardV2 = Table{ |
|
|
|
Name("dashboard").WithColumns( |
|
|
|
Name: "dashboard", |
|
|
|
|
|
|
|
Columns: []*Column{ |
|
|
|
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, |
|
|
|
&Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true}, |
|
|
|
&Column{Name: "version", Type: DB_Int, Nullable: false}, |
|
|
|
&Column{Name: "version", Type: DB_Int, Nullable: false}, |
|
|
|
&Column{Name: "slug", Type: DB_NVarchar, Length: 255, Nullable: false}, |
|
|
|
&Column{Name: "slug", Type: DB_NVarchar, Length: 255, Nullable: false}, |
|
|
@ -60,22 +62,28 @@ func addDashboardMigration(mg *Migrator) { |
|
|
|
&Column{Name: "org_id", Type: DB_BigInt, Nullable: false}, |
|
|
|
&Column{Name: "org_id", Type: DB_BigInt, Nullable: false}, |
|
|
|
&Column{Name: "created", Type: DB_DateTime, Nullable: false}, |
|
|
|
&Column{Name: "created", Type: DB_DateTime, Nullable: false}, |
|
|
|
&Column{Name: "updated", Type: DB_DateTime, Nullable: false}, |
|
|
|
&Column{Name: "updated", Type: DB_DateTime, Nullable: false}, |
|
|
|
)) |
|
|
|
}, |
|
|
|
|
|
|
|
Indices: []*Index{ |
|
|
|
//------- dashboard table indexes ------------------
|
|
|
|
&Index{Cols: []string{"org_id"}}, |
|
|
|
mg.AddMigration("add index dashboard.org_id", new(AddIndexMigration). |
|
|
|
&Index{Cols: []string{"org_id", "slug"}, Type: UniqueIndex}, |
|
|
|
Table("dashboard").Columns("org_id")) |
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
mg.AddMigration("add unique index dashboard_org_id_slug", new(AddIndexMigration). |
|
|
|
|
|
|
|
Table("dashboard").Columns("org_id", "slug").Unique()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mg.AddMigration("add unique index dashboard_tag.dasboard_id_term v2", new(AddIndexMigration). |
|
|
|
|
|
|
|
Table("dashboard_tag").Columns("dashboard_id", "term").Unique()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//------- copy data from table -------------------
|
|
|
|
|
|
|
|
mg.AddMigration("copy data from dashboard_old table", new(CopyTableDataMigration). |
|
|
|
|
|
|
|
Source("dashboard_old", "id, version, slug, title, data, account_id, created, updated"). |
|
|
|
|
|
|
|
Target("dashboard", "id, version, slug, title, data, org_id, created, updated")) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mg.AddMigration("Drop old table dashboard_old", new(DropTableMigration).Table("dashboard_old")) |
|
|
|
// recreate table
|
|
|
|
|
|
|
|
mg.AddMigration("create dashboard v2", NewAddTableMigration(dashboardV2)) |
|
|
|
|
|
|
|
// recreate indices
|
|
|
|
|
|
|
|
addTableIndicesMigrations(mg, "v2", dashboardV2) |
|
|
|
|
|
|
|
// copy data
|
|
|
|
|
|
|
|
mg.AddMigration("copy dashboard v1 to v2", NewCopyTableDataMigration("dashboard", "dashboard_v1", map[string]string{ |
|
|
|
|
|
|
|
"id": "id", |
|
|
|
|
|
|
|
"version": "version", |
|
|
|
|
|
|
|
"slug": "slug", |
|
|
|
|
|
|
|
"title": "title", |
|
|
|
|
|
|
|
"data": "data", |
|
|
|
|
|
|
|
"org_id": "account_id", |
|
|
|
|
|
|
|
"created": "created", |
|
|
|
|
|
|
|
"updated": "updated", |
|
|
|
|
|
|
|
})) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mg.AddMigration("drop table dashboard_v1", NewDropTableMigration("dashboard_v1")) |
|
|
|
} |
|
|
|
} |
|
|
|