Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ abstract class HtmlMigration extends DocumentMigration {

val newArticle = oldArticle.copy(
title = convertedTitle,
introduction = convertedIntroduction,
content = convertedContent,
metaDescription = convertedMetaDescription,
visualElement = convertedVisualElement,
introduction = convertedIntroduction,
metaDescription = convertedMetaDescription,
)
newArticle.asJson.noSpaces
}
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kanskje lurt med noen tester på disse migreringene?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migreringene er avhengig av nye spørringer, så blir fort litt komplekst. Men kan sikkert mocke noe her. Ser på det.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Part of NDLA article-api
* Copyright (C) 2026 NDLA
*
* See LICENSE
*
*/

package no.ndla.articleapi.db.migration

import io.circe.{Json, parser}
import no.ndla.database.TableMigration
import org.postgresql.util.PGobject
import scalikejdbc.{DBSession, SQLSyntax, WrappedResultSet}
import scalikejdbc.interpolation.Implicits.scalikejdbcSQLInterpolationImplicitDef

case class DocumentRow(id: Long, revision: Int, articleId: Long, document: String)

class V69__SetPublishedCount extends TableMigration[DocumentRow] {
val columnName: String = "document"
override val tableName: String = "contentdata"

private lazy val columnNameSQL: SQLSyntax = SQLSyntax.createUnsafely(columnName)
override lazy val whereClause: SQLSyntax = sqls"$columnNameSQL is not null"

private def countOtherVersions(revision: Int, articleId: Long)(implicit session: DBSession): Long = {
sql"select count(*) from $tableNameSQL where revision < $revision and article_id = $articleId"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ser at det tar ganske lang tid å kjøre denne lokalt med dump fra test, men ikke sikkert det er verdt bryet å skulle skrive den raskere 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Den er jo avhengig av å telle opp gamle varianter av artikler, så tar jo tid. Kom gjerne med forslag til måter å gjøre den kjappere.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Virker som det går an å gjøre det med en SQL-only migrering, men det ble ganske syre, så tenker det er fint som det er 😄

.map(rs => rs.long("count"))
.single()
.getOrElse(0L)
}

override def extractRowData(rs: WrappedResultSet): DocumentRow =
DocumentRow(rs.long("id"), rs.int("revision"), rs.long("article_id"), rs.string(columnName))

override def updateRow(rowData: DocumentRow)(implicit session: DBSession): Int = {
val other = countOtherVersions(rowData.revision, rowData.articleId)
val oldDocument = parser.parse(rowData.document).toTry.get
val newDoc = oldDocument.mapObject(_.add("publishedCount", Json.fromLong(other + 1))).noSpaces

val dataObject = new PGobject()
dataObject.setType("jsonb")
dataObject.setValue(newDoc)
sql"""update $tableNameSQL
set $columnNameSQL = $dataObject
where id = ${rowData.id}
""".update()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ case class ArticleV2DTO(
disclaimer: Option[DisclaimerDTO],
@description("Traits extracted from the article content")
traits: List[ArticleTrait],
@description("Number of times the article have been published")
publishedCount: Int,
)

object ArticleV2DTO {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ class ConverterService(using props: Props) extends StrictLogging {
val newPublishedDate = partialArticle.published.getOrElse(existingArticle.published)

existingArticle.copy(
availability = newAvailability,
grepCodes = newGrepCodes,
copyright = existingArticle.copyright.copy(license = newLicense),
tags = newTags,
metaDescription = newMeta,
published = newPublishedDate,
grepCodes = newGrepCodes,
availability = newAvailability,
relatedContent = newRelatedContent,
tags = newTags,
revisionDate = newRevisionDate,
published = newPublishedDate,
)
}

Expand Down Expand Up @@ -289,6 +289,7 @@ class ConverterService(using props: Props) extends StrictLogging {
slug = article.slug,
disclaimer = disclaimer,
traits = article.traits,
publishedCount = article.publishedCount.getOrElse(1),
)
)
} else {
Expand Down
8 changes: 8 additions & 0 deletions article-api/src/test/scala/no/ndla/articleapi/TestData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class TestData {
slug = None,
disclaimer = None,
traits = List.empty,
publishedCount = 1,
)

val apiArticleV2: api.ArticleV2DTO = api.ArticleV2DTO(
Expand Down Expand Up @@ -129,6 +130,7 @@ class TestData {
slug = None,
disclaimer = None,
traits = List.empty,
publishedCount = 1,
)

val sampleArticleWithPublicDomain: Article = Article(
Expand Down Expand Up @@ -161,6 +163,7 @@ class TestData {
slug = None,
disclaimer = OptLanguageFields.empty,
traits = List.empty,
publishedCount = Some(1),
)

val sampleDomainArticle: Article = Article(
Expand Down Expand Up @@ -188,6 +191,7 @@ class TestData {
slug = None,
disclaimer = OptLanguageFields.empty,
traits = List.empty,
publishedCount = Some(1),
)

val sampleDomainArticle2: Article = Article(
Expand Down Expand Up @@ -215,6 +219,7 @@ class TestData {
slug = None,
disclaimer = OptLanguageFields.empty,
traits = List.empty,
publishedCount = Some(1),
)

val sampleArticleWithByNcSa: Article = sampleArticleWithPublicDomain.copy(copyright = byNcSaCopyright)
Expand Down Expand Up @@ -254,6 +259,7 @@ class TestData {
slug = None,
disclaimer = OptLanguageFields.empty,
traits = List.empty,
publishedCount = Some(1),
)

val apiArticleWithHtmlFaultV2: api.ArticleV2DTO = api.ArticleV2DTO(
Expand Down Expand Up @@ -301,6 +307,7 @@ class TestData {
slug = None,
disclaimer = None,
traits = List.empty,
publishedCount = 1,
)

val (nodeId, nodeId2) = ("1234", "4321")
Expand Down Expand Up @@ -337,6 +344,7 @@ class TestData {
slug = None,
disclaimer = OptLanguageFields.empty,
traits = List.empty,
publishedCount = Some(1),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ class ConverterServiceTest extends UnitSuite with TestEnvironment {
val existingArticle = TestData
.sampleDomainArticle
.copy(
availability = Availability.everyone,
grepCodes = Seq("old", "code"),
copyright = Copyright("CC-BY-4.0", Some("origin"), Seq(), Seq(), Seq(), None, None, false),
tags = Seq(Tag(Seq("gammel", "Tag"), "nb")),
metaDescription = Seq(Description("gammelDesc", "nb")),
grepCodes = Seq("old", "code"),
availability = Availability.everyone,
relatedContent = Seq(Left(RelatedContentLink("title1", "url1")), Right(12L)),
tags = Seq(Tag(Seq("gammel", "Tag"), "nb")),
)

val revisionDate = NDLADate.now()
Expand All @@ -232,18 +232,18 @@ class ConverterServiceTest extends UnitSuite with TestEnvironment {
val updatedArticle = TestData
.sampleDomainArticle
.copy(
availability = Availability.teacher,
grepCodes = Seq("New", "grep", "codes"),
copyright = Copyright("newLicense", Some("origin"), Seq(), Seq(), Seq(), None, None, false),
tags = Seq(Tag(Seq("nye", "Tags"), "nb")),
metaDescription = Seq(Description("nyDesc", "nb")),
published = revisionDate,
grepCodes = Seq("New", "grep", "codes"),
availability = Availability.teacher,
relatedContent = Seq(
Left(RelatedContentLink("New Title", "New Url")),
Left(RelatedContentLink("Newer Title", "Newer Url")),
Right(42L),
),
tags = Seq(Tag(Seq("nye", "Tags"), "nb")),
revisionDate = Some(revisionDate),
published = revisionDate,
)

service.updateArticleFields(existingArticle, partialArticle) should be(updatedArticle)
Expand All @@ -254,13 +254,13 @@ class ConverterServiceTest extends UnitSuite with TestEnvironment {
val existingArticle = TestData
.sampleDomainArticle
.copy(
availability = Availability.everyone,
grepCodes = Seq("old", "code"),
copyright = Copyright("CC-BY-4.0", Some("origin"), Seq(), Seq(), Seq(), None, None, false),
tags = Seq(Tag(Seq("Gluten", "Tag"), "de")),
metaDescription = Seq(Description("oldDesc", "de")),
grepCodes = Seq("old", "code"),
availability = Availability.everyone,
relatedContent =
Seq(Left(RelatedContentLink("title1", "url1")), Left(RelatedContentLink("old title", "old url"))),
tags = Seq(Tag(Seq("Gluten", "Tag"), "de")),
)

val revisionDate = NDLADate.now()
Expand Down Expand Up @@ -289,14 +289,14 @@ class ConverterServiceTest extends UnitSuite with TestEnvironment {
val updatedArticle = TestData
.sampleDomainArticle
.copy(
availability = Availability.teacher,
grepCodes = Seq("New", "grep", "codes"),
copyright = Copyright("newLicense", Some("origin"), Seq(), Seq(), Seq(), None, None, false),
tags = Seq(Tag(Seq("Guten", "Tag"), "de")),
metaDescription = Seq(Description("neuDesc", "de")),
published = revisionDate,
grepCodes = Seq("New", "grep", "codes"),
availability = Availability.teacher,
relatedContent = Seq(Right(42L), Right(420L), Right(4200L)),
tags = Seq(Tag(Seq("Guten", "Tag"), "de")),
revisionDate = Some(revisionDate),
published = revisionDate,
)

service.updateArticleFields(existingArticle, partialArticle) should be(updatedArticle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ class ReadServiceTest extends UnitSuite with TestEnvironment {
title = Seq(Title("Parent title", "nb")),
metaDescription = Seq(Description("Parent description", "nb")),
metaImage = Seq(ArticleMetaImage("1000", "alt", "nb")),
slug = Some("some-slug"),
published = date,
slug = Some("some-slug"),
)

val article1 = TestData
Expand All @@ -304,8 +304,8 @@ class ReadServiceTest extends UnitSuite with TestEnvironment {
title = Seq(Title("Article1 title", "nb")),
metaDescription = Seq(Description("Article1 description", "nb")),
metaImage = Seq(ArticleMetaImage("1000", "alt", "nb")),
slug = Some("slug-one"),
published = date,
slug = Some("slug-one"),
)

val article2 = TestData
Expand All @@ -315,8 +315,8 @@ class ReadServiceTest extends UnitSuite with TestEnvironment {
title = Seq(Title("Article2 title", "nb")),
metaDescription = Seq(Description("Article2 description", "nb")),
metaImage = Seq(),
slug = Some("slug-two"),
published = date,
slug = Some("slug-two"),
)

val frontPage = FrontPageDTO(
Expand Down
Loading