From 25cd49639590c076fc619a32ed95f1fbf0ca329a Mon Sep 17 00:00:00 2001 From: vran Date: Fri, 27 May 2022 13:13:23 +0800 Subject: [PATCH] feat: diff result can show removed items --- .../domain/document/diff/DiffTypeFills.java | 108 ++++++++++++++++++ .../document/service/DocumentService.java | 90 ++------------- 2 files changed, 118 insertions(+), 80 deletions(-) create mode 100644 core/src/main/java/com/databasir/core/domain/document/diff/DiffTypeFills.java diff --git a/core/src/main/java/com/databasir/core/domain/document/diff/DiffTypeFills.java b/core/src/main/java/com/databasir/core/domain/document/diff/DiffTypeFills.java new file mode 100644 index 0000000..073efdd --- /dev/null +++ b/core/src/main/java/com/databasir/core/domain/document/diff/DiffTypeFills.java @@ -0,0 +1,108 @@ +package com.databasir.core.domain.document.diff; + +import com.databasir.core.diff.data.DiffType; +import com.databasir.core.domain.document.comparator.DiffResult; +import com.databasir.core.domain.document.comparator.TableDiffResult; +import com.databasir.core.domain.document.data.DiffAble; +import com.databasir.core.domain.document.data.TableDocumentResponse; +import com.databasir.core.domain.document.data.TableDocumentResponse.ForeignKeyDocumentResponse; + +import java.util.Collection; +import java.util.List; +import java.util.function.Function; +import java.util.stream.Collectors; + +import static java.util.Collections.emptyList; + +public class DiffTypeFills { + + public static TableDocumentResponse fillRemoved(TableDocumentResponse data) { + data.setDiffType(DiffType.REMOVED); + data.getColumns().forEach(col -> col.setDiffType(DiffType.REMOVED)); + data.getForeignKeys().forEach(col -> col.setDiffType(DiffType.REMOVED)); + data.getIndexes().forEach(col -> col.setDiffType(DiffType.REMOVED)); + data.getTriggers().forEach(col -> col.setDiffType(DiffType.REMOVED)); + return data; + } + + public static TableDocumentResponse fillAdded(TableDocumentResponse data, TableDiffResult diff) { + data.setDiffType(DiffType.ADDED); + var cols = + diff(diff.getColumnDiffResults(), emptyList(), data.getColumns(), i -> i.getName()); + data.setColumns(cols); + var indexes = + diff(diff.getIndexDiffResults(), emptyList(), data.getIndexes(), i -> i.getName()); + data.setIndexes(indexes); + var foreignKeys = foreignKeyDiff(diff.getForeignKeyDiffResults(), + emptyList(), data.getForeignKeys()); + data.setForeignKeys(foreignKeys); + var triggers = + diff(diff.getTriggerDiffResults(), emptyList(), data.getTriggers(), t -> t.getName()); + data.setTriggers(triggers); + return data; + } + + public static TableDocumentResponse fillModified(TableDocumentResponse current, + TableDocumentResponse original, + TableDiffResult diff) { + current.setDiffType(DiffType.MODIFIED); + current.setOriginal(original); + var cols = + diff(diff.getColumnDiffResults(), original.getColumns(), current.getColumns(), + col -> col.getName()); + current.setColumns(cols); + var indexes = + diff(diff.getIndexDiffResults(), original.getIndexes(), current.getIndexes(), i -> i.getName()); + current.setIndexes(indexes); + var foreignKeys = foreignKeyDiff(diff.getForeignKeyDiffResults(), + original.getForeignKeys(), current.getForeignKeys()); + current.setForeignKeys(foreignKeys); + var triggers = + diff(diff.getTriggerDiffResults(), original.getTriggers(), current.getTriggers(), + t -> t.getName()); + current.setTriggers(triggers); + return current; + } + + public static List diff(Collection diffs, + Collection original, + Collection current, + Function idMapping) { + var currentMapByName = current.stream() + .collect(Collectors.toMap(idMapping, Function.identity(), (a, b) -> a)); + var originalMapByName = original.stream() + .collect(Collectors.toMap(idMapping, Function.identity(), (a, b) -> a)); + return diffs.stream().map(diff -> { + if (diff.getDiffType() == DiffType.ADDED) { + var t = currentMapByName.get(diff.getId()); + t.setDiffType(DiffType.ADDED); + return t; + } + if (diff.getDiffType() == DiffType.REMOVED) { + var t = originalMapByName.get(diff.getId()); + t.setDiffType(DiffType.REMOVED); + return t; + } + if (diff.getDiffType() == DiffType.MODIFIED) { + var c = currentMapByName.get(diff.getId()); + var o = originalMapByName.get(diff.getId()); + c.setDiffType(DiffType.MODIFIED); + c.setOriginal(o); + return c; + } + var t = currentMapByName.get(diff.getId()); + t.setDiffType(DiffType.NONE); + return t; + }) + .collect(Collectors.toList()); + } + + public static List foreignKeyDiff(Collection diffs, + Collection original, + Collection current) { + Function idMapping = fk -> { + return fk.getFkTableName() + "." + fk.getFkColumnName() + "." + fk.getKeySeq(); + }; + return diff(diffs, original, current, idMapping); + } +} diff --git a/core/src/main/java/com/databasir/core/domain/document/service/DocumentService.java b/core/src/main/java/com/databasir/core/domain/document/service/DocumentService.java index f018eb7..4d8f963 100644 --- a/core/src/main/java/com/databasir/core/domain/document/service/DocumentService.java +++ b/core/src/main/java/com/databasir/core/domain/document/service/DocumentService.java @@ -7,12 +7,11 @@ import com.databasir.core.diff.Diffs; import com.databasir.core.diff.data.DiffType; import com.databasir.core.diff.data.RootDiff; import com.databasir.core.domain.DomainErrors; -import com.databasir.core.domain.document.comparator.DiffResult; import com.databasir.core.domain.document.comparator.DocumentDiffs; import com.databasir.core.domain.document.comparator.TableDiffResult; import com.databasir.core.domain.document.converter.*; import com.databasir.core.domain.document.data.*; -import com.databasir.core.domain.document.data.TableDocumentResponse.ForeignKeyDocumentResponse; +import com.databasir.core.domain.document.diff.DiffTypeFills; import com.databasir.core.domain.document.diff.DiffTypePredictor; import com.databasir.core.domain.document.event.DocumentUpdated; import com.databasir.core.domain.document.generator.DocumentFileGenerator; @@ -28,13 +27,13 @@ import com.databasir.dao.tables.pojos.*; import com.databasir.dao.value.DocumentDiscussionCountPojo; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.collections4.CollectionUtils; import org.jooq.tools.StringUtils; import org.springframework.dao.DuplicateKeyException; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.CollectionUtils; import java.io.OutputStream; import java.sql.Connection; @@ -238,7 +237,7 @@ public class DocumentService { descriptionMapByTableName ); - // if original version is not null mean version diff enabled + // if original version is not null means version diff enabled if (originalVersion != null) { var originalDocument = databaseDocumentDao.selectOptionalByProjectIdAndVersion(projectId, originalVersion) @@ -444,6 +443,8 @@ public class DocumentService { return emptyList(); } var current = this.getTableDetails(projectId, databaseDocumentId, request.getTableIds()); + List currentTableIds = current.stream().map(t -> t.getId()).collect(Collectors.toList()); + var missedIds = CollectionUtils.disjunction(currentTableIds, request.getTableIds()); if (request.getOriginalVersion() != null) { DatabaseDocumentPojo doc = databaseDocumentDao.selectOptionalByProjectIdAndVersion(projectId, request.getOriginalVersion()) @@ -451,7 +452,8 @@ public class DocumentService { List tableNames = current.stream().map(t -> t.getName()).distinct().collect(Collectors.toList()); List originalTableIds = tableDocumentDao.selectTableIdsByDatabaseDocumentIdAndTableNameIn(doc.getId(), tableNames); - var original = this.getTableDetails(projectId, doc.getId(), originalTableIds); + var allOriginalTableIds = CollectionUtils.union(missedIds, originalTableIds); + var original = this.getTableDetails(projectId, doc.getId(), allOriginalTableIds); Map currentMapByName = current.stream() .collect(Collectors.toMap(TableDocumentResponse::getName, Function.identity(), (a, b) -> a)); Map originalMapByName = original.stream() @@ -463,46 +465,16 @@ public class DocumentService { .map(diff -> { if (diff.getDiffType() == DiffType.ADDED) { TableDocumentResponse c = currentMapByName.get(diff.getId()); - c.setDiffType(DiffType.ADDED); - var cols = - diff(diff.getColumnDiffResults(), emptyList(), c.getColumns(), i -> i.getName()); - c.setColumns(cols); - var indexes = - diff(diff.getIndexDiffResults(), emptyList(), c.getIndexes(), i -> i.getName()); - c.setIndexes(indexes); - var foreignKeys = foreignKeyDiff(diff.getForeignKeyDiffResults(), - emptyList(), c.getForeignKeys()); - c.setForeignKeys(foreignKeys); - var triggers = - diff(diff.getTriggerDiffResults(), emptyList(), c.getTriggers(), t -> t.getName()); - c.setTriggers(triggers); - return c; + return DiffTypeFills.fillAdded(c, diff); } if (diff.getDiffType() == DiffType.REMOVED) { TableDocumentResponse t = originalMapByName.get(diff.getId()); - t.setDiffType(DiffType.REMOVED); - return t; + return DiffTypeFills.fillRemoved(t); } if (diff.getDiffType() == DiffType.MODIFIED) { TableDocumentResponse c = currentMapByName.get(diff.getId()); TableDocumentResponse o = originalMapByName.get(diff.getId()); - c.setDiffType(DiffType.MODIFIED); - c.setOriginal(o); - var cols = - diff(diff.getColumnDiffResults(), o.getColumns(), c.getColumns(), - col -> col.getName()); - c.setColumns(cols); - var indexes = - diff(diff.getIndexDiffResults(), o.getIndexes(), c.getIndexes(), i -> i.getName()); - c.setIndexes(indexes); - var foreignKeys = foreignKeyDiff(diff.getForeignKeyDiffResults(), - o.getForeignKeys(), c.getForeignKeys()); - c.setForeignKeys(foreignKeys); - var triggers = - diff(diff.getTriggerDiffResults(), o.getTriggers(), c.getTriggers(), - t -> t.getName()); - c.setTriggers(triggers); - return c; + return DiffTypeFills.fillModified(c, o, diff); } TableDocumentResponse t = currentMapByName.get(diff.getId()); t.setDiffType(DiffType.NONE); @@ -567,46 +539,4 @@ public class DocumentService { return tableResponseConverter.from(tables, columnMapByTableId); } } - - private List diff(Collection diffs, - Collection original, - Collection current, - Function idMapping) { - var currentMapByName = current.stream() - .collect(Collectors.toMap(idMapping, Function.identity(), (a, b) -> a)); - var originalMapByName = original.stream() - .collect(Collectors.toMap(idMapping, Function.identity(), (a, b) -> a)); - return diffs.stream().map(diff -> { - if (diff.getDiffType() == DiffType.ADDED) { - var t = currentMapByName.get(diff.getId()); - t.setDiffType(DiffType.ADDED); - return t; - } - if (diff.getDiffType() == DiffType.REMOVED) { - var t = originalMapByName.get(diff.getId()); - t.setDiffType(DiffType.REMOVED); - return t; - } - if (diff.getDiffType() == DiffType.MODIFIED) { - var c = currentMapByName.get(diff.getId()); - var o = originalMapByName.get(diff.getId()); - c.setDiffType(DiffType.MODIFIED); - c.setOriginal(o); - return c; - } - var t = currentMapByName.get(diff.getId()); - t.setDiffType(DiffType.NONE); - return t; - }) - .collect(Collectors.toList()); - } - - private List foreignKeyDiff(Collection diffs, - Collection original, - Collection current) { - Function idMapping = fk -> { - return fk.getFkTableName() + "." + fk.getFkColumnName() + "." + fk.getKeySeq(); - }; - return diff(diffs, original, current, idMapping); - } }