SEBSERV-414 fix migration and DB consistency check for Orientations
This commit is contained in:
parent
d3cbc18a09
commit
0fd710c7e3
3 changed files with 43 additions and 20 deletions
|
@ -8,14 +8,11 @@
|
||||||
|
|
||||||
package ch.ethz.seb.sebserver.gui.service.examconfig.impl;
|
package ch.ethz.seb.sebserver.gui.service.examconfig.impl;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.*;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import ch.ethz.seb.sebserver.webservice.datalayer.batis.model.OrientationRecord;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -54,34 +51,41 @@ public class AttributeMapping {
|
||||||
.stream()
|
.stream()
|
||||||
.collect(Collectors.toMap(
|
.collect(Collectors.toMap(
|
||||||
o -> o.attributeId,
|
o -> o.attributeId,
|
||||||
Function.identity())));
|
Function.identity(),
|
||||||
|
(first, second) -> {
|
||||||
|
log.warn("*** Found duplicate orientation, use {} instead of {}", second, first);
|
||||||
|
return second;
|
||||||
|
})));
|
||||||
this.attributeIdMapping = Utils.immutableMapOf(attributes
|
this.attributeIdMapping = Utils.immutableMapOf(attributes
|
||||||
.stream()
|
.stream()
|
||||||
.filter(attr -> this.orientationAttributeMapping.containsKey(attr.id))
|
.filter(attr -> this.orientationAttributeMapping.containsKey(attr.id))
|
||||||
.collect(Collectors.toMap(
|
.collect(Collectors.toMap(
|
||||||
attr -> attr.id,
|
attr -> attr.id,
|
||||||
Function.identity())));
|
Function.identity(),
|
||||||
|
(first, second) -> second)));
|
||||||
|
|
||||||
this.attributeNameIdMapping = Utils.immutableMapOf(attributes
|
this.attributeNameIdMapping = Utils.immutableMapOf(attributes
|
||||||
.stream()
|
.stream()
|
||||||
.filter(attr -> this.orientationAttributeMapping.containsKey(attr.id))
|
.filter(attr -> this.orientationAttributeMapping.containsKey(attr.id))
|
||||||
.collect(Collectors.toMap(
|
.collect(Collectors.toMap(
|
||||||
attr -> attr.name,
|
attr -> attr.name,
|
||||||
attr -> attr.id)));
|
attr -> attr.id,
|
||||||
|
(first, second) -> second)));
|
||||||
|
|
||||||
this.orientationAttributeNameMapping = Utils.immutableMapOf(orientations
|
this.orientationAttributeNameMapping = Utils.immutableMapOf(orientations
|
||||||
.stream()
|
.stream()
|
||||||
.collect(Collectors.toMap(
|
.collect(Collectors.toMap(
|
||||||
o -> this.attributeIdMapping.get(o.attributeId).name,
|
o -> this.attributeIdMapping.get(o.attributeId).name,
|
||||||
Function.identity())));
|
Function.identity(),
|
||||||
|
(first, second) -> second)));
|
||||||
|
|
||||||
this.childAttributeMapping = Utils.immutableMapOf(attributes
|
this.childAttributeMapping = Utils.immutableMapOf(attributes
|
||||||
.stream()
|
.stream()
|
||||||
.filter(attr -> this.orientationAttributeMapping.containsKey(attr.id))
|
.filter(attr -> this.orientationAttributeMapping.containsKey(attr.id))
|
||||||
.collect(Collectors.toMap(
|
.collect(Collectors.toMap(
|
||||||
attr -> attr.id,
|
attr -> attr.id,
|
||||||
this::getChildAttributes)));
|
this::getChildAttributes,
|
||||||
|
(first, second) -> second)));
|
||||||
|
|
||||||
this.attributeGroupMapping = Utils.immutableMapOf(orientations
|
this.attributeGroupMapping = Utils.immutableMapOf(orientations
|
||||||
.stream()
|
.stream()
|
||||||
|
@ -91,7 +95,8 @@ public class AttributeMapping {
|
||||||
.stream()
|
.stream()
|
||||||
.collect(Collectors.toMap(
|
.collect(Collectors.toMap(
|
||||||
Function.identity(),
|
Function.identity(),
|
||||||
this::getAttributesOfGroup)));
|
this::getAttributesOfGroup,
|
||||||
|
(first, second) -> second)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Collection<ConfigurationAttribute> getAttributes() {
|
public Collection<ConfigurationAttribute> getAttributes() {
|
||||||
|
|
|
@ -8,9 +8,7 @@
|
||||||
|
|
||||||
package ch.ethz.seb.sebserver.webservice.datalayer.checks;
|
package ch.ethz.seb.sebserver.webservice.datalayer.checks;
|
||||||
|
|
||||||
import java.util.HashSet;
|
import java.util.*;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.mybatis.dynamic.sql.SqlBuilder;
|
import org.mybatis.dynamic.sql.SqlBuilder;
|
||||||
|
@ -68,7 +66,8 @@ public class OrientationTableDuplicatesCheck implements DBIntegrityCheck {
|
||||||
|
|
||||||
final List<Long> checkedToDelete = toDelete
|
final List<Long> checkedToDelete = toDelete
|
||||||
.stream()
|
.stream()
|
||||||
.filter(this::doubleCheck)
|
.map(this::getOldestForDeletion)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
if (checkedToDelete == null || checkedToDelete.isEmpty()) {
|
if (checkedToDelete == null || checkedToDelete.isEmpty()) {
|
||||||
|
@ -88,21 +87,38 @@ public class OrientationTableDuplicatesCheck implements DBIntegrityCheck {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean doubleCheck(final Long id) {
|
private Long getOldestForDeletion(final Long id) {
|
||||||
try {
|
try {
|
||||||
final OrientationRecord selectByPrimaryKey = this.orientationRecordMapper.selectByPrimaryKey(id);
|
final OrientationRecord selectByPrimaryKey = this.orientationRecordMapper.selectByPrimaryKey(id);
|
||||||
final Long count = this.orientationRecordMapper.countByExample()
|
final List<OrientationRecord> records = this.orientationRecordMapper.selectByExample()
|
||||||
.where(
|
.where(
|
||||||
OrientationRecordDynamicSqlSupport.configAttributeId,
|
OrientationRecordDynamicSqlSupport.configAttributeId,
|
||||||
SqlBuilder.isEqualTo(selectByPrimaryKey.getConfigAttributeId()))
|
SqlBuilder.isEqualTo(selectByPrimaryKey.getConfigAttributeId()))
|
||||||
.and(
|
.and(
|
||||||
OrientationRecordDynamicSqlSupport.templateId,
|
OrientationRecordDynamicSqlSupport.templateId,
|
||||||
SqlBuilder.isEqualTo(selectByPrimaryKey.getTemplateId()))
|
SqlBuilder.isEqualTo(selectByPrimaryKey.getTemplateId()))
|
||||||
|
.orderBy(OrientationRecordDynamicSqlSupport.id)
|
||||||
.build()
|
.build()
|
||||||
.execute();
|
.execute();
|
||||||
return count != null && count.longValue() > 1;
|
|
||||||
|
// get latest entry of duplicates
|
||||||
|
if (records != null && records.size() > 1) {
|
||||||
|
Long result = null;
|
||||||
|
for (int i = 0; i < records.size(); i++) {
|
||||||
|
final OrientationRecord rec = records.get(i);
|
||||||
|
if (result == null) {
|
||||||
|
result = rec.getId();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (result > rec.getId()) {
|
||||||
|
result = rec.getId();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
} catch (final Exception e) {
|
} catch (final Exception e) {
|
||||||
return false;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,8 @@ sebserver.webservice.distributed.updateInterval=1000
|
||||||
sebserver.webservice.distributed.connectionUpdate=2000
|
sebserver.webservice.distributed.connectionUpdate=2000
|
||||||
sebserver.webservice.clean-db-on-startup=false
|
sebserver.webservice.clean-db-on-startup=false
|
||||||
|
|
||||||
|
sebserver.init.database.integrity.try-fix=true
|
||||||
|
|
||||||
# webservice setup configuration
|
# webservice setup configuration
|
||||||
sebserver.init.adminaccount.gen-on-init=false
|
sebserver.init.adminaccount.gen-on-init=false
|
||||||
sebserver.webservice.light.setup=false
|
sebserver.webservice.light.setup=false
|
||||||
|
|
Loading…
Reference in a new issue