PageRenderTime 187ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/play/app/models/Version.scala

https://github.com/Bochenski/stackcanon
Scala | 119 lines | 99 code | 15 blank | 5 comment | 4 complexity | 24f48d542d85eac49bcccc4c4d61daab MD5 | raw file
  1. package models
  2. import play._
  3. import play.libs.Codec;
  4. import com.mongodb.casbah.Imports._
  5. import net.liftweb.json._
  6. import net.liftweb.json.JsonAST
  7. import net.liftweb.json.JsonDSL._
  8. import com.thoughtworks.xstream.core.util.ObjectIdDictionary
  9. class Version(o: DBObject) extends DBInstance("Version", o) {
  10. lazy val oid = o.getAs[ObjectId]("_id")
  11. lazy val version = o.getAs[Int]("version")
  12. def getIdString = {
  13. oid match {
  14. case Some(x) => x.toString
  15. case None => ""
  16. }
  17. }
  18. }
  19. object Version extends DBBase[Version]("Versions") {
  20. def create(version: Int) = {
  21. val builder = MongoDBObject.newBuilder
  22. builder += "version" -> version
  23. val newObj = builder.result().asDBObject
  24. coll += newObj
  25. true
  26. }
  27. def check() = {
  28. //first find our current DB version
  29. Logger.info("checking DB version and updating if required)")
  30. val appVersion = Play.configuration.getProperty("version").toInt
  31. Logger.info("appVersion " + appVersion.toString)
  32. findOne match {
  33. case Some(entry) => {
  34. entry.version match {
  35. case Some(dbVersion) => {
  36. migrate(dbVersion,appVersion)
  37. }
  38. case None => {
  39. Logger.error("no version number found")
  40. }
  41. }
  42. }
  43. case None => {
  44. //so we need to apply all schema changes
  45. migrate(0,appVersion)
  46. }
  47. }
  48. }
  49. private def migrate(fromVersion: Int, toVersion: Int) = {
  50. Logger.info("in migrate")
  51. var currentVersion = fromVersion
  52. if (fromVersion < toVersion) {
  53. while (currentVersion < toVersion ) {
  54. currentVersion match {
  55. case 0 => {
  56. //upgrade to version 1
  57. models.User.all() foreach { user =>
  58. var currentRoles = user.getUserRoleIdStrings
  59. user.getUserRoleIdStrings foreach { role =>
  60. models.Role.findByName(role) match {
  61. case Some(dbRole) => {
  62. //so here we already have a role by that name, just need to alter the object
  63. currentRoles = migrateRoleNamesToRoleIds(user,role,dbRole,currentRoles)
  64. }
  65. case None => {
  66. models.Role.create(role)
  67. val dbRole = models.Role.findByName(role).get
  68. currentRoles = migrateRoleNamesToRoleIds(user,role,dbRole,currentRoles)
  69. }
  70. }
  71. }
  72. }
  73. update(1)
  74. currentVersion += 1
  75. }
  76. }
  77. }
  78. } else {
  79. //TODO: Rollbacks
  80. }
  81. }
  82. def removeString(s: String, li: List[String]) = {
  83. val (left, right) = li.span(_ != s)
  84. left ::: right.drop(1)
  85. }
  86. private def migrateRoleNamesToRoleIds(user: User, role: String, dbRole: Role, currentRoles: List[String]) = {
  87. Logger.info("migrating user role: " + role)
  88. val newRoles = removeString(role,currentRoles) ++ List(dbRole.getIdString)
  89. Logger.info("newRoles " + newRoles.toString)
  90. Logger.info("user id" + user.getIdString)
  91. models.User.update(MongoDBObject("_id" -> user.oid.get), $set("roles" -> newRoles))
  92. newRoles
  93. }
  94. private def update(toVersion: Int) {
  95. findOne() match {
  96. case Some(entry) => {
  97. models.User.update(MongoDBObject("_id" -> entry.oid.get),$set("version"-> toVersion))
  98. }
  99. case None => {
  100. create(toVersion)
  101. }
  102. }
  103. }
  104. }