user_mig.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package migrations
  2. import (
  3. "fmt"
  4. "github.com/go-xorm/xorm"
  5. . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
  6. "github.com/grafana/grafana/pkg/util"
  7. )
  8. func addUserMigrations(mg *Migrator) {
  9. userV1 := Table{
  10. Name: "user",
  11. Columns: []*Column{
  12. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  13. {Name: "version", Type: DB_Int, Nullable: false},
  14. {Name: "login", Type: DB_NVarchar, Length: 190, Nullable: false},
  15. {Name: "email", Type: DB_NVarchar, Length: 190, Nullable: false},
  16. {Name: "name", Type: DB_NVarchar, Length: 255, Nullable: true},
  17. {Name: "password", Type: DB_NVarchar, Length: 255, Nullable: true},
  18. {Name: "salt", Type: DB_NVarchar, Length: 50, Nullable: true},
  19. {Name: "rands", Type: DB_NVarchar, Length: 50, Nullable: true},
  20. {Name: "company", Type: DB_NVarchar, Length: 255, Nullable: true},
  21. {Name: "account_id", Type: DB_BigInt, Nullable: false},
  22. {Name: "is_admin", Type: DB_Bool, Nullable: false},
  23. {Name: "created", Type: DB_DateTime, Nullable: false},
  24. {Name: "updated", Type: DB_DateTime, Nullable: false},
  25. },
  26. Indices: []*Index{
  27. {Cols: []string{"login"}, Type: UniqueIndex},
  28. {Cols: []string{"email"}, Type: UniqueIndex},
  29. },
  30. }
  31. // create table
  32. mg.AddMigration("create user table", NewAddTableMigration(userV1))
  33. // add indices
  34. mg.AddMigration("add unique index user.login", NewAddIndexMigration(userV1, userV1.Indices[0]))
  35. mg.AddMigration("add unique index user.email", NewAddIndexMigration(userV1, userV1.Indices[1]))
  36. // ---------------------
  37. // account -> org changes
  38. //------- drop indexes ------------------
  39. addDropAllIndicesMigrations(mg, "v1", userV1)
  40. //------- rename table ------------------
  41. addTableRenameMigration(mg, "user", "user_v1", "v1")
  42. //------- recreate table with new column names ------------------
  43. userV2 := Table{
  44. Name: "user",
  45. Columns: []*Column{
  46. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  47. {Name: "version", Type: DB_Int, Nullable: false},
  48. {Name: "login", Type: DB_NVarchar, Length: 190, Nullable: false},
  49. {Name: "email", Type: DB_NVarchar, Length: 190, Nullable: false},
  50. {Name: "name", Type: DB_NVarchar, Length: 255, Nullable: true},
  51. {Name: "password", Type: DB_NVarchar, Length: 255, Nullable: true},
  52. {Name: "salt", Type: DB_NVarchar, Length: 50, Nullable: true},
  53. {Name: "rands", Type: DB_NVarchar, Length: 50, Nullable: true},
  54. {Name: "company", Type: DB_NVarchar, Length: 255, Nullable: true},
  55. {Name: "org_id", Type: DB_BigInt, Nullable: false},
  56. {Name: "is_admin", Type: DB_Bool, Nullable: false},
  57. {Name: "email_verified", Type: DB_Bool, Nullable: true},
  58. {Name: "theme", Type: DB_NVarchar, Length: 255, Nullable: true},
  59. {Name: "created", Type: DB_DateTime, Nullable: false},
  60. {Name: "updated", Type: DB_DateTime, Nullable: false},
  61. },
  62. Indices: []*Index{
  63. {Cols: []string{"login"}, Type: UniqueIndex},
  64. {Cols: []string{"email"}, Type: UniqueIndex},
  65. },
  66. }
  67. mg.AddMigration("create user table v2", NewAddTableMigration(userV2))
  68. addTableIndicesMigrations(mg, "v2", userV2)
  69. //------- copy data from v1 to v2 -------------------
  70. mg.AddMigration("copy data_source v1 to v2", NewCopyTableDataMigration("user", "user_v1", map[string]string{
  71. "id": "id",
  72. "version": "version",
  73. "login": "login",
  74. "email": "email",
  75. "name": "name",
  76. "password": "password",
  77. "salt": "salt",
  78. "rands": "rands",
  79. "company": "company",
  80. "org_id": "account_id",
  81. "is_admin": "is_admin",
  82. "created": "created",
  83. "updated": "updated",
  84. }))
  85. mg.AddMigration("Drop old table user_v1", NewDropTableMigration("user_v1"))
  86. mg.AddMigration("Add column help_flags1 to user table", NewAddColumnMigration(userV2, &Column{
  87. Name: "help_flags1", Type: DB_BigInt, Nullable: false, Default: "0",
  88. }))
  89. mg.AddMigration("Update user table charset", NewTableCharsetMigration("user", []*Column{
  90. {Name: "login", Type: DB_NVarchar, Length: 190, Nullable: false},
  91. {Name: "email", Type: DB_NVarchar, Length: 190, Nullable: false},
  92. {Name: "name", Type: DB_NVarchar, Length: 255, Nullable: true},
  93. {Name: "password", Type: DB_NVarchar, Length: 255, Nullable: true},
  94. {Name: "salt", Type: DB_NVarchar, Length: 50, Nullable: true},
  95. {Name: "rands", Type: DB_NVarchar, Length: 50, Nullable: true},
  96. {Name: "company", Type: DB_NVarchar, Length: 255, Nullable: true},
  97. {Name: "theme", Type: DB_NVarchar, Length: 255, Nullable: true},
  98. }))
  99. mg.AddMigration("Add last_seen_at column to user", NewAddColumnMigration(userV2, &Column{
  100. Name: "last_seen_at", Type: DB_DateTime, Nullable: true,
  101. }))
  102. // Adds salt & rands for old users who used ldap or oauth
  103. mg.AddMigration("Add missing user data", &AddMissingUserSaltAndRandsMigration{})
  104. }
  105. type AddMissingUserSaltAndRandsMigration struct {
  106. MigrationBase
  107. }
  108. func (m *AddMissingUserSaltAndRandsMigration) Sql(dialect Dialect) string {
  109. return "code migration"
  110. }
  111. type TempUserDTO struct {
  112. Id int64
  113. Login string
  114. }
  115. func (m *AddMissingUserSaltAndRandsMigration) Exec(sess *xorm.Session, mg *Migrator) error {
  116. users := make([]*TempUserDTO, 0)
  117. err := sess.Sql(fmt.Sprintf("SELECT id, login from %s WHERE rands = ''", mg.Dialect.Quote("user"))).Find(&users)
  118. if err != nil {
  119. return err
  120. }
  121. for _, user := range users {
  122. _, err := sess.Exec("UPDATE "+mg.Dialect.Quote("user")+" SET salt = ?, rands = ? WHERE id = ?", util.GetRandomString(10), util.GetRandomString(10), user.Id)
  123. if err != nil {
  124. return err
  125. }
  126. }
  127. return nil
  128. }