-
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.
concept-api: Migrate
:
-separated tags to separate tags
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
concept-api/src/main/scala/no/ndla/conceptapi/db/migration/V22__SplitTags.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,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 | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
concept-api/src/test/scala/no/ndla/conceptapi/db/migration/V22__SplitTagsTest.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,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) | ||
} | ||
} |