我们有一个巨大的表,我们需要向其中添加一个type
列。
从技术上讲,该列假定不可为空,但是更新所有内容(1.5亿条记录)的迁移有点疯狂。
因此,我们决定将其保留为可空状态,并在解析器中构建一个转换器,以便始终为模型填充正确的值,并在保存数据时缓慢而确定地将数据回填。
对于可空布尔值,这种方法在很多地方对我们都有效。
我的模型如下:
package models.tasks
import anorm.{Column, _}
import db.ProcessStreetColumn._
import exceptions.ProcessStreetException
import json.ProcessStreetJoda
import models._
import models.checklists.ChecklistRevision
import models.organization.Organization
import models.task_templates.TaskTemplate
import org.joda.time.DateTime
import play.api.libs.json.{Format, Json}
import st.process.util.Muid
case class Task(id: Muid,
audit: AuditMetadata,
completedDate: Option[DateTime],
completedBy: Option[Ref[User]],
organization: Ref[Organization],
taskTemplate: Ref[TaskTemplate],
checklistRevision: Ref[ChecklistRevision],
status: Task.Status.Value,
dueDate: Option[DateTime],
dueDateOverridden: Boolean,
hidden: Boolean,
stopped: Boolean,
taskType: Task.TaskType.Value)
extends Identifiable[Muid]
with Auditable
object Task extends ProcessStreetModel[Task] with ProcessStreetJoda {
object TaskType extends Enumeration with AnormEnumeration with JsonEnumeration {
val Standard, Approval = Value
}
...
在这种情况下,我创建了此转换器:
val nullableRowToTaskType: Column[TaskType.Value] = Column[TaskType.Value] {
case (value, MetaDataItem(qualified, _, _)) =>
Option(value) match {
case Some(TaskType.Approval) => Right(TaskType.Approval)
case Some(TaskType.Standard) | None => Right(TaskType.Standard)
case _ =>
Left(TypeDoesNotMatch("Cannot convert " + value + " to TaskType for column " + qualified))
}
}
我正在按如下方式使用它:
...
SqlParser.get[TaskType.Value](qualifiedColumnNameOf(prefix, "task_type"))(nullableRowToTaskType)
...
但是,在转换器中,出现以下错误:
play.sbt.PlayExceptions$CompilationException: Compilation error[object Column does not take type parameters.]
at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:34)
at play.sbt.PlayExceptions$CompilationException$.apply(PlayExceptions.scala:34)
at scala.Option.map(Option.scala:146)
at play.sbt.run.PlayReload$.$anonfun$taskFailureHandler$1(PlayReload.scala:33)
at scala.Option.map(Option.scala:146)
at play.sbt.run.PlayReload$.taskFailureHandler(PlayReload.scala:28)
at play.sbt.run.PlayReload$.compileFailure(PlayReload.scala:24)
at play.sbt.run.PlayReload$.$anonfun$compile$3(PlayReload.scala:51)
at scala.util.Either$LeftProjection.map(Either.scala:573)
at play.sbt.run.PlayReload$.compile(PlayReload.scala:51)
我不太确定该怎么做...不知道这意味着什么。 我在做什么错了?