apikey_mig.go 2.6 KB

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