Skip to content

Commit

Permalink
Merge pull request #233 from NDLANO/retrofit-database-migrations
Browse files Browse the repository at this point in the history
Retrofit database migrations
  • Loading branch information
gunnarvelle authored Jan 11, 2024
2 parents 661560f + e99ba88 commit b2a76cb
Show file tree
Hide file tree
Showing 17 changed files with 178 additions and 36 deletions.
12 changes: 11 additions & 1 deletion src/main/resources/db-master-changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1186,10 +1186,20 @@
</changeSet>

<changeSet id="20230208 Move translations to separate column for relevances" author="Jonas Natten">
<validCheckSum>8:895d7f163967dd6d6f3f65fb2547addc</validCheckSum>
<addColumn tableName="relevance">
<column name="translations" type="jsonb" />
</addColumn>
<!-- There are no translations in the database for now, so lets not bother with a migration that does nothing -->
<sql>
UPDATE relevance r
SET translations=(
SELECT json_agg(trans) FROM (
SELECT ((js->'f1')::jsonb-'id'-'relevance_id'-'language_code'|| jsonb_build_object('languageCode', js->'f1'->'language_code')) trans FROM (SELECT row_to_json(row(rt)) AS js FROM relevance tr
INNER JOIN relevance_translation rt ON rt.relevance_id = tr.id
WHERE rt.id = r.id) AS _
) AS _
)
</sql>
<sql>
UPDATE relevance
SET translations='[]'
Expand Down
153 changes: 153 additions & 0 deletions src/main/resources/db-migrate-from-flyway.xml

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/test/java/no/ndla/taxonomy/domain/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ public Relevance relevance(String key, Consumer<RelevanceBuilder> consumer) {
}

public Relevance core() {
return relevance("core", f -> f.publicId("urn:relevance:core").name("Core"));
return entityManager
.createQuery("select r from Relevance r where r.publicId = :publicId", Relevance.class)
.setParameter("publicId", URI.create("urn:relevance:core"))
.getSingleResult();
}

