-
Notifications
You must be signed in to change notification settings - Fork 0
Add published count #856
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
base: master
Are you sure you want to change the base?
Add published count #856
Changes from all commits
1e711ec
197a458
1bc1b3d
3432770
520c6a1
621ef43
977f288
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 😄
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
| } | ||
| } | ||
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.
Kanskje lurt med noen tester på disse migreringene?
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.
Migreringene er avhengig av nye spørringer, så blir fort litt komplekst. Men kan sikkert mocke noe her. Ser på det.