-
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.
Merge pull request #557 from NDLANO/migrate-resources-urls
Add migration to convert resource paths
- Loading branch information
Showing
6 changed files
with
126 additions
and
34 deletions.
There are no files selected for viewing
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
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
42 changes: 42 additions & 0 deletions
42
...main/scala/no/ndla/myndlaapi/db/migrationwithdependencies/V16__MigrateResourcePaths.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,42 @@ | ||
/* | ||
* Part of NDLA myndla-api | ||
* Copyright (C) 2024 NDLA | ||
* | ||
* See LICENSE | ||
*/ | ||
|
||
package no.ndla.myndlaapi.db.migrationwithdependencies | ||
|
||
import no.ndla.database.TableMigration | ||
import no.ndla.myndlaapi.integration.TaxonomyApiClient | ||
import no.ndla.network.NdlaClient | ||
import scalikejdbc.{DBSession, WrappedResultSet, scalikejdbcSQLInterpolationImplicitDef} | ||
|
||
import java.util.UUID | ||
|
||
trait V16__MigrateResourcePaths { | ||
this: TaxonomyApiClient & NdlaClient => | ||
|
||
class V16__MigrateResourcePaths extends TableMigration[ResourceRow] { | ||
override val tableName: String = "resources" | ||
override val whereClause: scalikejdbc.SQLSyntax = sqls"path is not null" | ||
override val chunkSize: Int = 1000 | ||
override def extractRowData(rs: WrappedResultSet): ResourceRow = ResourceRow( | ||
UUID.fromString(rs.string("id")), | ||
rs.string("resource_type"), | ||
rs.string("path") | ||
) | ||
override def updateRow(rowData: ResourceRow)(implicit session: DBSession): Int = { | ||
rowData.resourceType match { | ||
case "article" | "learningpath" | "multidisciplinary" | "topic" => | ||
taxonomyApiClient | ||
.resolveUrl(rowData.path) | ||
.map { path => sql"update resources set path=$path where id = ${rowData.id}".update() } | ||
.get | ||
case _ => 0 | ||
} | ||
} | ||
} | ||
} | ||
|
||
case class ResourceRow(id: UUID, resourceType: String, path: String) |
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
38 changes: 38 additions & 0 deletions
38
myndla-api/src/main/scala/no/ndla/myndlaapi/integration/TaxonomyApiClient.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,38 @@ | ||
/* | ||
* Part of NDLA myndla-api | ||
* Copyright (C) 2024 NDLA | ||
* | ||
* See LICENSE | ||
*/ | ||
|
||
package no.ndla.myndlaapi.integration | ||
|
||
import com.typesafe.scalalogging.StrictLogging | ||
import io.circe.{Decoder, Encoder} | ||
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder} | ||
import no.ndla.myndlaapi.Props | ||
import no.ndla.network.NdlaClient | ||
import sttp.client3.quick.* | ||
|
||
import scala.util.{Success, Try} | ||
|
||
trait TaxonomyApiClient { | ||
this: NdlaClient & Props => | ||
|
||
val taxonomyApiClient: TaxonomyApiClient | ||
|
||
class TaxonomyApiClient extends StrictLogging { | ||
private val resolveEndpoint = s"${props.TaxonomyUrl}/v1/url/resolve" | ||
|
||
def resolveUrl(path: String): Try[String] = { | ||
val req = quickRequest.get(uri"$resolveEndpoint?path=$path") | ||
ndlaClient.fetch[ResolvePathResponse](req).map(resolved => resolved.url).orElse(Success(path)) | ||
} | ||
} | ||
} | ||
|
||
case class ResolvePathResponse(url: String) | ||
object ResolvePathResponse { | ||
implicit def decoder: Decoder[ResolvePathResponse] = deriveDecoder | ||
implicit def encoder: Encoder[ResolvePathResponse] = deriveEncoder | ||
} |
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