Skip to content

Commit

Permalink
concept-api: Migrate :-separated tags to separate tags
Browse files Browse the repository at this point in the history
  • Loading branch information
jnatten committed Feb 5, 2025
1 parent 12ea8ab commit cfab8c1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Part of NDLA concept-api
* Copyright (C) 2025 NDLA
*
* See LICENSE
*
*/

package no.ndla.conceptapi.db.migration

import io.circe.parser
import io.circe.syntax.EncoderOps
import io.circe.generic.auto.*
import no.ndla.database.DocumentMigration

case class TagsObject(tags: List[String], language: String)

class V22__SplitTags extends DocumentMigration {
override val columnName: String = "document"
override val tableName: String = "conceptdata"

private def convertTags(tags: List[TagsObject]): List[TagsObject] = tags.map { to =>
val splitTags = to.tags.flatMap(_.split(":")).filterNot(_.isEmpty)
to.copy(tags = splitTags)
}

override def convertColumn(document: String): String = {
val oldDocument = parser.parse(document).toTry.get
oldDocument.hcursor.downField("tags").as[Option[List[TagsObject]]].toTry.get match {
case None => document
case Some(tags) =>
val convertedTags = convertTags(tags).asJson
val newDocument = oldDocument.mapObject(_.remove("tags").add("tags", convertedTags))
newDocument.noSpaces
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Part of NDLA concept-api
* Copyright (C) 2025 NDLA
*
* See LICENSE
*
*/

package no.ndla.conceptapi.db.migration

import no.ndla.conceptapi.UnitSuite

class V22__SplitTagsTest extends UnitSuite {

val migration = new V22__SplitTags

test("That converting colon separated tags works") {
val oldDocument =
"""{"tags":[{"tags":["tag1:tag2:tag3"],"language":"nb"},{"tags":["tag4:tag5:tag6"],"language":"en"}]}"""
val converted = migration.convertColumn(oldDocument)
val expectedDocument =
"""{"tags":[{"tags":["tag1","tag2","tag3"],"language":"nb"},{"tags":["tag4","tag5","tag6"],"language":"en"}]}"""
converted should be(expectedDocument)
}

test("That converting colon separated tags works with empty values") {
val oldDocument = """{"tags":[{"tags":["tag1::","apekatt",":snabeldyr:"],"language":"nb"}]}"""
val converted = migration.convertColumn(oldDocument)
val expectedDocument = """{"tags":[{"tags":["tag1","apekatt","snabeldyr"],"language":"nb"}]}"""
converted should be(expectedDocument)
}

test("That non colon separated tags are not changed") {
val oldDocument = """{"tags":[{"tags":["tag1","apekatt","snabeldyr"],"language":"nb"}]}"""
val converted = migration.convertColumn(oldDocument)
val expectedDocument = """{"tags":[{"tags":["tag1","apekatt","snabeldyr"],"language":"nb"}]}"""
converted should be(expectedDocument)
}

test("That no tags doesn't crash") {
val oldDocument = """{"someotherfield":"yabadabado"}"""
val converted = migration.convertColumn(oldDocument)
converted should be(oldDocument)
}
}

0 comments on commit cfab8c1

Please sign in to comment.