-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add upgrade plugin which ensures all projects have valid Restric…
…tedView settings persisted
- Loading branch information
Showing
7 changed files
with
200 additions
and
5 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
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
80 changes: 80 additions & 0 deletions
80
...c/main/scala/org/knora/webapi/store/triplestore/upgrade/plugins/UpgradePluginPR3112.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,80 @@ | ||
/* | ||
* Copyright © 2021 - 2024 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.knora.webapi.store.triplestore.upgrade.plugins | ||
|
||
import org.eclipse.rdf4j.sparqlbuilder.core.SparqlBuilder.`var` as variable | ||
import org.eclipse.rdf4j.sparqlbuilder.core.query.ModifyQuery | ||
import org.eclipse.rdf4j.sparqlbuilder.core.query.* | ||
import org.eclipse.rdf4j.sparqlbuilder.rdf.Rdf | ||
|
||
import org.knora.webapi.slice.admin.AdminConstants | ||
import org.knora.webapi.slice.admin.domain.model.RestrictedView.Size | ||
import org.knora.webapi.slice.admin.repo.rdf.Vocabulary | ||
import org.knora.webapi.store.triplestore.upgrade.GraphsForMigration | ||
import org.knora.webapi.store.triplestore.upgrade.MigrateSpecificGraphs | ||
|
||
/** | ||
* Certain restricted views have a watermark that is not a boolean. This plugin removes the invalid watermark triples. | ||
*/ | ||
class UpgradePluginPR3112 extends AbstractSparqlUpdatePlugin { | ||
|
||
override def graphsForMigration: GraphsForMigration = | ||
MigrateSpecificGraphs.from(AdminConstants.adminDataNamedGraph) | ||
Check warning on line 25 in webapi/src/main/scala/org/knora/webapi/store/triplestore/upgrade/plugins/UpgradePluginPR3112.scala Codecov / codecov/patchwebapi/src/main/scala/org/knora/webapi/store/triplestore/upgrade/plugins/UpgradePluginPR3112.scala#L25
|
||
|
||
private val removeWatermarkIfBothSet: ModifyQuery = { | ||
val (project, prevWatermark, prevSize) = (variable("project"), variable("prevWatermark"), variable("prevSize")) | ||
Queries | ||
.MODIFY() | ||
.prefix(Vocabulary.KnoraAdmin.NS) | ||
.delete(project.has(Vocabulary.KnoraAdmin.projectRestrictedViewWatermark, prevWatermark)) | ||
.from(Vocabulary.NamedGraphs.knoraAdminIri) | ||
.where( | ||
project | ||
.isA(Vocabulary.KnoraAdmin.KnoraProject) | ||
.andHas(Vocabulary.KnoraAdmin.projectRestrictedViewWatermark, prevWatermark) | ||
.andHas(Vocabulary.KnoraAdmin.projectRestrictedViewSize, prevSize) | ||
.from(Vocabulary.NamedGraphs.knoraAdminIri), | ||
) | ||
} | ||
|
||
private val addDefaultRestrictedViewSizeToProjectsWithout = { | ||
val project = variable("project") | ||
Queries | ||
.MODIFY() | ||
.prefix(Vocabulary.KnoraAdmin.NS) | ||
.`with`(Vocabulary.NamedGraphs.knoraAdminIri) | ||
.insert(project.has(Vocabulary.KnoraAdmin.projectRestrictedViewSize, Rdf.literalOf(Size.default.value))) | ||
.where( | ||
project | ||
.isA(Vocabulary.KnoraAdmin.KnoraProject) | ||
.filterNotExists(project.has(Vocabulary.KnoraAdmin.projectRestrictedViewSize, variable("size"))) | ||
.filterNotExists(project.has(Vocabulary.KnoraAdmin.projectRestrictedViewWatermark, variable("watermark"))) | ||
.from(Vocabulary.NamedGraphs.knoraAdminIri), | ||
) | ||
} | ||
|
||
private val replaceWatermarkFalseWithDefaultRestrictedViewSize = { | ||
val project = variable("project") | ||
Queries | ||
.MODIFY() | ||
.prefix(Vocabulary.KnoraAdmin.NS) | ||
.`with`(Vocabulary.NamedGraphs.knoraAdminIri) | ||
.insert(project.has(Vocabulary.KnoraAdmin.projectRestrictedViewSize, Rdf.literalOf(Size.default.value))) | ||
.where( | ||
project | ||
.isA(Vocabulary.KnoraAdmin.KnoraProject) | ||
.andHas(Vocabulary.KnoraAdmin.projectRestrictedViewWatermark, Rdf.literalOf(false)) | ||
.filterNotExists(project.has(Vocabulary.KnoraAdmin.projectRestrictedViewSize, variable("size"))) | ||
.from(Vocabulary.NamedGraphs.knoraAdminIri), | ||
) | ||
} | ||
|
||
override def getQueries: List[ModifyQuery] = List( | ||
removeWatermarkIfBothSet, | ||
addDefaultRestrictedViewSizeToProjectsWithout, | ||
replaceWatermarkFalseWithDefaultRestrictedViewSize, | ||
) | ||
} |
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
114 changes: 114 additions & 0 deletions
114
...st/scala/org/knora/webapi/store/triplestore/upgrade/plugins/UpgradePluginPR3112Spec.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,114 @@ | ||
package org.knora.webapi.store.triplestore.upgrade.plugins | ||
|
||
import zio.test.Spec | ||
import zio.test.ZIOSpecDefault | ||
import zio.test.assertTrue | ||
|
||
object UpgradePluginPR3112Spec extends ZIOSpecDefault with UpgradePluginTestOps { | ||
|
||
val spec: Spec[Any, Nothing] = suite("UpgradePluginPR3111")( | ||
test( | ||
"given project with invalid RestrictedView settings, " + | ||
"when the plugin is run, " + | ||
"then the size is retained or a default size is set.", | ||
) { | ||
// given | ||
val plugin = new UpgradePluginPR3112() | ||
val triG = | ||
s""" | ||
|@prefix knora-admin: <http://www.knora.org/ontology/knora-admin#> . | ||
|@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | ||
| | ||
|GRAPH <http://www.knora.org/data/admin> { | ||
| <http://rdfh.ch/projects/0001> a knora-admin:knoraProject ; | ||
| knora-admin:projectRestrictedViewWatermark true ; | ||
| knora-admin:projectRestrictedViewSize "!512,512"^^xsd:string . | ||
| | ||
| <http://rdfh.ch/projects/0002> a knora-admin:knoraProject . | ||
| | ||
| <http://rdfh.ch/projects/0003> a knora-admin:knoraProject ; | ||
| knora-admin:projectRestrictedViewWatermark false. | ||
| | ||
| <http://rdfh.ch/projects/0004> a knora-admin:knoraProject ; | ||
| knora-admin:projectRestrictedViewWatermark true. | ||
| | ||
| <http://rdfh.ch/projects/0005> a knora-admin:knoraProject ; | ||
| knora-admin:projectRestrictedViewSize "!555,555"^^xsd:string . | ||
|} | ||
|""".stripMargin | ||
val model = createJenaModelFromTriG(triG) | ||
|
||
// when | ||
printModel(model) | ||
plugin.transform(model) | ||
printModel(model) | ||
|
||
// then | ||
val project0001HasOnlyViewSize = | ||
""" | ||
|PREFIX knora-admin: <http://www.knora.org/ontology/knora-admin#> | ||
|PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> | ||
| | ||
|ASK { | ||
| GRAPH <http://www.knora.org/data/admin> { | ||
| <http://rdfh.ch/projects/0001> a knora-admin:knoraProject ; | ||
| knora-admin:projectRestrictedViewSize "!512,512"^^xsd:string . | ||
| FILTER NOT EXISTS { <http://rdfh.ch/projects/0001> knora-admin:projectRestrictedViewWatermark ?any . } | ||
| } | ||
|} | ||
|""".stripMargin | ||
|
||
val project0002HasDefaultRestrictedViewSize = | ||
""" | ||
|PREFIX knora-admin: <http://www.knora.org/ontology/knora-admin#> | ||
|PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> | ||
| | ||
|ASK { | ||
| GRAPH <http://www.knora.org/data/admin> { | ||
| <http://rdfh.ch/projects/0002> a knora-admin:knoraProject ; | ||
| knora-admin:projectRestrictedViewSize "!128,128"^^xsd:string . | ||
| FILTER NOT EXISTS { <http://rdfh.ch/projects/0001> knora-admin:projectRestrictedViewWatermark ?any . } | ||
| } | ||
|} | ||
|""".stripMargin | ||
|
||
val watermarkFalseIsReplacedByDefaultRestrictedViewSize = | ||
""" | ||
|PREFIX knora-admin: <http://www.knora.org/ontology/knora-admin#> | ||
|PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> | ||
| | ||
|ASK { | ||
| GRAPH <http://www.knora.org/data/admin> { | ||
| <http://rdfh.ch/projects/0003> a knora-admin:knoraProject ; | ||
| knora-admin:projectRestrictedViewSize "!128,128"^^xsd:string . | ||
| FILTER NOT EXISTS { <http://rdfh.ch/projects/0001> knora-admin:projectRestrictedViewWatermark ?any . } | ||
| } | ||
|} | ||
|""".stripMargin | ||
|
||
val validProjectsRemainUntouched = | ||
""" | ||
|PREFIX knora-admin: <http://www.knora.org/ontology/knora-admin#> | ||
|PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> | ||
| | ||
|ASK { | ||
| GRAPH <http://www.knora.org/data/admin> { | ||
| | ||
| <http://rdfh.ch/projects/0004> a knora-admin:knoraProject ; | ||
| knora-admin:projectRestrictedViewWatermark true. | ||
| | ||
| <http://rdfh.ch/projects/0005> a knora-admin:knoraProject ; | ||
| knora-admin:projectRestrictedViewSize "!555,555"^^xsd:string . | ||
| } | ||
|} | ||
|""".stripMargin | ||
|
||
assertTrue( | ||
queryAsk(project0001HasOnlyViewSize, model), | ||
queryAsk(project0002HasDefaultRestrictedViewSize, model), | ||
queryAsk(watermarkFalseIsReplacedByDefaultRestrictedViewSize, model), | ||
queryAsk(validProjectsRemainUntouched, model), | ||
) | ||
}, | ||
) | ||
} |