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
1 change: 1 addition & 0 deletions image-api/src/main/resources/v27-migration-image-ids.txt
Comment thread
gunnarvelle marked this conversation as resolved.

Large diffs are not rendered by default.

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

package no.ndla.imageapi.db.migration

import io.circe.{Json, parser}
import no.ndla.database.{DocumentMigration, DocumentRow}
import org.postgresql.util.PGobject
import scalikejdbc.*
import scala.io.Source

class V27__SetImagesInactive extends DocumentMigration {
override val tableName: String = "imagemetadata"
override val columnName: String = "metadata"

private val ids: Set[Long] = {
val stream = getClass.getResourceAsStream("/v27-migration-image-ids.txt")
try {
val content = Source.fromInputStream(stream).mkString.trim
content
.split(",")
.flatMap { part =>
val trimmed = part.trim
if (trimmed.contains("-")) {
// Handle range like "11-14"
val Array(start, end) = trimmed.split("-").map(_.toLong)
start to end
} else {
// Handle single ID
Seq(trimmed.toLong)
}
}
.toSet
} finally {
if (stream != null) stream.close()
}
}

private val cutoffId = 73000

override def convertColumn(value: String): String = {
// This method is not used since we override updateRow
value
}

override def updateRow(rowData: DocumentRow)(implicit session: DBSession): Int = {
val oldDocument = parser.parse(rowData.document).toTry.get
val documentId = rowData.id

// Condition 1: id is less than cutoffId and not in ids list
val matchesIdCondition = documentId < cutoffId && !ids.contains(documentId)

// Get titles array
val titlesOpt = oldDocument.hcursor.downField("titles").focus.flatMap(_.asArray)

// Condition 2: any title contains "ikke bruk"
val matchesTitleFilter = titlesOpt.exists { titles =>
titles.exists { titleJson =>
titleJson.hcursor.downField("title").as[String].toOption match {
case Some(title) => title.toLowerCase().contains("ikke bruk")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Har vi sjekket om dette matcher på noen bilder vi vil ha egentlig?
Ser for meg "Ikke bruk snus, det er farlig" feks 😅

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Det kan det sjølvsagt treffe på. Kan sjekke litt

Copy link
Copy Markdown
Member Author

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

case None => false
}
}
}

// Get images array
val imagesOpt = oldDocument.hcursor.downField("images").focus.flatMap(_.asArray)

// Condition 3: any image has width less than 990
val hasSmallImage = imagesOpt.exists { images =>
images.exists { imageJson =>
imageJson.hcursor.downField("dimensions").downField("width").as[Int].toOption match {
case Some(width) => width < 990
case None => false
}
}
}
Comment thread
gunnarvelle marked this conversation as resolved.

if (
!(
matchesIdCondition || matchesTitleFilter || hasSmallImage
)
) {
// No conditions met, return 0
return 0
}

// At least one condition met, set inactive to true
val newDocument = oldDocument.mapObject(_.add("inactive", Json.fromBoolean(true)))
Comment thread
gunnarvelle marked this conversation as resolved.

val dataObject = new PGobject()
dataObject.setType("jsonb")
dataObject.setValue(newDocument.noSpaces)

val columnNameSQL = SQLSyntax.createUnsafely(columnName)
val tableNameSQL = SQLSyntax.createUnsafely(tableName)

sql"""update $tableNameSQL
set $columnNameSQL = $dataObject
where id = ${rowData.id}
""".update()
}

}
Loading