Commit 9a573f19 authored by Matija Obreza's avatar Matija Obreza
Browse files

Descriptor referenced by Dataset cannot be deleted

- Remove problematic DatasetDescriptors (with min(position) > 0)
parent 6656119c
......@@ -32,6 +32,7 @@ import javax.persistence.Lob;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.OrderColumn;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
......
......@@ -216,9 +216,7 @@ public class Descriptor extends UuidModel implements SelfCleaning, Publishable,
private List<DescriptorList> descriptorLists;
/** The datasets. */
@ManyToMany(fetch = FetchType.LAZY, cascade = {})
@JoinTable(name = "datasetDescriptor", joinColumns = @JoinColumn(name = "descriptorId", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "datasetId", referencedColumnName = "id"))
// @OrderColumn(name = "position")
@ManyToMany(fetch = FetchType.LAZY, cascade = {}, mappedBy = "descriptors")
@JsonIgnore
private List<Dataset> datasets;
......
......@@ -254,6 +254,9 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(savedDataset.getDescriptors(), not(nullValue()));
assertThat(savedDataset.getDescriptors().contains(savedDescriptor), is(true));
// for {@link #testCannotDeleteDescriptor()}
savedDataset = datasetService.publishDataset(savedDataset);
}
@Test
......@@ -276,6 +279,23 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(savedDataset.getDescriptors(), contains(descriptor2));
}
@Test(expected = DataIntegrityViolationException.class)
public void testCannotDeleteDescriptor() {
testAddDescriptorToDataset();
DatasetFilter filter = new DatasetFilter();
Page<Dataset> loaded = datasetService.listDatasets(filter, new PageRequest(0, 1));
assertThat(loaded.getContent(), hasSize(1));
Dataset savedDataset = loaded.getContent().get(0);
savedDataset = datasetService.loadDataset(savedDataset);
Descriptor referencedDescriptor = savedDataset.getDescriptors().get(0);
assertThat(referencedDescriptor, not(nullValue()));
assertThat(referencedDescriptor.getId(), not(nullValue()));
descriptorService.removeDescriptor(referencedDescriptor);
}
@Test
public void testReorderDescriptors() {
......
......@@ -3879,3 +3879,13 @@ databaseChangeLog:
- sql:
sql: >-
UPDATE short_filter SET code = '' WHERE json = '{}';
- changeSet:
id: 1530205849000-1
author: mobreza
comment: Unpublish datasets that have missing descriptors and remove their descriptors
changes:
- sql:
sql: >-
update dataset set published = 0 where id in (select X.dataset_id from (select dataset_id from dataset_descriptor group by dataset_id having min(position)>0) X);
delete from dataset_descriptor where dataset_id in (select X.dataset_id from (select dataset_id from dataset_descriptor group by dataset_id having min(position)>0) X);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment