Skip to content

Commit

Permalink
Remove remaining run table db triggers
Browse files Browse the repository at this point in the history
Propagate run description and access changes to
the corresponding datasets without db triggers.

Removed after_run_update_non_data trigger and
related function.

Removed delete_run_validations as we are not
even exposing Run deletion.

Signed-off-by: Andrea Lamparelli <a.lamparelli95@gmail.com>
  • Loading branch information
lampajr committed Jan 8, 2025
1 parent eb8d52a commit 714223f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,13 @@ public JsonNode getMetadata(int id, String schemaUri) {
@Override
// TODO: it would be nicer to use @FormParams but fetchival on client side doesn't support that
public void updateAccess(int id, String owner, Access access) {
Query query = em.createNativeQuery(CHANGE_ACCESS);
query.setParameter(1, owner);
query.setParameter(2, access.ordinal());
query.setParameter(3, id);
if (query.executeUpdate() != 1) {
int updatedRecords = RunDAO.update("owner = ?1, access = ?2 WHERE id = ?3", owner, access, id);
if (updatedRecords != 1) {
throw ServiceException.serverError("Access change failed (missing permissions?)");
}

// propagate the same change to all datasets belonging to the run
DatasetDAO.update("owner = ?1, access = ?2 WHERE run.id = ?3", owner, access, id);
}

@RolesAllowed(Roles.UPLOADER)
Expand Down Expand Up @@ -1058,7 +1058,8 @@ public void updateDescription(int id, String description) {
throw ServiceException.notFound("Run not found: " + id);
}
run.description = description;
run.persistAndFlush();
// propagate the same change to all datasets belonging to the run
DatasetDAO.update("description = ?1 WHERE run.id = ?2", description, run.id);
}

@RolesAllowed(Roles.TESTER)
Expand Down
12 changes: 12 additions & 0 deletions horreum-backend/src/main/resources/db/changeLog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4742,4 +4742,16 @@
DROP FUNCTION ds_after_dataset_insert_func;
</sql>
</changeSet>
<changeSet id="128" author="lampajr">
<validCheckSum>ANY</validCheckSum>
<sql>
-- drop triggers
DROP TRIGGER IF EXISTS after_run_update_non_data ON run;
DROP TRIGGER IF EXISTS delete_run_validations ON run;

-- drop functions
DROP FUNCTION after_run_update_non_data_func;
DROP FUNCTION delete_run_validations;
</sql>
</changeSet>
</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ public void runExperiment() throws InterruptedException {
List<ExperimentService.ExperimentResult> experimentResults = runExperiments(maxDataset);

assertNotNull(experimentResults);
assertTrue(experimentResults.size() > 0);
assertFalse(experimentResults.isEmpty());

jsonRequest().auth().oauth2(getTesterToken())
.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
Expand Down Expand Up @@ -1232,6 +1232,105 @@ public void testAllRunsOrdering() throws IOException {
assertTrue(runs.runs.get(0).start.isBefore(runs.runs.get(1).start));
}

@org.junit.jupiter.api.Test
public void testDatasetsCreation() {
String name = "with_meta";
Test test = createTest(createExampleTest(name));
createSchema("Foo", "urn:foo");
createSchema("Bar", "urn:bar");

long now = System.currentTimeMillis();
ObjectNode data = simpleObject("urn:foo", "foo", "xxx");
ArrayNode metadata = JsonNodeFactory.instance.arrayNode();
metadata.add(simpleObject("urn:bar", "bar", "yyy"));

assertEquals(0, DatasetDAO.count());

uploadRun(now, data, metadata, test.name);
now = System.currentTimeMillis();
uploadRun(now, data, metadata, test.name);

assertEquals(2, RunDAO.count());
// datasets properly created
assertEquals(2, DatasetDAO.count());
}

@org.junit.jupiter.api.Test
public void testUpdateDescriptionPropagation() {
String name = "with_meta";
Test test = createTest(createExampleTest(name));
createSchema("Foo", "urn:foo");
createSchema("Bar", "urn:bar");

long now = System.currentTimeMillis();
ObjectNode data = simpleObject("urn:foo", "foo", "xxx");
ArrayNode metadata = JsonNodeFactory.instance.arrayNode();
metadata.add(simpleObject("urn:bar", "bar", "yyy"));

assertEquals(0, DatasetDAO.count());

int firstId = uploadRun(now, data, metadata, test.name);
now = System.currentTimeMillis();
uploadRun(now, data, metadata, test.name);

assertEquals(2, RunDAO.count());
// datasets properly created
assertEquals(2, DatasetDAO.count());

// update the description
jsonRequest().auth().oauth2(getTesterToken())
.header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN)
.body("a new description")
.post("/api/run/" + firstId + "/description")
.then()
.statusCode(204);

RunDAO run = RunDAO.findById(firstId);
assertNotNull(run);
assertEquals("a new description", run.description);

DatasetDAO ds = DatasetDAO.find("run.id = ?1", firstId).firstResult();
assertNotNull(ds);
assertEquals("a new description", ds.description);
}

@org.junit.jupiter.api.Test
public void testUpdateAccessPropagation() {
String name = "with_meta";
Test test = createTest(createExampleTest(name));
createSchema("Foo", "urn:foo");
createSchema("Bar", "urn:bar");

long now = System.currentTimeMillis();
ObjectNode data = simpleObject("urn:foo", "foo", "xxx");
ArrayNode metadata = JsonNodeFactory.instance.arrayNode();
metadata.add(simpleObject("urn:bar", "bar", "yyy"));

assertEquals(0, DatasetDAO.count());

int firstId = uploadRun(now, data, metadata, test.name);
now = System.currentTimeMillis();
uploadRun(now, data, metadata, test.name);

assertEquals(2, RunDAO.count());
// datasets properly created
assertEquals(2, DatasetDAO.count());

jsonRequest().auth().oauth2(getTesterToken())
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
.post("/api/run/" + firstId + "/updateAccess?owner=" + UPLOADER_ROLES[0] + "&access=" + Access.PROTECTED)
.then()
.statusCode(204);

RunDAO run = RunDAO.findById(firstId);
assertNotNull(run);
assertEquals(Access.PROTECTED, run.access);

DatasetDAO ds = DatasetDAO.find("run.id = ?1", firstId).firstResult();
assertNotNull(ds);
assertEquals(Access.PROTECTED, ds.access);
}

private JsonNode getBySchema(JsonNode data, String schema) {
JsonNode foo = StreamSupport.stream(data.spliterator(), false)
.filter(item -> schema.equals(item.path("$schema").asText())).findFirst().orElse(null);
Expand Down

0 comments on commit 714223f

Please sign in to comment.