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:
+2
-2
@@ -28,10 +28,10 @@ public interface DedupeTotalDataMapper extends BaseMapper<DedupeTotalDataEntity>
|
||||
@Insert("""
|
||||
<script>
|
||||
INSERT IGNORE INTO biz_dedupe_total_data
|
||||
(data_value, country, group_id, uploader_user_id, uploader_username)
|
||||
(data_value, country, group_id, uploader_user_id, uploader_username, company_id)
|
||||
VALUES
|
||||
<foreach collection="rows" item="row" separator=",">
|
||||
(#{row.dataValue}, #{row.country}, #{row.groupId}, #{row.uploaderUserId}, #{row.uploaderUsername})
|
||||
(#{row.dataValue}, #{row.country}, #{row.groupId}, #{row.uploaderUserId}, #{row.uploaderUsername}, #{row.companyId})
|
||||
</foreach>
|
||||
</script>
|
||||
""")
|
||||
|
||||
+10
@@ -15,6 +15,16 @@ public class DedupeTotalDataEntity {
|
||||
|
||||
@TableId(type = IdType.AUTO)
|
||||
private Long id;
|
||||
/**
|
||||
* 归属公司(V145 加列)。写侧一律显式落值——create 与 insertBatchIgnore 的入口都经
|
||||
* {@code CompanyScopeSupport.requireCompanyId(操作者)} 解析后带进每个 insert,不许依赖列上那条
|
||||
* 临时的 {@code DEFAULT 1}:V145 留它只为历史行与同批的旧 jar 不报 1364,待摘
|
||||
* (CompanyDefaultGuard 哨兵在喊)。
|
||||
*
|
||||
* <p>读侧仍按 {@code uploader_user_id} 认公司(见 DedupeTotalDataService.applyCompanyScope):
|
||||
* 历史行该列全部是 DEFAULT 兜的 1,对区分公司没有信息量。</p>
|
||||
*/
|
||||
private Long companyId;
|
||||
private String dataValue;
|
||||
private String country;
|
||||
private Long groupId;
|
||||
|
||||
+21
-11
@@ -468,6 +468,7 @@ public class DedupeTotalDataService {
|
||||
@Transactional
|
||||
public DedupeTotalDataItemVo create(DedupeTotalDataCreateRequest request, Long operatorId) {
|
||||
AdminUserEntity uploader = getOperator(operatorId);
|
||||
Long companyId = companyScopeSupport.requireCompanyId(uploader);
|
||||
ShopManageGroupEntity group = resolveWritableGroup(request.getGroupId(), uploader);
|
||||
String dataValue = normalizeComparableValue(request.getDataValue());
|
||||
ensureUnique(dataValue, null);
|
||||
@@ -476,6 +477,7 @@ public class DedupeTotalDataService {
|
||||
entity.setGroupId(group.getId());
|
||||
entity.setUploaderUserId(uploader.getId());
|
||||
entity.setUploaderUsername(uploader.getUsername());
|
||||
entity.setCompanyId(companyId);
|
||||
dedupeTotalDataMapper.insert(entity);
|
||||
return toItemVo(getById(entity.getId()), group.getGroupName());
|
||||
}
|
||||
@@ -526,6 +528,7 @@ public class DedupeTotalDataService {
|
||||
throw new BusinessException("导入文件超过大小上限");
|
||||
}
|
||||
AdminUserEntity uploader = getOperator(operatorId);
|
||||
Long companyId = companyScopeSupport.requireCompanyId(uploader);
|
||||
ShopManageGroupEntity group = resolveWritableGroup(groupId, uploader);
|
||||
if (!importSlots.tryAcquire()) {
|
||||
throw new BusinessException("导入任务过多,请稍后重试");
|
||||
@@ -546,7 +549,8 @@ public class DedupeTotalDataService {
|
||||
File tempFile = saveMultipartToTempFile(file);
|
||||
String filename = file.getOriginalFilename();
|
||||
Thread.ofVirtual().start(() -> runImportTask(
|
||||
importId, tempFile, filename, uploader.getId(), uploader.getUsername(), group.getId()));
|
||||
importId, tempFile, filename, uploader.getId(), uploader.getUsername(), group.getId(),
|
||||
companyId));
|
||||
} catch (Exception e) {
|
||||
importSlots.release();
|
||||
importProgressMap.remove(importId);
|
||||
@@ -660,7 +664,7 @@ public class DedupeTotalDataService {
|
||||
}
|
||||
|
||||
private void runImportTask(String importId, File tempFile, String filename,
|
||||
Long uploaderUserId, String uploaderUsername, Long groupId) {
|
||||
Long uploaderUserId, String uploaderUsername, Long groupId, Long companyId) {
|
||||
DedupeTotalDataImportProgressVo progress = importProgressMap.get(importId);
|
||||
if (progress == null) {
|
||||
importSlots.release();
|
||||
@@ -669,7 +673,7 @@ public class DedupeTotalDataService {
|
||||
progress.setStatus("running");
|
||||
try (InputStream inputStream = Files.newInputStream(tempFile.toPath())) {
|
||||
DedupeTotalDataImportVo result = importFromExcelInternal(
|
||||
inputStream, filename, progress, uploaderUserId, uploaderUsername, groupId);
|
||||
inputStream, filename, progress, uploaderUserId, uploaderUsername, groupId, companyId);
|
||||
progress.setTotalRows(result.getTotalRows());
|
||||
progress.setAsinCount(result.getAsinCount());
|
||||
progress.setInsertedCount(result.getInsertedCount());
|
||||
@@ -721,11 +725,12 @@ public class DedupeTotalDataService {
|
||||
throw new BusinessException("请上传 xlsx 文件");
|
||||
}
|
||||
AdminUserEntity uploader = getOperator(operatorId);
|
||||
Long companyId = companyScopeSupport.requireCompanyId(uploader);
|
||||
ShopManageGroupEntity group = resolveWritableGroup(groupId, uploader);
|
||||
try (InputStream inputStream = file.getInputStream()) {
|
||||
return importFromExcelInternal(
|
||||
inputStream, file.getOriginalFilename(), null, uploader.getId(), uploader.getUsername(),
|
||||
group.getId());
|
||||
group.getId(), companyId);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
@@ -759,7 +764,7 @@ public class DedupeTotalDataService {
|
||||
private DedupeTotalDataImportVo importFromExcelInternal(InputStream inputStream, String filename,
|
||||
DedupeTotalDataImportProgressVo progress,
|
||||
Long uploaderUserId, String uploaderUsername,
|
||||
Long groupId) {
|
||||
Long groupId, Long companyId) {
|
||||
String lowerFilename = filename == null ? "" : filename.toLowerCase();
|
||||
if (!(lowerFilename.endsWith(".xlsx") || lowerFilename.endsWith(".xls"))) {
|
||||
throw new BusinessException("仅支持 .xlsx 或 .xls 文件");
|
||||
@@ -769,7 +774,7 @@ public class DedupeTotalDataService {
|
||||
// 此前 WorkbookFactory.create 把整表读成 DOM(50 万行约 1~2GB 堆),且行数上限在
|
||||
// 加载完之后才校验(等于没有防线)。现在内存只保留单批待插入值与文件内去重集合,
|
||||
// 行数上限在迭代过程中即时生效。
|
||||
ImportSession session = new ImportSession(progress, groupId, uploaderUserId, uploaderUsername);
|
||||
ImportSession session = new ImportSession(progress, groupId, uploaderUserId, uploaderUsername, companyId);
|
||||
try {
|
||||
ExcelStreamReader.readFirstSheet(inputStream, new ExcelStreamReader.SheetRowHandler() {
|
||||
private Integer countryColumnIndex;
|
||||
@@ -845,6 +850,7 @@ public class DedupeTotalDataService {
|
||||
private final Long groupId;
|
||||
private final Long uploaderUserId;
|
||||
private final String uploaderUsername;
|
||||
private final Long companyId;
|
||||
private final Set<String> seenInFile = new HashSet<>();
|
||||
private final List<String> pendingValues = new ArrayList<>(IMPORT_BATCH_SIZE);
|
||||
private final Map<String, String> pendingCountries = new HashMap<>();
|
||||
@@ -855,11 +861,12 @@ public class DedupeTotalDataService {
|
||||
private int skippedCount;
|
||||
|
||||
private ImportSession(DedupeTotalDataImportProgressVo progress, Long groupId,
|
||||
Long uploaderUserId, String uploaderUsername) {
|
||||
Long uploaderUserId, String uploaderUsername, Long companyId) {
|
||||
this.progress = progress;
|
||||
this.groupId = groupId;
|
||||
this.uploaderUserId = uploaderUserId;
|
||||
this.uploaderUsername = uploaderUsername;
|
||||
this.companyId = companyId;
|
||||
}
|
||||
|
||||
private void acceptRow(Map<Integer, String> rowMap, Integer countryColumnIndex) {
|
||||
@@ -897,7 +904,7 @@ public class DedupeTotalDataService {
|
||||
return;
|
||||
}
|
||||
ImportBatchResult batch = insertImportBatch(
|
||||
pendingValues, pendingCountries, groupId, uploaderUserId, uploaderUsername);
|
||||
pendingValues, pendingCountries, groupId, uploaderUserId, uploaderUsername, companyId);
|
||||
insertedCount += batch.insertedCount();
|
||||
skippedCount += batch.skippedCount();
|
||||
pendingValues.clear();
|
||||
@@ -928,7 +935,8 @@ public class DedupeTotalDataService {
|
||||
Map<String, String> countriesByValue,
|
||||
Long groupId,
|
||||
Long uploaderUserId,
|
||||
String uploaderUsername) {
|
||||
String uploaderUsername,
|
||||
Long companyId) {
|
||||
if (values == null || values.isEmpty()) {
|
||||
return new ImportBatchResult(0, 0);
|
||||
}
|
||||
@@ -944,6 +952,7 @@ public class DedupeTotalDataService {
|
||||
entity.setGroupId(groupId);
|
||||
entity.setUploaderUserId(uploaderUserId);
|
||||
entity.setUploaderUsername(uploaderUsername);
|
||||
entity.setCompanyId(companyId);
|
||||
rows.add(entity);
|
||||
}
|
||||
if (rows.isEmpty()) {
|
||||
@@ -1285,8 +1294,9 @@ public class DedupeTotalDataService {
|
||||
* 公司 tab 收窄:本表按「上传账号」认公司——{@code uploader_user_id} 是 NOT NULL(V75 先回灌再收紧),
|
||||
* 且有 idx_uploader_user_id_id 可走点查。
|
||||
*
|
||||
* <p>不读 V145 新加的 company_id:写入侧实体没有该字段、{@code insertBatchIgnore} 的列清单也不带,
|
||||
* 全靠 MySQL 的 DEFAULT 1 兜底,所以第 2 家账号新导入的行今天仍会被盖成公司 1。那一列不能当读侧依据。</p>
|
||||
* <p>不按 V145 加的 company_id 筛:那一列对 V145 之前的历史行全部是 DEFAULT 兜的 1,没有区分力。
|
||||
* 写侧现已显式落值(create 与 insertBatchIgnore 都带 company_id),但读侧一旦改用它,历史行会整批归到
|
||||
* 公司 1——所以那列只供写入留存与 uk_company_data_value 使用,读侧口径维持按上传账号推导。</p>
|
||||
*
|
||||
* <p>空集给一条不可能命中的条件而不是"不筛":tab 选了这家公司却看得到全平台,比看不到更坏。</p>
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
-- =============================================================
|
||||
-- 迁移 V152__columns_shop_key_company_id_drop_default
|
||||
-- 模板:docs/flyway-migration-template.md
|
||||
-- 需求:2026-10-06「优先处理 columns 等三列 DEFAULT 问题」
|
||||
-- 合并依据:V150 头注释第 6 段与 docs/migration-inventory.md 的同款待办都要求
|
||||
-- 「columns 与 biz_shop_key 这两条摘 DEFAULT 合并成一版发出」(两条都是纯元数据、
|
||||
-- 前提各自独立);第三条 biz_dedupe_total_data 为什么不在本版见第 6 段。
|
||||
--
|
||||
-- 1. 变更目的:摘掉两列在混合版本窗口期留的兜底默认值 DEFAULT 1——
|
||||
-- 留着的唯一后果是:任何绕过显式落列的写入被静默塞进第 1 家(厦门米椿花)。
|
||||
-- 摘掉之后同类写入会撞 MySQL 1364(Field 'company_id' doesn't have a default value)
|
||||
-- 炸在日志里——这是取舍,不是缺陷:宁可当场炸,也不要静默归错公司(同 V144 的取舍)。
|
||||
--
|
||||
-- 摘除的前提【已逐条取证,2026-10-07】:
|
||||
-- · 带 V149/V150 的 jar 已在两台同版运行:flyway_schema_history 只读实查,
|
||||
-- 149/150/151 三条 success=1、installed_on=2026-10-06 17:32:29(此后 10-07 各批 jar
|
||||
-- 均为其后代,写侧代码只多不少);
|
||||
-- · columns 的 INSERT 通道都显式落 company_id:PermissionMenuService.create /
|
||||
-- copyTreeFromTemplate;PermissionMenuSchemaInitializer 的种子 INSERT(生产默认关:
|
||||
-- @ConditionalOnProperty 要求 AIIMAGE_PERMISSION_SCHEMA_INIT_ENABLED=true,
|
||||
-- 两台 .env 实查未设);
|
||||
-- · biz_shop_key 的唯一 INSERT 通道 ShopKeyService.create 显式落 company_id;
|
||||
-- · 两列的 COLUMN_DEFAULT 当前都是 1(10-07 只读实查 information_schema)。
|
||||
--
|
||||
-- 2. 影响表与数据量:只动两列的默认值属性。不写数据、不加索引、不删列。
|
||||
-- · columns:154 行(10-07 只读取证:公司 1/2/3 各 52/51/51 行);
|
||||
-- · biz_shop_key:16 行(全部 company_id = 1)。
|
||||
-- DROP DEFAULT 不改变任何已存行的读值:instant 加列后的历史行读值由加列时记录的
|
||||
-- 默认值给出(本机 MySQL 8.4.9 实验证实摘除前后读值一致),摘除只影响"之后省略
|
||||
-- 该列的 INSERT"。
|
||||
--
|
||||
-- 3. 锁表风险:极低。ALTER TABLE ... ALTER COLUMN ... DROP DEFAULT 是纯元数据操作
|
||||
-- (瞬时完成、不重建表、不扫描行)。
|
||||
-- 【禁写成 MODIFY COLUMN ... NOT NULL】——MODIFY 走重建表,重建按"当时的列默认值"
|
||||
-- 物化历史行;默认值刚被摘掉,历史归属就失去依据(V144/V150 头注释同款禁令)。
|
||||
-- 不碰 users / company / biz_shop_manage / biz_dedupe_total_data 等任何其他表。
|
||||
--
|
||||
-- 4. 验证 SQL(迁移后跑,不许只看启动日志的 Successfully applied):
|
||||
-- SHOW COLUMNS FROM `columns` LIKE 'company_id';
|
||||
-- -> 期望 Null = NO,Default = NULL
|
||||
-- SHOW COLUMNS FROM `biz_shop_key` LIKE 'company_id';
|
||||
-- -> 期望 Null = NO,Default = NULL
|
||||
-- SELECT TABLE_NAME, COLUMN_DEFAULT FROM information_schema.COLUMNS
|
||||
-- WHERE TABLE_SCHEMA = DATABASE() AND COLUMN_NAME = 'company_id'
|
||||
-- AND TABLE_NAME IN ('columns','biz_shop_key');
|
||||
-- -> 期望两行 COLUMN_DEFAULT 均为 NULL
|
||||
-- 启动期 CompanyDefaultGuard 对这两张表不再打 COMPANY_DEFAULT_STILL_PRESENT
|
||||
-- (签名与清单原样保留:哪天 DEFAULT 被装回——dump 重建、手滑 SET DEFAULT——
|
||||
-- 它会再喊出来;biz_dedupe_total_data 仍会喊,它还没摘,见第 6 段)。
|
||||
--
|
||||
-- 5. 回滚步骤:人工补丁 backend-java/docs/rollback-V152-columns-shop-key-default.sql
|
||||
-- (在 Flyway 扫描路径之外;版本化回滚等于让"决定回滚"这件事自己发生)。
|
||||
-- 触发场景是「摘错了」——场外的、省略 company_id 的写入被 1364 打出来。
|
||||
-- 顺序:停两台应用 → 跑脚本(两条 ALTER COLUMN ... SET DEFAULT 1,纯元数据)
|
||||
-- → 删 flyway_schema_history 的 version='152' 行 → 回滚 jar → 起应用。
|
||||
-- 仅仅回滚 jar(代码问题、与默认值无关)不需要本补丁:V149/V150 起的 jar 全部
|
||||
-- 显式落列,对默认值在不在毫无感知,V152 保持应用态即可。
|
||||
--
|
||||
-- 6. 上线窗口与「为什么只剩这一条」:随 jar 双节点同窗口发布,金丝雀过了立刻补第二台。
|
||||
-- biz_dedupe_total_data 的 DEFAULT 1 不在本版:它的写入侧(insertBatchIgnore 的列
|
||||
-- 清单)历史上不带 company_id、实体也没有该字段,全靠 DEFAULT 兜底——先修写侧、
|
||||
-- 上线并验证真实导入之后才谈摘(修复已落码,尚未出厂)。在那之前启动期哨兵继续
|
||||
-- 通过 COMPANY_DEFAULT_STILL_PRESENT 盯它,不靠人记得。
|
||||
-- 【不要顺手加入 biz_product_category / biz_invalid_asin_data】:那两张共享表的
|
||||
-- DEFAULT 1 是承重约束(写侧有意不落该列、靠 DEFAULT 满足 NOT NULL),摘它=每次
|
||||
-- 新建报 1364。它们真正消失的时点是另一笔迁移直接 DROP COLUMN,不是这里。
|
||||
--
|
||||
-- 【可重跑性】本文件只有两条 ALTER COLUMN ... DROP DEFAULT:对"已无默认值"的列重复
|
||||
-- 执行是无操作(发布前在 scratch MySQL 8.4.9 上重放两遍证实),列由 V149/V150 建、
|
||||
-- Flyway 保证顺序 ⇒ 不需要 information_schema 守卫(与 V144 同口径;守卫是留给
|
||||
-- "重复执行会报错"的 ADD COLUMN/INDEX 那一类)。按「补数据 → flyway repair → 重启」
|
||||
-- 恢复时重跑本文件安全。
|
||||
-- =============================================================
|
||||
|
||||
ALTER TABLE `columns`
|
||||
ALTER COLUMN company_id DROP DEFAULT;
|
||||
|
||||
ALTER TABLE `biz_shop_key`
|
||||
ALTER COLUMN company_id DROP DEFAULT;
|
||||
Reference in New Issue
Block a user