-
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.
- Loading branch information
1 parent
4fe7e84
commit f13bcb9
Showing
20 changed files
with
1,992 additions
and
20 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
article-api/src/main/scala/no/ndla/articleapi/db/migration/V58__FixContributorTypes.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,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) |
153 changes: 153 additions & 0 deletions
153
...cle-api/src/test/scala/no/ndla/articleapi/db/migration/V58__FixContributorTypesTest.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,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) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
audio-api/src/main/scala/no/ndla/audioapi/db/migration/V22__FixContributorTypes.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,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) |
125 changes: 125 additions & 0 deletions
125
audio-api/src/test/scala/no/ndla/audioapi/db/migration/V22__FixContributorTypesTest.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,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) | ||
} | ||
} |
Oops, something went wrong.