user_mig.go 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  8. {Name: "version", Type: DB_Int, Nullable: false},
  9. {Name: "login", Type: DB_NVarchar, Length: 255, Nullable: false},
  10. {Name: "email", Type: DB_NVarchar, Length: 255, Nullable: false},
  11. {Name: "name", Type: DB_NVarchar, Length: 255, Nullable: true},
  12. {Name: "password", Type: DB_NVarchar, Length: 255, Nullable: true},
  13. {Name: "salt", Type: DB_NVarchar, Length: 50, Nullable: true},
  14. {Name: "rands", Type: DB_NVarchar, Length: 50, Nullable: true},
  15. {Name: "company", Type: DB_NVarchar, Length: 255, Nullable: true},
  16. {Name: "account_id", Type: DB_BigInt, Nullable: false},
  17. {Name: "is_admin", Type: DB_Bool, Nullable: false},
  18. {Name: "created", Type: DB_DateTime, Nullable: false},
  19. {Name: "updated", Type: DB_DateTime, Nullable: false},
  20. },
  21. Indices: []*Index{
  22. {Cols: []string{"login"}, Type: UniqueIndex},
  23. {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. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  42. {Name: "version", Type: DB_Int, Nullable: false},
  43. {Name: "login", Type: DB_NVarchar, Length: 255, Nullable: false},
  44. {Name: "email", Type: DB_NVarchar, Length: 255, Nullable: false},
  45. {Name: "name", Type: DB_NVarchar, Length: 255, Nullable: true},
  46. {Name: "password", Type: DB_NVarchar, Length: 255, Nullable: true},
  47. {Name: "salt", Type: DB_NVarchar, Length: 50, Nullable: true},
  48. {Name: "rands", Type: DB_NVarchar, Length: 50, Nullable: true},
  49. {Name: "company", Type: DB_NVarchar, Length: 255, Nullable: true},
  50. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  51. {Name: "is_admin", Type: DB_Bool, Nullable: false},
  52. {Name: "email_verified", Type: DB_Bool, Nullable: true},
  53. {Name: "theme", Type: DB_NVarchar, Length: 255, Nullable: true},
  54. {Name: "created", Type: DB_DateTime, Nullable: false},
  55. {Name: "updated", Type: DB_DateTime, Nullable: false},
  56. },
  57. Indices: []*Index{
  58. {Cols: []string{"login"}, Type: UniqueIndex},
  59. {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. mg.AddMigration("Add column help_flags1 to user table", NewAddColumnMigration(userV2, &Column{
  82. Name: "help_flags1", Type: DB_BigInt, Nullable: false, Default: "0",
  83. }))
  84. }