user_mig.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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: 190, Nullable: false},
  10. {Name: "email", Type: DB_NVarchar, Length: 190, 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: 190, Nullable: false},
  44. {Name: "email", Type: DB_NVarchar, Length: 190, 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. mg.AddMigration("Update user table charset", NewTableCharsetMigration("user", []*Column{
  85. {Name: "login", Type: DB_NVarchar, Length: 190, Nullable: false},
  86. {Name: "email", Type: DB_NVarchar, Length: 190, Nullable: false},
  87. {Name: "name", Type: DB_NVarchar, Length: 255, Nullable: true},
  88. {Name: "password", Type: DB_NVarchar, Length: 255, Nullable: true},
  89. {Name: "salt", Type: DB_NVarchar, Length: 50, Nullable: true},
  90. {Name: "rands", Type: DB_NVarchar, Length: 50, Nullable: true},
  91. {Name: "company", Type: DB_NVarchar, Length: 255, Nullable: true},
  92. {Name: "theme", Type: DB_NVarchar, Length: 255, Nullable: true},
  93. }))
  94. mg.AddMigration("Add last_seen_at column to user", NewAddColumnMigration(userV2, &Column{
  95. Name: "last_seen_at", Type: DB_DateTime, Nullable: true,
  96. }))
  97. }