forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
apikey_mig.go
81 lines (70 loc) · 2.93 KB
/
apikey_mig.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package migrations
import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
func addApiKeyMigrations(mg *Migrator) {
apiKeyV1 := Table{
Name: "api_key",
Columns: []*Column{
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "account_id", Type: DB_BigInt, Nullable: false},
{Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
{Name: "key", Type: DB_Varchar, Length: 64, Nullable: false},
{Name: "role", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "created", Type: DB_DateTime, Nullable: false},
{Name: "updated", Type: DB_DateTime, Nullable: false},
},
Indices: []*Index{
{Cols: []string{"account_id"}},
{Cols: []string{"key"}, Type: UniqueIndex},
{Cols: []string{"account_id", "name"}, Type: UniqueIndex},
},
}
// create table
mg.AddMigration("create api_key table", NewAddTableMigration(apiKeyV1))
// create indices
mg.AddMigration("add index api_key.account_id", NewAddIndexMigration(apiKeyV1, apiKeyV1.Indices[0]))
mg.AddMigration("add index api_key.key", NewAddIndexMigration(apiKeyV1, apiKeyV1.Indices[1]))
mg.AddMigration("add index api_key.account_id_name", NewAddIndexMigration(apiKeyV1, apiKeyV1.Indices[2]))
// ---------------------
// account -> org changes
// drop indexes
addDropAllIndicesMigrations(mg, "v1", apiKeyV1)
// rename table
addTableRenameMigration(mg, "api_key", "api_key_v1", "v1")
apiKeyV2 := Table{
Name: "api_key",
Columns: []*Column{
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "org_id", Type: DB_BigInt, Nullable: false},
{Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
{Name: "key", Type: DB_Varchar, Length: 190, Nullable: false},
{Name: "role", Type: DB_NVarchar, Length: 255, Nullable: false},
{Name: "created", Type: DB_DateTime, Nullable: false},
{Name: "updated", Type: DB_DateTime, Nullable: false},
},
Indices: []*Index{
{Cols: []string{"org_id"}},
{Cols: []string{"key"}, Type: UniqueIndex},
{Cols: []string{"org_id", "name"}, Type: UniqueIndex},
},
}
// create v2 table
mg.AddMigration("create api_key table v2", NewAddTableMigration(apiKeyV2))
// add v2 indíces
addTableIndicesMigrations(mg, "v2", apiKeyV2)
//------- copy data from v1 to v2 -------------------
mg.AddMigration("copy api_key v1 to v2", NewCopyTableDataMigration("api_key", "api_key_v1", map[string]string{
"id": "id",
"org_id": "account_id",
"name": "name",
"key": "key",
"role": "role",
"created": "created",
"updated": "updated",
}))
mg.AddMigration("Drop old table api_key_v1", NewDropTableMigration("api_key_v1"))
mg.AddMigration("Update api_key table charset", NewTableCharsetMigration("api_key", []*Column{
{Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
{Name: "key", Type: DB_Varchar, Length: 190, Nullable: false},
{Name: "role", Type: DB_NVarchar, Length: 255, Nullable: false},
}))
}