org_mig.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package migrations
  2. import . "github.com/grafana/grafana/pkg/services/sqlstore/migrator"
  3. func addOrgMigrations(mg *Migrator) {
  4. orgV1 := Table{
  5. Name: "org",
  6. Columns: []*Column{
  7. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  8. {Name: "version", Type: DB_Int, Nullable: false},
  9. {Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
  10. {Name: "address1", Type: DB_NVarchar, Length: 255, Nullable: true},
  11. {Name: "address2", Type: DB_NVarchar, Length: 255, Nullable: true},
  12. {Name: "city", Type: DB_NVarchar, Length: 255, Nullable: true},
  13. {Name: "state", Type: DB_NVarchar, Length: 255, Nullable: true},
  14. {Name: "zip_code", Type: DB_NVarchar, Length: 50, Nullable: true},
  15. {Name: "country", Type: DB_NVarchar, Length: 255, Nullable: true},
  16. {Name: "billing_email", Type: DB_NVarchar, Length: 255, Nullable: true},
  17. {Name: "created", Type: DB_DateTime, Nullable: false},
  18. {Name: "updated", Type: DB_DateTime, Nullable: false},
  19. },
  20. Indices: []*Index{
  21. {Cols: []string{"name"}, Type: UniqueIndex},
  22. },
  23. }
  24. // add org v1
  25. mg.AddMigration("create org table v1", NewAddTableMigration(orgV1))
  26. addTableIndicesMigrations(mg, "v1", orgV1)
  27. orgUserV1 := Table{
  28. Name: "org_user",
  29. Columns: []*Column{
  30. {Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
  31. {Name: "org_id", Type: DB_BigInt},
  32. {Name: "user_id", Type: DB_BigInt},
  33. {Name: "role", Type: DB_NVarchar, Length: 20},
  34. {Name: "created", Type: DB_DateTime},
  35. {Name: "updated", Type: DB_DateTime},
  36. },
  37. Indices: []*Index{
  38. {Cols: []string{"org_id"}},
  39. {Cols: []string{"org_id", "user_id"}, Type: UniqueIndex},
  40. },
  41. }
  42. //------- org_user table -------------------
  43. mg.AddMigration("create org_user table v1", NewAddTableMigration(orgUserV1))
  44. addTableIndicesMigrations(mg, "v1", orgUserV1)
  45. mg.AddMigration("Update org table charset", NewTableCharsetMigration("org", []*Column{
  46. {Name: "name", Type: DB_NVarchar, Length: 190, Nullable: false},
  47. {Name: "address1", Type: DB_NVarchar, Length: 255, Nullable: true},
  48. {Name: "address2", Type: DB_NVarchar, Length: 255, Nullable: true},
  49. {Name: "city", Type: DB_NVarchar, Length: 255, Nullable: true},
  50. {Name: "state", Type: DB_NVarchar, Length: 255, Nullable: true},
  51. {Name: "zip_code", Type: DB_NVarchar, Length: 50, Nullable: true},
  52. {Name: "country", Type: DB_NVarchar, Length: 255, Nullable: true},
  53. {Name: "billing_email", Type: DB_NVarchar, Length: 255, Nullable: true},
  54. }))
  55. mg.AddMigration("Update org_user table charset", NewTableCharsetMigration("org_user", []*Column{
  56. {Name: "role", Type: DB_NVarchar, Length: 20},
  57. }))
  58. const migrateReadOnlyViewersToViewers = `UPDATE org_user SET role = 'Viewer' WHERE role = 'Read Only Editor'`
  59. mg.AddMigration("Migrate all Read Only Viewers to Viewers", NewRawSqlMigration(migrateReadOnlyViewersToViewers))
  60. }