user_mig.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package migrations
  2. import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
  3. func addUserMigrations(mg *Migrator) {
  4. userV1 := Table{
  5. Name: "user",
  6. Columns: []*Column{
  7. &Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  8. &Column{Name: "version", Type: DB_Int, Nullable: false},
  9. &Column{Name: "login", Type: DB_NVarchar, Length: 255, Nullable: false},
  10. &Column{Name: "email", Type: DB_NVarchar, Length: 255, Nullable: false},
  11. &Column{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: true},
  12. &Column{Name: "password", Type: DB_NVarchar, Length: 255, Nullable: true},
  13. &Column{Name: "salt", Type: DB_NVarchar, Length: 50, Nullable: true},
  14. &Column{Name: "rands", Type: DB_NVarchar, Length: 50, Nullable: true},
  15. &Column{Name: "company", Type: DB_NVarchar, Length: 255, Nullable: true},
  16. &Column{Name: "account_id", Type: DB_BigInt, Nullable: false},
  17. &Column{Name: "is_admin", Type: DB_Bool, Nullable: false},
  18. &Column{Name: "created", Type: DB_DateTime, Nullable: false},
  19. &Column{Name: "updated", Type: DB_DateTime, Nullable: false},
  20. },
  21. Indices: []*Index{
  22. &Index{Cols: []string{"login"}, Type: UniqueIndex},
  23. &Index{Cols: []string{"email"}, Type: UniqueIndex},
  24. },
  25. }
  26. // create table
  27. mg.AddMigration("create user table", NewAddTableMigration(userV1))
  28. // add indices
  29. mg.AddMigration("add unique index user.login", NewAddIndexMigration(userV1, userV1.Indices[0]))
  30. mg.AddMigration("add unique index user.email", NewAddIndexMigration(userV1, userV1.Indices[1]))
  31. // ---------------------
  32. // account -> org changes
  33. //------- drop indexes ------------------
  34. addDropAllIndicesMigrations(mg, "v1", userV1)
  35. //------- rename table ------------------
  36. addTableRenameMigration(mg, "user", "user_v1", "v1")
  37. //------- recreate table with new column names ------------------
  38. userV2 := Table{
  39. Name: "user",
  40. Columns: []*Column{
  41. &Column{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  42. &Column{Name: "version", Type: DB_Int, Nullable: false},
  43. &Column{Name: "login", Type: DB_NVarchar, Length: 255, Nullable: false},
  44. &Column{Name: "email", Type: DB_NVarchar, Length: 255, Nullable: false},
  45. &Column{Name: "name", Type: DB_NVarchar, Length: 255, Nullable: true},
  46. &Column{Name: "password", Type: DB_NVarchar, Length: 255, Nullable: true},
  47. &Column{Name: "salt", Type: DB_NVarchar, Length: 50, Nullable: true},
  48. &Column{Name: "rands", Type: DB_NVarchar, Length: 50, Nullable: true},
  49. &Column{Name: "company", Type: DB_NVarchar, Length: 255, Nullable: true},
  50. &Column{Name: "org_id", Type: DB_BigInt, Nullable: false},
  51. &Column{Name: "is_admin", Type: DB_Bool, Nullable: false},
  52. &Column{Name: "email_verified", Type: DB_Bool, Nullable: true},
  53. &Column{Name: "theme", Type: DB_NVarchar, Length: 255, Nullable: true},
  54. &Column{Name: "created", Type: DB_DateTime, Nullable: false},
  55. &Column{Name: "updated", Type: DB_DateTime, Nullable: false},
  56. },
  57. Indices: []*Index{
  58. &Index{Cols: []string{"login"}, Type: UniqueIndex},
  59. &Index{Cols: []string{"email"}, Type: UniqueIndex},
  60. },
  61. }
  62. mg.AddMigration("create user table v2", NewAddTableMigration(userV2))
  63. addTableIndicesMigrations(mg, "v2", userV2)
  64. //------- copy data from v1 to v2 -------------------
  65. mg.AddMigration("copy data_source v1 to v2", NewCopyTableDataMigration("user", "user_v1", map[string]string{
  66. "id": "id",
  67. "version": "version",
  68. "login": "login",
  69. "email": "email",
  70. "name": "name",
  71. "password": "password",
  72. "salt": "salt",
  73. "rands": "rands",
  74. "company": "company",
  75. "org_id": "account_id",
  76. "is_admin": "is_admin",
  77. "created": "created",
  78. "updated": "updated",
  79. }))
  80. mg.AddMigration("Drop old table user_v1", NewDropTableMigration("user_v1"))
  81. }