-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Change naming prioritized -> priority - Update priority to either be prioritized, on-hold or unspecified - Add migration to update articles
- Loading branch information
Showing
28 changed files
with
252 additions
and
142 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
common/src/main/scala/no/ndla/common/model/domain/Priority.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package no.ndla.common.model.domain | ||
import enumeratum._ | ||
import no.ndla.common.errors.ValidationException | ||
|
||
import scala.util.{Failure, Success, Try} | ||
|
||
sealed abstract class Priority(override val entryName: String) extends EnumEntry | ||
|
||
object Priority extends Enum[Priority] { | ||
case object Prioritized extends Priority("prioritized") | ||
case object OnHold extends Priority("on-hold") | ||
case object Unspecified extends Priority("unspecified") | ||
|
||
val values: IndexedSeq[Priority] = findValues | ||
|
||
def all: Seq[String] = Priority.values.map(_.entryName) | ||
def valueOf(s: String): Option[Priority] = Priority.withNameOption(s) | ||
|
||
def valueOfOrError(s: String): Try[Priority] = | ||
valueOf(s) match { | ||
case Some(p) => Success(p) | ||
case None => | ||
val validPriorities = values.map(_.toString).mkString(", ") | ||
Failure( | ||
ValidationException("priority", s"'$s' is not a valid priority. Must be one of $validPriorities") | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
draft-api/src/main/scala/no/ndla/draftapi/db/migration/V52__UpdatePrioritizedField.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Part of NDLA draft-api. | ||
* Copyright (C) 2023 NDLA | ||
* | ||
* See LICENSE | ||
*/ | ||
|
||
package no.ndla.draftapi.db.migration | ||
|
||
import io.circe.syntax.EncoderOps | ||
import io.circe.{parser} | ||
import org.flywaydb.core.api.migration.{BaseJavaMigration, Context} | ||
import org.postgresql.util.PGobject | ||
import scalikejdbc.{DB, DBSession, _} | ||
|
||
class V52__UpdatePrioritizedField extends BaseJavaMigration { | ||
|
||
def countAllRows(implicit session: DBSession): Option[Long] = { | ||
sql"select count(*) from articledata where document is not NULL" | ||
.map(rs => rs.long("count")) | ||
.single() | ||
} | ||
|
||
def allRows(offset: Long)(implicit session: DBSession): Seq[(Long, String)] = { | ||
sql"select id, document, article_id from articledata where document is not null order by id limit 1000 offset $offset" | ||
.map(rs => { | ||
(rs.long("id"), rs.string("document")) | ||
}) | ||
.list() | ||
} | ||
|
||
def updateRow(document: String, id: Long)(implicit session: DBSession): Int = { | ||
val dataObject = new PGobject() | ||
dataObject.setType("jsonb") | ||
dataObject.setValue(document) | ||
|
||
sql"update articledata set document = $dataObject where id = $id" | ||
.update() | ||
} | ||
|
||
override def migrate(context: Context): Unit = DB(context.getConnection) | ||
.autoClose(false) | ||
.withinTx { session => migrateRows(session) } | ||
|
||
def migrateRows(implicit session: DBSession): Unit = { | ||
val count = countAllRows.get | ||
var numPagesLeft = (count / 1000) + 1 | ||
var offset = 0L | ||
|
||
while (numPagesLeft > 0) { | ||
allRows(offset * 1000).map { case (id, document) => | ||
updateRow(convertDocument(document), id) | ||
}: Unit | ||
numPagesLeft -= 1 | ||
offset += 1 | ||
} | ||
} | ||
|
||
private[migration] def convertDocument(document: String): String = { | ||
val oldArticle = parser.parse(document).toTry.get | ||
val cursor = oldArticle.hcursor | ||
val oldPrioritized = cursor.downField("prioritized").as[Boolean].toTry.get | ||
val newValue = if (oldPrioritized) { | ||
"prioritized" | ||
} else { | ||
"unspecified" | ||
} | ||
val newJson = cursor.withFocus(_.mapObject(obj => obj.add("priority", newValue.asJson).remove("prioritized"))) | ||
newJson.top.get.noSpaces | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.