private VersionBuilder getVersionBuilder(String key) {
Expand Down Expand Up @@ -470,7 +473,7 @@ public NodeBuilder resource(Node resource) {
}

public NodeBuilder resource(Node resource, boolean primary) {
entityManager.persist(NodeConnection.create(node, resource, relevance(), primary));
entityManager.persist(NodeConnection.create(node, resource, core(), primary));

contextUpdaterService.updateContexts(resource);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class NodeConnectionsTest extends RestTest {
public void add_core_relevance() {
nodeRepository.deleteAllAndFlush();
nodeConnectionRepository.deleteAllAndFlush();

builder.core();
}

@Test
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/no/ndla/taxonomy/rest/v1/NodeResourcesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ public class NodeResourcesTest extends RestTest {
public void add_core_relevance() {
nodeRepository.deleteAllAndFlush();
nodeConnectionRepository.deleteAllAndFlush();

builder.core();
}

@Test
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/no/ndla/taxonomy/rest/v1/NodesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ void clearAllRepos() {
nodeConnectionRepository.deleteAllAndFlush();
resourceTypeRepository.deleteAllAndFlush();
resourceResourceTypeRepository.deleteAllAndFlush();

builder.core();
}

@Test
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/no/ndla/taxonomy/rest/v1/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public class QueryTest extends RestTest {
public void add_core_relevance() {
nodeRepository.deleteAllAndFlush();
nodeConnectionRepository.deleteAllAndFlush();

builder.core();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public class RelevanceTranslationsTest extends RestTest {

@Test
public void can_get_all_relevances() throws Exception {
builder.relevance(r -> r.name("Core").translation("Kjernestoff", "nb"));
builder.relevance(r -> r.name("Supplementary").translation("Tilleggstoff", "nb"));

MockHttpServletResponse response = testUtils.getResource("/v1/relevances?language=nb");
RelevanceDTO[] relevances = testUtils.getObject(RelevanceDTO[].class, response);

Expand Down
3 changes: 1 addition & 2 deletions src/test/java/no/ndla/taxonomy/rest/v1/RelevancesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ public void can_get_a_single_relevance() throws Exception {
@Test
public void can_get_all_relevances() throws Exception {
builder.relevance(f -> f.publicId("urn:relevance:1").name("Core material"));

builder.relevance(f -> f.publicId("urn:relevance:2").name("Supplementary material"));

MockHttpServletResponse response = testUtils.getResource("/v1/relevances");
RelevanceDTO[] relevances = testUtils.getObject(RelevanceDTO[].class, response);

assertEquals(2, relevances.length);
assertEquals(4, relevances.length);
assertAnyTrue(relevances, f -> f.name.equals("Core material"));
assertAnyTrue(relevances, f -> f.name.equals("Supplementary material"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

import static no.ndla.taxonomy.TestUtils.assertAnyTrue;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.*;

import java.net.URI;
import no.ndla.taxonomy.domain.ResourceType;
Expand All @@ -30,7 +29,7 @@ public void can_get_all_resource_types() throws Exception {
MockHttpServletResponse response = testUtils.getResource("/v1/resource-types?language=nb");
ResourceTypeDTO[] resourceTypes = testUtils.getObject(ResourceTypeDTO[].class, response);

assertEquals(2, resourceTypes.length);
assertTrue(resourceTypes.length >= 2);
assertAnyTrue(resourceTypes, s -> s.name.equals("Artikkel"));
assertAnyTrue(resourceTypes, s -> s.name.equals("Forelesning"));
}
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/no/ndla/taxonomy/rest/v1/ResourceTypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public void can_get_all_resource_types() throws Exception {
builder.resourceType(rt -> rt.name("audio"));

MockHttpServletResponse response = testUtils.getResource("/v1/resource-types");
ResourceTypeDTO[] resourcetypes = testUtils.getObject(ResourceTypeDTO[].class, response);
ResourceTypeDTO[] resourceTypes = testUtils.getObject(ResourceTypeDTO[].class, response);

assertEquals(2, resourcetypes.length);
assertTrue(resourceTypes.length >= 2);
assertAnyTrue(
resourcetypes,
resourceTypes,
s -> "video".equals(s.name) && s.subtypes.get(0).name.equals("lecture"));
assertAnyTrue(resourcetypes, s -> "audio".equals(s.name));
assertAllTrue(resourcetypes, s -> isValidId(s.id));
assertAnyTrue(resourceTypes, s -> "audio".equals(s.name));
assertAllTrue(resourceTypes, s -> isValidId(s.id));
}

@Test
Expand All @@ -54,7 +54,7 @@ public void can_get_all_resource_types_with_translation() throws Exception {
MockHttpServletResponse response = testUtils.getResource("/v1/resource-types?language=nb");
ResourceTypeDTO[] resourcetypes = testUtils.getObject(ResourceTypeDTO[].class, response);

assertEquals(2, resourcetypes.length);
assertTrue(resourcetypes.length >= 2);
assertAnyTrue(resourcetypes, s -> "film".equals(s.name));
assertAnyTrue(resourcetypes, s -> "lydklipp".equals(s.name));
}
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/no/ndla/taxonomy/rest/v1/ResourcesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ public class ResourcesTest extends RestTest {
void clearAllRepos() {
nodeRepository.deleteAllAndFlush();
nodeConnectionRepository.deleteAllAndFlush();

builder.core();
}

@Test
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/no/ndla/taxonomy/rest/v1/SubjectTopicsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ public class SubjectTopicsTest extends RestTest {
void clearAllRepos() {
nodeRepository.deleteAllAndFlush();
nodeConnectionRepository.deleteAllAndFlush();

builder.core();
}

@Test
Expand Down
2 changes: 0 additions & 2 deletions src/test/java/no/ndla/taxonomy/rest/v1/SubjectsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public class SubjectsTest extends RestTest {
void clearAllRepos() {
nodeRepository.deleteAllAndFlush();
nodeConnectionRepository.deleteAllAndFlush();

builder.core();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ public class TopicResourcesTest extends RestTest {
public void add_core_relevance() {
nodeRepository.deleteAllAndFlush();
nodeConnectionRepository.deleteAllAndFlush();

builder.core();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ public class TopicSubtopicsTest extends RestTest {
public void add_core_relevance() {
nodeRepository.deleteAllAndFlush();
nodeConnectionRepository.deleteAllAndFlush();

builder.core();
}

@Test
Expand Down
1 change: 0 additions & 1 deletion src/test/java/no/ndla/taxonomy/rest/v1/TopicsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public class TopicsTest extends RestTest {
@BeforeEach
void clearAllRepos() {
nodeRepository.deleteAllAndFlush();
builder.core();
}

@Test
Expand Down

0 comments on commit b2a76cb

Please sign in to comment.