-
Notifications
You must be signed in to change notification settings - Fork 0
Add migration to mark images as inactive #952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gunnarvelle
wants to merge
4
commits into
master
Choose a base branch
from
image-api-inactive-migration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+110
−0
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
109 changes: 109 additions & 0 deletions
109
image-api/src/main/scala/no/ndla/imageapi/db/migration/V27__SetImagesInactive.scala
This file contains hidden or 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,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("/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") | ||
| 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 | ||
| } | ||
| } | ||
| } | ||
|
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))) | ||
|
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() | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 😅
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Falske positiver: https://ed.ndla.no/media/image-upload/71367/edit/nb, https://ed.ndla.no/media/image-upload/47749/edit/nb, https://ed.ndla.no/media/image-upload/47750/edit/nb, https://ed.ndla.no/media/image-upload/47751/edit/nb, https://ed.ndla.no/media/image-upload/47752/edit/nb, https://ed.ndla.no/media/image-upload/47753/edit/nb, https://ed.ndla.no/media/image-upload/48539/edit/nb, https://ed.ndla.no/media/image-upload/48540/edit/nb
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Konstruert falsk positiv https://ed.test.ndla.no/media/image-upload/70091/edit/nb