2015-02-24 04:46:34 -06:00
|
|
|
package migrations
|
|
|
|
|
2022-03-07 08:58:20 -06:00
|
|
|
import (
|
|
|
|
. "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
|
|
|
|
)
|
2015-02-24 04:46:34 -06:00
|
|
|
|
|
|
|
func addApiKeyMigrations(mg *Migrator) {
|
2015-02-24 10:59:21 -06:00
|
|
|
apiKeyV1 := Table{
|
|
|
|
Name: "api_key",
|
|
|
|
Columns: []*Column{
|
2015-03-07 09:23:22 -06:00
|
|
|
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
|
|
|
{Name: "account_id", Type: DB_BigInt, Nullable: false},
|
2017-03-28 07:34:53 -05:00
|
|
|
{Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
|
2015-03-07 09:23:22 -06:00
|
|
|
{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},
|
2015-02-24 10:59:21 -06:00
|
|
|
},
|
|
|
|
Indices: []*Index{
|
2015-03-07 09:23:22 -06:00
|
|
|
{Cols: []string{"account_id"}},
|
|
|
|
{Cols: []string{"key"}, Type: UniqueIndex},
|
|
|
|
{Cols: []string{"account_id", "name"}, Type: UniqueIndex},
|
2015-02-24 10:59:21 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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]))
|
2015-02-24 04:46:34 -06:00
|
|
|
|
|
|
|
// ---------------------
|
|
|
|
// account -> org changes
|
|
|
|
|
2015-02-24 10:59:21 -06:00
|
|
|
// drop indexes
|
|
|
|
addDropAllIndicesMigrations(mg, "v1", apiKeyV1)
|
|
|
|
// rename table
|
|
|
|
addTableRenameMigration(mg, "api_key", "api_key_v1", "v1")
|
|
|
|
|
|
|
|
apiKeyV2 := Table{
|
|
|
|
Name: "api_key",
|
|
|
|
Columns: []*Column{
|
2015-03-07 09:23:22 -06:00
|
|
|
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
|
|
|
|
{Name: "org_id", Type: DB_BigInt, Nullable: false},
|
2017-03-28 07:34:53 -05:00
|
|
|
{Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
|
|
|
|
{Name: "key", Type: DB_Varchar, Length: 190, Nullable: false},
|
2015-03-07 09:23:22 -06:00
|
|
|
{Name: "role", Type: DB_NVarchar, Length: 255, Nullable: false},
|
|
|
|
{Name: "created", Type: DB_DateTime, Nullable: false},
|
|
|
|
{Name: "updated", Type: DB_DateTime, Nullable: false},
|
2015-02-24 10:59:21 -06:00
|
|
|
},
|
|
|
|
Indices: []*Index{
|
2015-03-07 09:23:22 -06:00
|
|
|
{Cols: []string{"org_id"}},
|
|
|
|
{Cols: []string{"key"}, Type: UniqueIndex},
|
|
|
|
{Cols: []string{"org_id", "name"}, Type: UniqueIndex},
|
2015-02-24 10:59:21 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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"))
|
2017-03-28 07:34:53 -05:00
|
|
|
|
|
|
|
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},
|
|
|
|
}))
|
2019-06-26 01:47:03 -05:00
|
|
|
|
|
|
|
mg.AddMigration("Add expires to api_key table", NewAddColumnMigration(apiKeyV2, &Column{
|
|
|
|
Name: "expires", Type: DB_BigInt, Nullable: true,
|
|
|
|
}))
|
2021-10-20 07:36:11 -05:00
|
|
|
|
|
|
|
mg.AddMigration("Add service account foreign key", NewAddColumnMigration(apiKeyV2, &Column{
|
|
|
|
Name: "service_account_id", Type: DB_BigInt, Nullable: true,
|
|
|
|
}))
|
2022-03-07 08:58:20 -06:00
|
|
|
|
|
|
|
mg.AddMigration("set service account foreign key to nil if 0", NewRawSQLMigration(
|
|
|
|
"UPDATE api_key SET service_account_id = NULL WHERE service_account_id = 0;"))
|
2022-06-28 09:42:40 -05:00
|
|
|
|
|
|
|
mg.AddMigration("Add last_used_at to api_key table", NewAddColumnMigration(apiKeyV2, &Column{
|
|
|
|
Name: "last_used_at", Type: DB_DateTime, Nullable: true,
|
|
|
|
}))
|
2022-08-18 09:54:39 -05:00
|
|
|
|
|
|
|
// is_revoked indicates whether key is revoked or not. Revoked keys should be kept in the table, but invalid.
|
|
|
|
mg.AddMigration("Add is_revoked column to api_key table", NewAddColumnMigration(apiKeyV2, &Column{
|
|
|
|
Name: "is_revoked", Type: DB_Bool, Nullable: true, Default: "0",
|
|
|
|
}))
|
2015-02-24 04:46:34 -06:00
|
|
|
}
|