fix(多租户): V152 摘 columns/biz_shop_key 的 company_id 列默认值,去重库写侧显式带归属
- V152:两条裸 ALTER COLUMN DROP DEFAULT(纯元数据、可重跑)。依据 V150 §6「合并成一版」 与 migration-inventory 取号;前提=V149/150/151 已于 10-06 17:32:29 落库、写侧三条通道 (PermissionMenuService create/copyTreeFromTemplate、ShopKeyService)均显式落列。 - 去重库:DedupeTotalDataMapper/Entity/Service 的 INSERT 显式写 company_id,不再靠列 DEFAULT 1 兜底(第二家公司开通后静默归错)。biz_dedupe_total_data 的 DEFAULT 摘除排在 本批上线并验证写入之后,不在本版。 - CompanyDefaultDropV152Test 9 例(含 V149/150/151 已应用字节的 CRC 冻结)+ dedupe 两支测试 + rollback-V152 人工补丁(默认档可直接执行)+ 盘点文档同步(取号起点改 V153)。
This commit is contained in:
+65
@@ -0,0 +1,65 @@
|
||||
package com.nanri.aiimage.modules.dedupe.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.nanri.aiimage.modules.dedupe.model.entity.DedupeTotalDataEntity;
|
||||
import org.apache.ibatis.builder.annotation.MapperAnnotationBuilder;
|
||||
import org.apache.ibatis.mapping.BoundSql;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* {@code insertBatchIgnore} 生成 SQL 的审计:去重库写侧必须显式带 {@code company_id}。
|
||||
*
|
||||
* <p>为什么必须单独有一支:服务层测试用 mock 捕获的是实体对象,看不到最终 SQL——本表写侧
|
||||
* 靠的是注解里的列清单(不是 MyBatis-Plus 的实体字段推导),把 {@code company_id} 从清单里漏掉
|
||||
* 或把 {@code #{row.companyId}} 写成 {@code ${...}} 都是"SQL 变了而 service 测试照样绿"。
|
||||
* 走 {@code MapperAnnotationBuilder} 解析注解、向真 {@code BoundSql} 断言,
|
||||
* 口径照抄 {@code ShopDataDuplicateCheckCompanyScopeTest#boundSqlFor}。</p>
|
||||
*/
|
||||
class DedupeTotalDataMapperSqlTest {
|
||||
|
||||
@Test
|
||||
void insertBatchCarriesCompanyIdOnEveryRow() throws Exception {
|
||||
BoundSql boundSql = boundSqlFor(2);
|
||||
|
||||
String sql = boundSql.getSql().replaceAll("\\s+", " ");
|
||||
assertTrue(sql.contains("INSERT IGNORE INTO biz_dedupe_total_data"),
|
||||
"批插语句应保留 INSERT IGNORE 形态: " + sql);
|
||||
assertTrue(sql.contains(
|
||||
"(data_value, country, group_id, uploader_user_id, uploader_username, company_id)"),
|
||||
"列清单必须逐字带上 company_id(去重库写侧修复的就是这一列): " + sql);
|
||||
// 每行 6 个值占位符:写成 ${row.companyId} 或漏掉一列都会让这里的数字对不上
|
||||
assertEquals(12, sql.chars().filter(c -> c == '?').count(),
|
||||
"2 行应有 12 个 ? 占位符(6 列 x 2 行): " + sql);
|
||||
assertEquals(12, boundSql.getParameterMappings().size(),
|
||||
"参数绑定数应等于占位符数");
|
||||
assertFalse(sql.contains("$"), "不得出现 ${} 拼接面: " + sql);
|
||||
}
|
||||
|
||||
private static BoundSql boundSqlFor(int rowCount) throws Exception {
|
||||
Method method = DedupeTotalDataMapper.class.getMethod("insertBatchIgnore", List.class);
|
||||
String script = String.join(" ", method.getAnnotation(org.apache.ibatis.annotations.Insert.class).value());
|
||||
assertTrue(script.contains("company_id"), "注解原文应含 company_id(生成 SQL 才可能含): " + script);
|
||||
Configuration configuration = new MybatisConfiguration();
|
||||
new MapperAnnotationBuilder(configuration, DedupeTotalDataMapper.class).parse();
|
||||
MappedStatement statement = configuration.getMappedStatement(
|
||||
DedupeTotalDataMapper.class.getName() + ".insertBatchIgnore");
|
||||
List<DedupeTotalDataEntity> rows = new java.util.ArrayList<>(rowCount);
|
||||
for (int i = 0; i < rowCount; i++) {
|
||||
DedupeTotalDataEntity row = new DedupeTotalDataEntity();
|
||||
row.setDataValue("B00000000" + i);
|
||||
row.setCompanyId(2L);
|
||||
rows.add(row);
|
||||
}
|
||||
return statement.getBoundSql(Map.of("rows", rows));
|
||||
}
|
||||
}
|
||||
+29
@@ -7,6 +7,7 @@ import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.dedupe.mapper.DedupeTotalDataMapper;
|
||||
import com.nanri.aiimage.modules.dedupe.model.dto.DedupeTotalDataCreateRequest;
|
||||
import com.nanri.aiimage.modules.dedupe.model.entity.DedupeTotalDataEntity;
|
||||
import com.nanri.aiimage.common.service.CompanyScopeSupport;
|
||||
import com.nanri.aiimage.common.service.NodeSharedStore;
|
||||
import com.nanri.aiimage.modules.dedupe.model.vo.DedupeTotalDataImportProgressVo;
|
||||
import com.nanri.aiimage.modules.dedupe.model.vo.DedupeTotalDataItemVo;
|
||||
@@ -64,6 +65,8 @@ class DedupeTotalDataServiceTest {
|
||||
@Mock
|
||||
private ShopManageGroupMapper shopManageGroupMapper;
|
||||
@Mock
|
||||
private CompanyScopeSupport companyScopeSupport;
|
||||
@Mock
|
||||
private PlatformTransactionManager transactionManager;
|
||||
@Mock
|
||||
private TransactionStatus transactionStatus;
|
||||
@@ -74,6 +77,7 @@ class DedupeTotalDataServiceTest {
|
||||
void createRecordsUploaderIdentity() {
|
||||
AdminUserEntity uploader = user(23L, "normal", "member-a");
|
||||
when(adminUserMapper.selectById(23L)).thenReturn(uploader);
|
||||
when(companyScopeSupport.requireCompanyId(any(AdminUserEntity.class))).thenReturn(1L);
|
||||
stubWritableGroup(23L, 7L);
|
||||
when(dedupeTotalDataMapper.selectOne(any())).thenReturn(null);
|
||||
when(dedupeTotalDataMapper.insert(any(DedupeTotalDataEntity.class))).thenAnswer(invocation -> {
|
||||
@@ -102,6 +106,7 @@ class DedupeTotalDataServiceTest {
|
||||
assertEquals("member-a", captor.getValue().getUploaderUsername());
|
||||
assertEquals("B012345678", captor.getValue().getDataValue());
|
||||
assertEquals(7L, captor.getValue().getGroupId());
|
||||
assertEquals(1L, captor.getValue().getCompanyId(), "create 必须显式落 company_id,不许靠列上的 DEFAULT 兜");
|
||||
assertEquals("member-a", item.getUsername());
|
||||
assertEquals(7L, item.getGroupId());
|
||||
assertEquals("group-7", item.getGroupName());
|
||||
@@ -179,6 +184,7 @@ class DedupeTotalDataServiceTest {
|
||||
@Test
|
||||
void duplicateExcelValueKeepsOriginalUploader() throws Exception {
|
||||
when(adminUserMapper.selectById(23L)).thenReturn(user(23L, "normal", "member-a"));
|
||||
when(companyScopeSupport.requireCompanyId(any(AdminUserEntity.class))).thenReturn(1L);
|
||||
stubWritableGroup(23L, 7L);
|
||||
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678")))
|
||||
.thenReturn(List.of("B012345678"));
|
||||
@@ -191,9 +197,27 @@ class DedupeTotalDataServiceTest {
|
||||
verify(dedupeTotalDataMapper, never()).insert(any(DedupeTotalDataEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void importRejectsOperatorWithoutCompany() throws Exception {
|
||||
AdminUserEntity noCompany = user(23L, "normal", "member-a");
|
||||
noCompany.setCompanyId(null);
|
||||
when(adminUserMapper.selectById(23L)).thenReturn(noCompany);
|
||||
when(companyScopeSupport.requireCompanyId(any(AdminUserEntity.class)))
|
||||
.thenThrow(new BusinessException(403, "当前账号没有所属公司,无法按公司读写数据"));
|
||||
|
||||
BusinessException exception = assertThrows(
|
||||
BusinessException.class,
|
||||
() -> service.importFromExcel(asinWorkbook("B012345678"), 7L, 23L));
|
||||
|
||||
assertEquals("当前账号没有所属公司,无法按公司读写数据", exception.getMessage());
|
||||
verify(dedupeTotalDataMapper, never()).insertBatchIgnore(any());
|
||||
verify(dedupeTotalDataMapper, never()).insert(any(DedupeTotalDataEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void importReadsAndNormalizesCountryColumn() throws Exception {
|
||||
when(adminUserMapper.selectById(23L)).thenReturn(user(23L, "normal", "member-a"));
|
||||
when(companyScopeSupport.requireCompanyId(any(AdminUserEntity.class))).thenReturn(1L);
|
||||
stubWritableGroup(23L, 7L);
|
||||
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678"))).thenReturn(List.of());
|
||||
when(dedupeTotalDataMapper.insertBatchIgnore(any())).thenReturn(1);
|
||||
@@ -206,11 +230,14 @@ class DedupeTotalDataServiceTest {
|
||||
verify(dedupeTotalDataMapper).insertBatchIgnore(captor.capture());
|
||||
// US 不在五国范围内,应被过滤掉
|
||||
assertEquals("DE,UK", captor.getValue().getFirst().getCountry());
|
||||
assertEquals(1L, captor.getValue().getFirst().getCompanyId(),
|
||||
"import 批插必须显式带 company_id(DEFAULT 是给历史行与旧 jar 的临时兜底,待摘)");
|
||||
}
|
||||
|
||||
@Test
|
||||
void importKeepsCountryBlankWhenHeaderMissing() throws Exception {
|
||||
when(adminUserMapper.selectById(23L)).thenReturn(user(23L, "normal", "member-a"));
|
||||
when(companyScopeSupport.requireCompanyId(any(AdminUserEntity.class))).thenReturn(1L);
|
||||
stubWritableGroup(23L, 7L);
|
||||
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678"))).thenReturn(List.of());
|
||||
when(dedupeTotalDataMapper.insertBatchIgnore(any())).thenReturn(1);
|
||||
@@ -247,6 +274,7 @@ class DedupeTotalDataServiceTest {
|
||||
@Test
|
||||
void concurrentDuplicateDuringImportIsSkipped() throws Exception {
|
||||
when(adminUserMapper.selectById(23L)).thenReturn(user(23L, "normal", "member-a"));
|
||||
when(companyScopeSupport.requireCompanyId(any(AdminUserEntity.class))).thenReturn(1L);
|
||||
stubWritableGroup(23L, 7L);
|
||||
when(dedupeTotalDataMapper.selectOne(any())).thenReturn(null);
|
||||
when(dedupeTotalDataMapper.selectExistingDataValues(List.of("B012345678"))).thenReturn(List.of());
|
||||
@@ -531,6 +559,7 @@ class DedupeTotalDataServiceTest {
|
||||
user.setRole(role);
|
||||
user.setUsername(username);
|
||||
user.setIsAdmin("normal".equals(role) ? 0 : 1);
|
||||
user.setCompanyId(1L);
|
||||
return user;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user