apikey_mig.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package migrations
  2. import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
  3. func addApiKeyMigrations(mg *Migrator) {
  4. mg.AddMigration("create api_key table", new(AddTableMigration).
  5. Name("api_key").WithColumns(
  6. &Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  7. &Column{Name: "account_id", Type: DB_BigInt, Nullable: false},
  8. &Column{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
  9. &Column{Name: "key", Type: DB_Varchar, Length: 64, Nullable: false},
  10. &Column{Name: "role", Type: DB_NVarchar, Length: 255, Nullable: false},
  11. &Column{Name: "created", Type: DB_DateTime, Nullable: false},
  12. &Column{Name: "updated", Type: DB_DateTime, Nullable: false},
  13. ))
  14. //------- indexes ------------------
  15. mg.AddMigration("add index api_key.account_id", new(AddIndexMigration).
  16. Table("api_key").Columns("account_id"))
  17. mg.AddMigration("add index api_key.key", new(AddIndexMigration).
  18. Table("api_key").Columns("key").Unique())
  19. mg.AddMigration("add index api_key.account_id_name", new(AddIndexMigration).
  20. Table("api_key").Columns("account_id", "name").Unique())
  21. // ---------------------
  22. // account -> org changes
  23. //------- drop indexes ------------------
  24. mg.AddMigration("drop index api_key.account_id", new(DropIndexMigration).
  25. Table("api_key").Columns("account_id"))
  26. mg.AddMigration("drop index api_key.key", new(DropIndexMigration).
  27. Table("api_key").Columns("key").Unique())
  28. mg.AddMigration("drop index api_key.account_id_name", new(DropIndexMigration).
  29. Table("api_key").Columns("account_id", "name").Unique())
  30. //------- rename table ------------------
  31. mg.AddMigration("rename table api_key to api_key_old", new(RenameTableMigration).
  32. Rename("api_key", "api_key_old"))
  33. //------- recreate table with new column names ------------------
  34. mg.AddMigration("create api_key table v2", new(AddTableMigration).
  35. Name("api_key").WithColumns(
  36. &Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  37. &Column{Name: "org_id", Type: DB_BigInt, Nullable: false},
  38. &Column{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: false},
  39. &Column{Name: "key", Type: DB_Varchar, Length: 64, Nullable: false},
  40. &Column{Name: "role", Type: DB_NVarchar, Length: 255, Nullable: false},
  41. &Column{Name: "created", Type: DB_DateTime, Nullable: false},
  42. &Column{Name: "updated", Type: DB_DateTime, Nullable: false},
  43. ))
  44. //------- recreate indexes ------------------
  45. mg.AddMigration("add index api_key.org_id", new(AddIndexMigration).
  46. Table("api_key").Columns("org_id"))
  47. mg.AddMigration("add index api_key.key v2", new(AddIndexMigration).
  48. Table("api_key").Columns("key").Unique())
  49. mg.AddMigration("add index api_key.org_id_name", new(AddIndexMigration).
  50. Table("api_key").Columns("org_id", "name").Unique())
  51. //------- copy data from old api_key_old -------------------
  52. mg.AddMigration("copy data from old api_key table", new(CopyTableDataMigration).
  53. Source("api_key_old", "id, account_id, name, key, role, created, updated").
  54. Target("api_key", "id, org_id, name, key, role, created, updated"))
  55. mg.AddMigration("Drop old table api_key_old", new(DropTableMigration).Table("api_key_old"))
  56. }