From 42b7946685bdfb89f94c613e70f890425a94aaf1 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 29 Sep 2024 07:41:35 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=8A=9F=E8=83=BD=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E3=80=91=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90=EF=BC=9A=E4=BB=A3?= =?UTF-8?q?=E5=90=8C=E6=AD=A5=E6=95=B0=E6=8D=AE=E5=BA=93=E7=BB=93=E6=9E=84?= =?UTF-8?q?=E4=B9=8B=E5=90=8E=E5=AD=97=E6=AE=B5=E9=A1=BA=E5=BA=8F=E6=B7=B7?= =?UTF-8?q?=E4=B9=B1=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/test/core/util/RandomUtils.java | 5 +++++ .../mysql/codegen/CodegenColumnMapper.java | 2 +- .../service/codegen/CodegenServiceImpl.java | 20 ++++++++++++----- .../codegen/CodegenServiceImplTest.java | 22 ++++++++++--------- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/util/RandomUtils.java b/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/util/RandomUtils.java index 095269751..1cafbcd45 100644 --- a/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/util/RandomUtils.java +++ b/yudao-framework/yudao-spring-boot-starter-test/src/main/java/cn/iocoder/yudao/framework/test/core/util/RandomUtils.java @@ -134,6 +134,11 @@ public class RandomUtils { @SafeVarargs public static List randomPojoList(Class clazz, Consumer... consumers) { int size = RandomUtil.randomInt(1, RANDOM_COLLECTION_LENGTH); + return randomPojoList(clazz, size, consumers); + } + + @SafeVarargs + public static List randomPojoList(Class clazz, int size, Consumer... consumers) { return Stream.iterate(0, i -> i).limit(size).map(o -> randomPojo(clazz, consumers)) .collect(Collectors.toList()); } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/codegen/CodegenColumnMapper.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/codegen/CodegenColumnMapper.java index 3f1aedb97..ea5a9bb63 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/codegen/CodegenColumnMapper.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/codegen/CodegenColumnMapper.java @@ -13,7 +13,7 @@ public interface CodegenColumnMapper extends BaseMapperX { default List selectListByTableId(Long tableId) { return selectList(new LambdaQueryWrapperX() .eq(CodegenColumnDO::getTableId, tableId) - .orderByAsc(CodegenColumnDO::getId)); + .orderByAsc(CodegenColumnDO::getOrdinalPosition)); } default void deleteListByTableId(Long tableId) { 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 e7067ed37..005eb8a1b 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 @@ -22,13 +22,14 @@ import cn.iocoder.yudao.module.system.api.user.AdminUserApi; import com.baomidou.mybatisplus.generator.config.po.TableField; import com.baomidou.mybatisplus.generator.config.po.TableInfo; import com.google.common.annotations.VisibleForTesting; +import jakarta.annotation.Resource; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import jakarta.annotation.Resource; import java.util.*; import java.util.function.BiPredicate; import java.util.stream.Collectors; +import java.util.stream.IntStream; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; @@ -179,11 +180,18 @@ public class CodegenServiceImpl implements CodegenService { && tableField.getMetaInfo().isNullable() == codegenColumn.getNullable() && tableField.isKeyFlag() == codegenColumn.getPrimaryKey() && tableField.getComment().equals(codegenColumn.getColumnComment()); - Set modifyFieldNames = tableFields.stream() - .filter(tableField -> codegenColumnDOMap.get(tableField.getColumnName()) != null - && !primaryKeyPredicate.test(tableField, codegenColumnDOMap.get(tableField.getColumnName()))) - .map(TableField::getColumnName) - .collect(Collectors.toSet()); + Set modifyFieldNames = IntStream.range(0, tableFields.size()).mapToObj(index -> { + TableField tableField = tableFields.get(index); + String columnName = tableField.getColumnName(); + CodegenColumnDO codegenColumn = codegenColumnDOMap.get(columnName); + if (codegenColumn == null) { + return null; + } + if (!primaryKeyPredicate.test(tableField, codegenColumn) || codegenColumn.getOrdinalPosition() != index) { + return columnName; + } + return null; + }).filter(Objects::nonNull).collect(Collectors.toSet()); // 3.2 计算需要【删除】的字段 Set tableFieldNames = convertSet(tableFields, TableField::getName); Set deleteColumnIds = codegenColumns.stream() diff --git a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenServiceImplTest.java b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenServiceImplTest.java index 6515f46dd..48e0429fb 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenServiceImplTest.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/CodegenServiceImplTest.java @@ -24,12 +24,11 @@ import cn.iocoder.yudao.module.system.api.user.AdminUserApi; import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO; import com.baomidou.mybatisplus.generator.config.po.TableField; import com.baomidou.mybatisplus.generator.config.po.TableInfo; -import org.junit.jupiter.api.Disabled; +import jakarta.annotation.Resource; import org.junit.jupiter.api.Test; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; -import jakarta.annotation.Resource; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -234,17 +233,16 @@ public class CodegenServiceImplTest extends BaseDbUnitTest { } @Test - @Disabled // TODO @芋艿:这个单测会随机性失败,需要定位下; public void testSyncCodegenFromDB() { // mock 数据(CodegenTableDO) CodegenTableDO table = randomPojo(CodegenTableDO.class, o -> o.setTableName("t_yunai") .setDataSourceConfigId(1L).setScene(CodegenSceneEnum.ADMIN.getScene())); codegenTableMapper.insert(table); CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()) - .setColumnName("id")); + .setColumnName("id").setPrimaryKey(true).setOrdinalPosition(0)); codegenColumnMapper.insert(column01); CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()) - .setColumnName("name")); + .setColumnName("name").setOrdinalPosition(1)); codegenColumnMapper.insert(column02); // 准备参数 Long tableId = table.getId(); @@ -263,7 +261,7 @@ public class CodegenServiceImplTest extends BaseDbUnitTest { when(databaseTableService.getTable(eq(1L), eq("t_yunai"))) .thenReturn(tableInfo); // mock 方法(CodegenTableDO) - List newColumns = randomPojoList(CodegenColumnDO.class); + List newColumns = randomPojoList(CodegenColumnDO.class, 2); when(codegenBuilder.buildColumns(eq(table.getId()), argThat(tableFields -> { assertEquals(2, tableFields.size()); assertSame(tableInfo.getFields(), tableFields); @@ -457,9 +455,11 @@ public class CodegenServiceImplTest extends BaseDbUnitTest { .setTemplateType(CodegenTemplateTypeEnum.ONE.getType())); codegenTableMapper.insert(table); // mock 数据(CodegenColumnDO) - CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())); + CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()) + .setOrdinalPosition(1)); codegenColumnMapper.insert(column01); - CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())); + CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()) + .setOrdinalPosition(2)); codegenColumnMapper.insert(column02); // mock 执行生成 Map codes = MapUtil.of(randomString(), randomString()); @@ -486,9 +486,11 @@ public class CodegenServiceImplTest extends BaseDbUnitTest { .setTemplateType(CodegenTemplateTypeEnum.MASTER_NORMAL.getType())); codegenTableMapper.insert(table); // mock 数据(CodegenColumnDO) - CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())); + CodegenColumnDO column01 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()) + .setOrdinalPosition(1)); codegenColumnMapper.insert(column01); - CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId())); + CodegenColumnDO column02 = randomPojo(CodegenColumnDO.class, o -> o.setTableId(table.getId()) + .setOrdinalPosition(2)); codegenColumnMapper.insert(column02); // mock 数据(sub CodegenTableDO) CodegenTableDO subTable = randomPojo(CodegenTableDO.class,