Skip to content

Commit

Permalink
Migrate all the authors
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarvelle committed Feb 26, 2025
1 parent 4fe7e84 commit f13bcb9
Show file tree
Hide file tree
Showing 20 changed files with 1,992 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Part of NDLA article-api
* Copyright (C) 2025 NDLA
*
* See LICENSE
*
*/

package no.ndla.articleapi.db.migration

import io.circe.{ACursor, parser}
import io.circe.generic.auto.*
import io.circe.syntax.EncoderOps
import no.ndla.common.model.domain.{Author, ContributorType}
import no.ndla.database.DocumentMigration

class V58__FixContributorTypes extends DocumentMigration {
override val tableName: String = "contentdata"
override val columnName: String = "document"

override def convertColumn(value: String): String = {
val oldDocument = parser.parse(value).toTry.get
val copyright = oldDocument.hcursor.downField("copyright")
val creators = convertList(copyright, "creators")
val processors = convertList(copyright, "processors")
val rightsholders = convertList(copyright, "rightsholders")

val newDocument = oldDocument.hcursor
.downField("copyright")
.withFocus(_.mapObject(_.remove("creators").add("creators", creators.asJson)))
.withFocus(_.mapObject(_.remove("processors").add("processors", processors.asJson)))
.withFocus(_.mapObject(_.remove("rightsholders").add("rightsholders", rightsholders.asJson)))
newDocument.top.get.noSpaces
}

private def convertList(cursor: ACursor, fieldName: String): List[Author] = {
val field = cursor.downField(fieldName)
if (field.succeeded) {
field.as[Option[List[OldAuthor]]].toTry.get match {
case None => List.empty
case Some(authors) => convertAuthors(authors)
}
} else List.empty
}

private def convertAuthors(authors: List[OldAuthor]): List[Author] = {
authors.map(author => {
ContributorType.valueOf(author.`type`.toLowerCase) match {
case None => Author(ContributorType.mapping(author.`type`.toLowerCase), author.name)
case Some(a) => Author(a, author.name)
}
})
}
}

