From 38161f05ac3291f12a0119147c623d668b1a63b5 Mon Sep 17 00:00:00 2001 From: huyuehua <690534687@qq.com> Date: Tue, 1 Nov 2022 14:50:27 +0800 Subject: [PATCH] =?UTF-8?q?fix:=EF=BC=88=E5=90=8C=E6=AD=A5=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=EF=BC=8C=E4=B8=8D=E5=AD=98=E5=9C=A8=E6=94=B9=E5=8F=98?= =?UTF-8?q?=EF=BC=89=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E5=AD=97=E6=AE=B5=E6=B3=A8=E8=A7=A3=E9=83=BD=E4=B8=8D?= =?UTF-8?q?=E8=83=BD=E4=BF=AE=E6=94=B9=20https://gitee.com/zhijiantianya/r?= =?UTF-8?q?uoyi-vue-pro/issues/I5WP5M?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/codegen/CodegenServiceImpl.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenServiceImpl.java index b2c1998c8..d531d237b 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenServiceImpl.java @@ -28,6 +28,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.function.BiPredicate; import java.util.stream.Collectors; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; @@ -154,12 +155,27 @@ public class CodegenServiceImpl implements CodegenService { // 构建 CodegenColumnDO 数组,只同步新增的字段 List codegenColumns = codegenColumnMapper.selectListByTableId(tableId); Set codegenColumnNames = CollectionUtils.convertSet(codegenColumns, CodegenColumnDO::getColumnName); + + //计算需要修改的字段,插入时重新插入,删除时将原来的删除 + BiPredicate pr = + (tableField, codegenColumn) -> tableField.getType().equals(codegenColumn.getDataType()) + && tableField.getMetaInfo().isNullable() == codegenColumn.getNullable() + && tableField.isKeyFlag() == codegenColumn.getPrimaryKey() + && tableField.getComment().equals(codegenColumn.getColumnComment()); + Map codegenColumnDOMap = CollectionUtils.convertMap(codegenColumns, CodegenColumnDO::getColumnName); + //需要修改的字段 + Set modifyFieldNames = tableFields.stream() + .filter(tableField -> codegenColumnDOMap.get(tableField.getColumnName()) != null + && !pr.test(tableField, codegenColumnDOMap.get(tableField.getColumnName()))) + .map(TableField::getColumnName) + .collect(Collectors.toSet()); // 计算需要删除的字段 Set tableFieldNames = CollectionUtils.convertSet(tableFields, TableField::getName); - Set deleteColumnIds = codegenColumns.stream().filter(column -> !tableFieldNames.contains(column.getColumnName())) + Set deleteColumnIds = codegenColumns.stream() + .filter(column -> (!tableFieldNames.contains(column.getColumnName())) || modifyFieldNames.contains(column.getColumnName())) .map(CodegenColumnDO::getId).collect(Collectors.toSet()); // 移除已经存在的字段 - tableFields.removeIf(column -> codegenColumnNames.contains(column.getColumnName())); + tableFields.removeIf(column -> codegenColumnNames.contains(column.getColumnName()) && (!modifyFieldNames.contains(column.getColumnName()))); if (CollUtil.isEmpty(tableFields) && CollUtil.isEmpty(deleteColumnIds)) { throw exception(CODEGEN_SYNC_NONE_CHANGE); }