case class OldAuthor(`type`: String, name: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/*
* Part of NDLA article-api
* Copyright (C) 2024 NDLA
*
* See LICENSE
*
*/

package no.ndla.articleapi.db.migration

import io.circe.parser
import no.ndla.articleapi.{TestEnvironment, UnitSuite}

class V58__FixContributorTypesTest extends UnitSuite with TestEnvironment {
test("That copyright has correct contributor types") {
val migration = new V58__FixContributorTypes
val oldDocument =
"""
|{
| "id": 1,
| "tags": [],
| "title": [
| {
| "title": "Title",
| "language": "nb"
| }
| ],
| "content": [
| {
| "content": "<section>Content</section>",
| "language": "nb"
| }
| ],
| "created": "2017-05-29T09:43:41.000Z",
| "updated": "2017-07-18T10:21:08.000Z",
| "revision": 1,
| "copyright": {
| "origin": "",
| "license": "CC-BY-SA-4.0",
| "validTo": null,
| "processed": false,
| "validFrom": null,
| "creators": [
| {
| "type": "Forfatter",
| "name": "Sissel Paaske"
| }
| ],
| "processors": [],
| "rightsholders": [
| {
| "type": "Supplier",
| "name": "Cerpus AS"
| }
| ]
| },
| "grepCodes": [],
| "metaImage": [],
| "published": "2017-07-18T10:21:08.000Z",
| "updatedBy": "r0gHb9Xg3li4yyXv0QSGQczV3bviakrT",
| "conceptIds": [],
| "disclaimer": {},
| "articleType": "standard",
| "availability": "everyone",
| "introduction": [
| {
| "language": "nb",
| "introduction": "Introduction."
| }
| ],
| "revisionDate": "2030-01-01T00:00:00.000Z",
| "visualElement": [],
| "relatedContent": [],
| "metaDescription": [
| {
| "content": "Metabeskrivelse",
| "language": "nb"
| }
| ],
| "requiredLibraries": []
|}
|""".stripMargin
val newDocument =
"""
|{
| "id": 1,
| "tags": [],
| "title": [
| {
| "title": "Title",
| "language": "nb"
| }
| ],
| "content": [
| {
| "content": "<section>Content</section>",
| "language": "nb"
| }
| ],
| "created": "2017-05-29T09:43:41.000Z",
| "updated": "2017-07-18T10:21:08.000Z",
| "revision": 1,
| "copyright": {
| "origin": "",
| "license": "CC-BY-SA-4.0",
| "validTo": null,
| "processed": false,
| "validFrom": null,
| "creators": [
| {
| "type": "writer",
| "name": "Sissel Paaske"
| }
| ],
| "processors": [],
| "rightsholders": [
| {
| "type": "supplier",
| "name": "Cerpus AS"
| }
| ]
| },
| "grepCodes": [],
| "metaImage": [],
| "published": "2017-07-18T10:21:08.000Z",
| "updatedBy": "r0gHb9Xg3li4yyXv0QSGQczV3bviakrT",
| "conceptIds": [],
| "disclaimer": {},
| "articleType": "standard",
| "availability": "everyone",
| "introduction": [
| {
| "language": "nb",
| "introduction": "Introduction."
| }
| ],
| "revisionDate": "2030-01-01T00:00:00.000Z",
| "visualElement": [],
| "relatedContent": [],
| "metaDescription": [
| {
| "content": "Metabeskrivelse",
| "language": "nb"
| }
| ],
| "requiredLibraries": []
|}
|""".stripMargin

val expected = parser.parse(newDocument).toTry.get
migration.convertColumn(oldDocument) should be(expected.noSpaces)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Part of NDLA audio-api
* Copyright (C) 2025 NDLA
*
* See LICENSE
*
*/

package no.ndla.audioapi.db.migration

import io.circe.{ACursor, parser}
import io.circe.generic.auto.*
import io.circe.syntax.EncoderOps
import no.ndla.common.model.domain.{Author, ContributorType}
import no.ndla.database.DocumentMigration

class V22__FixContributorTypes extends DocumentMigration {
override val tableName: String = "audiodata"
override val columnName: String = "document"

override def convertColumn(value: String): String = {
val oldDocument = parser.parse(value).toTry.get
val copyright = oldDocument.hcursor.downField("copyright")
val creators = convertList(copyright, "creators")
val processors = convertList(copyright, "processors")
val rightsholders = convertList(copyright, "rightsholders")

val newDocument = oldDocument.hcursor
.downField("copyright")
.withFocus(_.mapObject(_.remove("creators").add("creators", creators.asJson)))
.withFocus(_.mapObject(_.remove("processors").add("processors", processors.asJson)))
.withFocus(_.mapObject(_.remove("rightsholders").add("rightsholders", rightsholders.asJson)))
newDocument.top.get.noSpaces
}

private def convertList(cursor: ACursor, fieldName: String): List[Author] = {
val field = cursor.downField(fieldName)
if (field.succeeded) {
field.as[Option[List[OldAuthor]]].toTry.get match {
case None => List.empty
case Some(authors) => convertAuthors(authors)
}
} else List.empty
}

private def convertAuthors(authors: List[OldAuthor]): List[Author] = {
authors.map(author => {
ContributorType.valueOf(author.`type`.toLowerCase) match {
case None => Author(ContributorType.mapping(author.`type`.toLowerCase), author.name)
case Some(a) => Author(a, author.name)
}
})
}
}

case class OldAuthor(`type`: String, name: String)
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* Part of NDLA audio-api
* Copyright (C) 2025 NDLA
*
* See LICENSE
*
*/

package no.ndla.audioapi.db.migration

import io.circe.parser
import no.ndla.audioapi.{TestEnvironment, UnitSuite}

class V22__FixContributorTypesTest extends UnitSuite with TestEnvironment {
test("That copyright has correct contributor types") {
val migration = new V22__FixContributorTypes
val oldDocument =
"""
|{
| "tags": [
| {
| "tags": [
| "testtag",
| "testtttt"
| ],
| "language": "nb"
| }
| ],
| "titles": [
| {
| "title": "test",
| "language": "nb"
| }
| ],
| "created": "2023-10-04T20:30:18.167Z",
| "updated": "2023-10-04T20:30:18.167Z",
| "audioType": "standard",
| "copyright": {
| "license": "CC-BY-SA-4.0",
| "processed": true,
| "creators": [
| {
| "type": "Komponist",
| "name": "Apekatt"
| }
| ],
| "processors": [],
| "rightsholders": [
| {
| "type": "Supplier",
| "name": "NRK"
| }
| ]
| },
| "filePaths": [
| {
| "filePath": "T8BBtfD.mp3\"",
| "fileSize": 6289221,
| "language": "nb",
| "mimeType": "audio/mpeg"
| }
| ],
| "updatedBy": "fsexOCfJFGOKuy1C2e71OsvQwq0NWKAK",
| "manuscript": [],
| "podcastMeta": [],
| "supportedLanguages": null
|}
|""".stripMargin
val newDocument =
"""
|{
| "tags": [
| {
| "tags": [
| "testtag",
| "testtttt"
| ],
| "language": "nb"
| }
| ],
| "titles": [
| {
| "title": "test",
| "language": "nb"
| }
| ],
| "created": "2023-10-04T20:30:18.167Z",
| "updated": "2023-10-04T20:30:18.167Z",
| "audioType": "standard",
| "copyright": {
| "license": "CC-BY-SA-4.0",
| "processed": true,
| "creators": [
| {
| "type": "composer",
| "name": "Apekatt"
| }
| ],
| "processors": [],
| "rightsholders": [
| {
| "type": "supplier",
| "name": "NRK"
| }
| ]
| },
| "filePaths": [
| {
| "filePath": "T8BBtfD.mp3\"",
| "fileSize": 6289221,
| "language": "nb",
| "mimeType": "audio/mpeg"
| }
| ],
| "updatedBy": "fsexOCfJFGOKuy1C2e71OsvQwq0NWKAK",
| "manuscript": [],
| "podcastMeta": [],
| "supportedLanguages": null
|}
|""".stripMargin

val expected = parser.parse(newDocument).toTry.get
migration.convertColumn(oldDocument) should be(expected.noSpaces)
}
}
Loading

0 comments on commit f13bcb9

Please sign in to comment.